Repository: ansible/ansible

Issue:
ansible_service_mgr fact for sysvinit incorrect in ansible 2.4.0
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
Ansible fact / variable `ansible_service_mgr` 

##### ANSIBLE VERSION
```
ansible 2.4.0.0
```

##### CONFIGURATION
does not matter

##### OS / ENVIRONMENT
Target Debian Linux 7.11 (possible other Systems with sysvinit)

##### SUMMARY
Prior to ansible 2.4.0.0 ansible_service_mgr contained the value `sysvinit`, after the update to 2.4.0.0 ansible falls back to `service`. I would call it "alternative fact" right now, at least some roles fail due to this behaviour.

##### STEPS TO REPRODUCE
Outputting the fact ansible_service_mgr for a host running sysvinit (for example Debian 7.11 with sysvinit)

```
ansible -i host.example.com, -m setup  host.example.com | grep ansible_service_mgr
```

##### EXPECTED RESULTS
```
"ansible_service_mgr": "sysvinit",
```

##### ACTUAL RESULTS
```
"ansible_service_mgr": "service",
```


Discussion:
Looking into https://github.com/ansible/ansible/blob/stable-2.4/lib/ansible/module_utils/facts/system/service_mgr.py I see several checks for comparison I'll did these by hand:
```
toke@desktop~$ ansible -i machine.example.com, -m setup machine.example.com |grep ansible_system
        "ansible_system": "Linux", 
        "ansible_system_vendor": "oVirt",
ssh toke@machine
toke@machine~$ ps -p 1 -o comm|tail -n 1
init
toke@machine:~$ cat /proc/1/comm
init
toke@machine:~$ [ -d /etc/init.d/ ] && echo "yes" 
yes
```
hmm, I was pretty sure this was related to ordering of collected facts (what #30777 addresses)
but now I wonder if service_mgr is just using the wrong fact names (without the 'ansible_' prefix)
(See https://github.com/ansible/ansible/compare/devel...alikins:test_svc_mgr for a potential change
to fix)
fix was also cherry-picked to stable-2.4 in 4025b4762908d00f0ba2fee7a4bd8e75941f854b 
(from 95abc1d82e4c40832c802253107ad3f9aeebc68d from https://github.com/ansible/ansible/pull/30777)
Not sure if the fixes should work by now but testing against devel or v2.4.1.0-0.3.rc1 I still get the value "service" for the fact "ansible_service_mgr".

@alikins Should this issue reopened?
I've put together a small vagrant test: https://gist.github.com/toke/d5de03c70c9c7f1a2ec6acd7a9e3753a


Resolving patch:
diff --git a/lib/ansible/module_utils/facts/system/service_mgr.py b/lib/ansible/module_utils/facts/system/service_mgr.py
--- a/lib/ansible/module_utils/facts/system/service_mgr.py
+++ b/lib/ansible/module_utils/facts/system/service_mgr.py
@@ -105,22 +105,22 @@ def collect(self, module=None, collected_facts=None):
 
         # FIXME: replace with a system->service_mgr_name map?
         # start with the easy ones
-        elif collected_facts.get('distribution', None) == 'MacOSX':
+        elif collected_facts.get('ansible_distribution', None) == 'MacOSX':
             # FIXME: find way to query executable, version matching is not ideal
             if LooseVersion(platform.mac_ver()[0]) >= LooseVersion('10.4'):
                 service_mgr_name = 'launchd'
             else:
                 service_mgr_name = 'systemstarter'
-        elif 'BSD' in collected_facts.get('system', '') or collected_facts.get('system') in ['Bitrig', 'DragonFly']:
+        elif 'BSD' in collected_facts.get('ansible_system', '') or collected_facts.get('ansible_system') in ['Bitrig', 'DragonFly']:
             # FIXME: we might want to break out to individual BSDs or 'rc'
             service_mgr_name = 'bsdinit'
-        elif collected_facts.get('system') == 'AIX':
+        elif collected_facts.get('ansible_system') == 'AIX':
             service_mgr_name = 'src'
-        elif collected_facts.get('system') == 'SunOS':
+        elif collected_facts.get('ansible_system') == 'SunOS':
             service_mgr_name = 'smf'
-        elif collected_facts.get('distribution') == 'OpenWrt':
+        elif collected_facts.get('ansible_distribution') == 'OpenWrt':
             service_mgr_name = 'openwrt_init'
-        elif collected_facts.get('system') == 'Linux':
+        elif collected_facts.get('ansible_system') == 'Linux':
             # FIXME: mv is_systemd_managed
             if self.is_systemd_managed(module=module):
                 service_mgr_name = 'systemd'


Test patch: