Repository: ansible/ansible

Issue:
Sysctl module fails on OEL5
<!---
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
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
sysctl
##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
Running on: Ubuntu 16.04
Works when remote: OEL6 (python 2.6.6)
Doesn't work when remote: OEL5 (python 2.4.3)

##### SUMMARY
<!--- Explain the problem briefly -->
sysctl fails when trying to set a parameter on OEL5.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- hosts: hostname
  tasks:
  - sysctl:
      name: kernel.shmmni
      value: 4096
      state: present
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Parameter should be set.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
fatal: [hostname]: FAILED! =>
{
"changed": false, 
"failed": true, 
"module_stderr": "Shared connection to hostname closed.\r\n",

"module_stdout": "
Traceback (most recent call last):\r\n
  File \"/tmp/ansible_ctF6H0/ansible_module_sysctl.py\", line 397, in ?\r\n
    main()\r\n
  File \"/tmp/ansible_ctF6H0/ansible_module_sysctl.py\", line 391, in main\r\n
    result = SysctlModule(module)\r\n
  File \"/tmp/ansible_ctF6H0/ansible_module_sysctl.py\", line 142, in __init__\r\n
    self.process()\r\n
  File \"/tmp/ansible_ctF6H0/ansible_module_sysctl.py\", line 162, in process\r\n
    self.read_sysctl_file()\r\n
  File \"/tmp/ansible_ctF6H0/ansible_module_sysctl.py\", line 320, in read_sysctl_file\r\n
    if not line or line.startswith((\"#\", \";\")):\r\n
TypeError: expected a character buffer object\r\n
", 

"msg": "MODULE FAILURE",
"rc": 0}
```



Discussion:
cc @davixx
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Apparently python 2.4 is not happy with the expression startswith(("#", ';'))

The following fixes the problem:
```
@@ -317,7 +317,7 @@
             self.file_lines.append(line)

             # don't split empty lines or comments
-            if not line or line.startswith(("#", ";")):
+            if not line or line.startswith("#") or line.startswith(";"):
                 continue

             k, v = line.split('=',1)
@@ -330,7 +330,7 @@
         checked = []
         self.fixed_lines = []
         for line in self.file_lines:
-            if not line.strip() or line.strip().startswith(("#", ";")):
+            if not line.strip() or line.strip().startswith("#") or line.strip().startswith(";"):
                 self.fixed_lines.append(line)
                 continue
             tmpline = line.strip()
```

BTW, repeated calls to line.strip() seem like overkill, but didn't mean to be more intrusive.

Resolving patch:
diff --git a/lib/ansible/modules/system/sysctl.py b/lib/ansible/modules/system/sysctl.py
--- a/lib/ansible/modules/system/sysctl.py
+++ b/lib/ansible/modules/system/sysctl.py
@@ -317,7 +317,7 @@ def read_sysctl_file(self):
             self.file_lines.append(line)
 
             # don't split empty lines or comments
-            if not line or line.startswith(("#", ";")):
+            if not line or line.startswith("#") or line.startswith(";"):
                 continue
 
             k, v = line.split('=',1)
@@ -330,7 +330,7 @@ def fix_lines(self):
         checked = []
         self.fixed_lines = []
         for line in self.file_lines:
-            if not line.strip() or line.strip().startswith(("#", ";")):
+            if not line.strip() or line.startswith("#") or line.startswith(";"):
                 self.fixed_lines.append(line)
                 continue
             tmpline = line.strip()


Test patch: