Repository: ansible/ansible

Issue:
sysctl errors on comments
<!---
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.2.0.0
  config file = /var/lib/awx/jason/management/ansible.cfg
  configured module search path = Default w/o overrides
```

##### 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.
-->
Red Hat 6 and 7

##### SUMMARY
<!--- Explain the problem briefly -->
When Ansible is reading sysctl files it errors when encountering a comment.

##### 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 -->
**Variables**
```yaml
sysctl:
  postgres:
    file: "/etc/sysctl.d/99-postgres.conf"
    params:
      - name: "kernel.sem"
        value: "150 4000 100 400"
      - name: "kernel.shmall"
        value: 4294967296
      - name: "kernel.shmmax"
        value: 4294967296
      - name: "kernel.shmmni"
        value: 2048
```

**Tasks**
```yaml
---
- name: sysctl header
  lineinfile:
    create: yes
    dest: "{{ item.value.file }}"
    owner: root
    group: root
    insertbefore: BOF
    line: "{{ ansible_managed }}"
  with_dict: '{{ sysctl }}'
  when: (item.value.state is defined and item.value.state == 'present') or
         item.value.state is not defined
  tags: [sysctl]

- name: configure sysctl
  sysctl:
    name: "{{ item.1.name }}"
    value: "{{ item.1.value }}"
    sysctl_file: "{{ item.0.file }}"
    reload: yes
  with_subelements:
    - "{{ sysctl }}"
    - params
    - flags:
      skip_missing: yes
  when: (item.0.state is defined and item.0.state == 'present') or
         item.0.state is not defined
  tags: [sysctl]
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The comment header would be applied to the top of the file and then the sysctl parameters would be applied.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
When Ansible got to the comment in the file it failed.  Removing the comment fixes the issue.

<!--- Paste verbatim command output between quotes below -->
```
Traceback (most recent call last):
  File \"/tmp/ansible_skBeie/ansible_module_sysctl.py\", line 371, in <module>
    main()
  File \"/tmp/ansible_skBeie/ansible_module_sysctl.py\", line 364, in main
    result = SysctlModule(module)
  File \"/tmp/ansible_skBeie/ansible_module_sysctl.py\", line 124, in __init__
    self.process()
  File \"/tmp/ansible_skBeie/ansible_module_sysctl.py\", line 144, in process
    self.read_sysctl_file()
  File \"/tmp/ansible_skBeie/ansible_module_sysctl.py\", line 296, in read_sysctl_file
    k, v = line.split('=',1)
ValueError: need more than 1 value to unpack
```


Discussion:
cc @davixx
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Have an example sysctl file that causes the error?
Code looks like it intends not to try to split comment lines (https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/sysctl.py#L309-L311)

I think that code assumes only lines that start with a '#' are valid comments.  In the sysctl files causing problems, what do the comments look like?
Looking at sysctl.conf man page, looks like the current code wont handle ';' as a comment char.

sysctl.conf man page says 'Lines which begin with a # or ; are considered comments and ignored.', so looks like a bug in the module.



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
@@ -307,7 +307,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(("#", ";")):
                 continue
 
             k, v = line.split('=',1)
@@ -320,7 +320,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.strip().startswith(("#", ";")):
                 self.fixed_lines.append(line)
                 continue
             tmpline = line.strip()


Test patch: