Repository: ansible/ansible

Issue:
ansible_service_mgr incorrectly set to "init"
##### Issue Type:
- Bug Report
##### Ansible Version:

```
ansible 2.0.1.0
  config file = 
  configured module search path = Default w/o overrides
```
##### Ansible Configuration:

ANSIBLE_HOST_KEY_CHECKING=no
ANSIBLE_SSH_PIPELINING=yes
ANSIBLE_INVENTORY=/Users/anandb/repos/ripe/ansible/hosts
ANSIBLE_GATHERING=smart
ANSIBLE_REMOTE_TEMP=/tmp
ANSIBLE_SSH_CONTROL_PATH=/Users/anandb/.ssh/mux/%%r@%%h:%%p
ANSIBLE_HASH_BEHAVIOUR=merge
ANSIBLE_TIMEOUT=75
ANSIBLE_RETRY_FILES_ENABLED=no
##### Environment:

Managing node: MacOS X 10.11
Managed node: CentOS 6 with an older kernel:

```
# uname -a
Linux ee-tll-as51349 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
```

I would like to highlight the fact that on this older system, /proc/1/comm is missing. This should not be a problem, because the setup module should use other checks to determine the init system, but for some reason, fails.
##### Summary:

The fact "ansible_service_mgr" is set to "init" instead of "upstart"
##### Steps To Reproduce:

```
ansible ee-tll-as51349 -m setup -a filter=ansible_service_mgr
```

<!--- You can also paste gist.github.com links for larger files -->
##### Expected Results:

```
ee-tll-as51349 | SUCCESS => {
    "ansible_facts": {
        "ansible_service_mgr": "upstart"
    }, 
    "changed": false
}
```
##### Actual Results:

```
ee-tll-as51349 | SUCCESS => {
    "ansible_facts": {
        "ansible_service_mgr": "init"
    }, 
    "changed": false
}
```

ansible_service_mgr incorrectly set to "init"
##### Issue Type:
- Bug Report
##### Ansible Version:

```
ansible 2.0.1.0
  config file = 
  configured module search path = Default w/o overrides
```
##### Ansible Configuration:

ANSIBLE_HOST_KEY_CHECKING=no
ANSIBLE_SSH_PIPELINING=yes
ANSIBLE_INVENTORY=/Users/anandb/repos/ripe/ansible/hosts
ANSIBLE_GATHERING=smart
ANSIBLE_REMOTE_TEMP=/tmp
ANSIBLE_SSH_CONTROL_PATH=/Users/anandb/.ssh/mux/%%r@%%h:%%p
ANSIBLE_HASH_BEHAVIOUR=merge
ANSIBLE_TIMEOUT=75
ANSIBLE_RETRY_FILES_ENABLED=no
##### Environment:

Managing node: MacOS X 10.11
Managed node: CentOS 6 with an older kernel:

```
# uname -a
Linux ee-tll-as51349 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
```

I would like to highlight the fact that on this older system, /proc/1/comm is missing. This should not be a problem, because the setup module should use other checks to determine the init system, but for some reason, fails.
##### Summary:

The fact "ansible_service_mgr" is set to "init" instead of "upstart"
##### Steps To Reproduce:

```
ansible ee-tll-as51349 -m setup -a filter=ansible_service_mgr
```

<!--- You can also paste gist.github.com links for larger files -->
##### Expected Results:

```
ee-tll-as51349 | SUCCESS => {
    "ansible_facts": {
        "ansible_service_mgr": "upstart"
    }, 
    "changed": false
}
```
##### Actual Results:

```
ee-tll-as51349 | SUCCESS => {
    "ansible_facts": {
        "ansible_service_mgr": "init"
    }, 
    "changed": false
}
```



Discussion:
Would it be possible for you to verify that this patch resolves the issue for you?

``` diff
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
index 39dcd54..1aa16c9 100644
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -581,13 +581,16 @@ class Facts(object):
         else:
             proc_1 = os.path.basename(proc_1)

+        if proc_1 is not None:
+            proc_1 = proc_1.strip()
+
         if proc_1 == 'init' or proc_1.endswith('sh'):
             # many systems return init, so this cannot be trusted, if it ends in 'sh' it probalby is a shell in a container
             proc_1 = None

         # if not init/None it should be an identifiable or custom init, so we are done!
         if proc_1 is not None:
-            self.facts['service_mgr'] = proc_1.strip()
+            self.facts['service_mgr'] = proc_1

         # start with the easy ones
         elif  self.facts['distribution'] == 'MacOSX':
```

I have a rhel6 box, but it is correctly returning `upstart` already, however I have seen some improvement on a rhel5 box I have with this patch.

Would it be possible for you to verify that this patch resolves the issue for you?

``` diff
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
index 39dcd54..1aa16c9 100644
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -581,13 +581,16 @@ class Facts(object):
         else:
             proc_1 = os.path.basename(proc_1)

+        if proc_1 is not None:
+            proc_1 = proc_1.strip()
+
         if proc_1 == 'init' or proc_1.endswith('sh'):
             # many systems return init, so this cannot be trusted, if it ends in 'sh' it probalby is a shell in a container
             proc_1 = None

         # if not init/None it should be an identifiable or custom init, so we are done!
         if proc_1 is not None:
-            self.facts['service_mgr'] = proc_1.strip()
+            self.facts['service_mgr'] = proc_1

         # start with the easy ones
         elif  self.facts['distribution'] == 'MacOSX':
```

I have a rhel6 box, but it is correctly returning `upstart` already, however I have seen some improvement on a rhel5 box I have with this patch.


Resolving patch:
diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py
--- a/lib/ansible/module_utils/facts.py
+++ b/lib/ansible/module_utils/facts.py
@@ -581,13 +581,16 @@ def get_service_mgr_facts(self):
         else:
             proc_1 = os.path.basename(proc_1)
 
+        if proc_1 is not None:
+            proc_1 = proc_1.strip()
+
         if proc_1 == 'init' or proc_1.endswith('sh'):
             # many systems return init, so this cannot be trusted, if it ends in 'sh' it probalby is a shell in a container
             proc_1 = None
 
         # if not init/None it should be an identifiable or custom init, so we are done!
         if proc_1 is not None:
-            self.facts['service_mgr'] = proc_1.strip()
+            self.facts['service_mgr'] = proc_1
 
         # start with the easy ones
         elif  self.facts['distribution'] == 'MacOSX':


Test patch: