Issue 1:
From @lchccieurope on 2016-05-20T08:57:50Z

ISSUE TYPE

Bug Report

COMPONENT NAME

pam_limits

ANSIBLE VERSION

ansible 2.0.2.0
config file =
configured module search path = Default w/o overrides

CONFIGURATION

none

OS / ENVIRONMENT

Controller: MacOSX 10.11.5

Target: CentOS/7

SUMMARY

pam_limits misformats comments, first comment has no "#" (and not even space) prefix, third comment is indented twice.

STEPS TO REPRODUCE

- pam_limits: domain={{item.domain}} limit_type={{item.limit_type}} limit_item={{item.limit_item}} value={{item.value}} comment={{item.comment}}
with_items:
- { domain: 'user1', limit_type: 'soft', limit_item: 'nofile', value: '4096', comment: 'Comment 1' }
- { domain: 'user2', limit_type: 'soft', limit_item: 'nofile', value: '4096', comment: 'Comment 2' }
- { domain: 'user3', limit_type: 'soft', limit_item: 'nofile', value: '4096', comment: 'Comment 3' }

EXPECTED RESULTS

user1 soft nofile 4096 #Comment 1
user2 soft nofile 4096 #Comment 2
user3 soft nofile 4096 #Comment 3
# End of file

ACTUAL RESULTS

# End of file
user1 soft nofile 4096Comment 1
user2 soft nofile 4096 #Comment 2
user3 soft nofile 4096 # #Comment 3

Copied from original issue: ansible/ansible-modules-extras#2264

Issue 2:
ISSUE TYPE

Bug Report

COMPONENT NAME

pam_limits

ANSIBLE VERSION

ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]

CONFIGURATION

Default

OS / ENVIRONMENT

Ansible host : Fedora 25

Ansible target : Centos 7.3 (Vagrant centos/7 image)

SUMMARY

pam_limits breaks limits.conf file if comment is used, making any subsequent pam_limits change fail.

STEPS TO REPRODUCE

Run the below playbook.

The behaviour was discovered in Vagrant controlled Virtualbox VM.

---
- name: Deploy
hosts: all
become: true
become_user: root
vars:
user1_limits:
- { domain: user1, type: soft, name: nofile, value: 1024, comment: User1 }
- { domain: user1, type: hard, name: nofile, value: 65536, comment: User1 }
- { domain: user1, type: soft, name: nproc, value: 2047, comment: User1 }
- { domain: user1, type: hard, name: nproc, value: 16384, comment: User1 }
- { domain: user1, type: soft, name: stack, value: 10240, comment: User1 }
- { domain: user1, type: hard, name: stack, value: 32768, comment: User1 }
tasks:
- name: set user limits
pam_limits:
limit_item: "{{ item.name }}"
limit_type: "{{ item.type }}"
value: "{{ item.value }}"
domain: "{{ item.domain }}"
comment: "{{ item.comment }}"
with_items: "{{ user1_limits }}"

EXPECTED RESULTS

limits set with comments in limits.conf file :

[...]
user1 soft nofile 1024 # User1
user1 hard nofile 65536 # User1
user1 soft nproc 2047 # User1
user1 hard nproc 16384 # User1
user1 soft stack 10240 # User1
user1 hard stack 32768 # User1

PLAY [Deploy] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [default]

TASK [set user limits] *******************************************************
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'nofile', u'value': 1024})
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'nofile', u'value': 65536})
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'nproc', u'value': 2047})
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'nproc', u'value': 16384})
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'stack', u'value': 10240})
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'stack', u'value': 32768})

PLAY RECAP *********************************************************************
default : ok=2 changed=1 unreachable=0 failed=0

ACTUAL RESULTS

Limits are not set at all, apart from first limit in first playbook run.

user1 soft nofile 1024User1

If comment contains "#" itself, behaviour changes to :

user1 hard nofile 65536 ## User1
user1 soft nproc 2047 # ## User1
user1 hard nproc 16384 # # ## User1
user1 soft stack 10240 # # # ## User1
user1 hard stack 32768 # # # # ## User1

PLAY [Deploy] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [default]

TASK [set user limits] *******************************************************
changed: [default] => (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'nofile', u'value': 1024})
failed: [default] (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'nofile', u'value': 65536}) => {"failed": true, "item": {"comment": "User1", "domain": "user1", "name": "nofile", "type": "hard", "value": 65536}, "msg": "Invalid configuration of '/etc/security/limits.conf'. Current value of nofile is unsupported."}
failed: [default] (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'nproc', u'value': 2047}) => {"failed": true, "item": {"comment": "User1", "domain": "user1", "name": "nproc", "type": "soft", "value": 2047}, "msg": "Invalid configuration of '/etc/security/limits.conf'. Current value of nofile is unsupported."}
failed: [default] (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'nproc', u'value': 16384}) => {"failed": true, "item": {"comment": "User1", "domain": "user1", "name": "nproc", "type": "hard", "value": 16384}, "msg": "Invalid configuration of '/etc/security/limits.conf'. Current value of nofile is unsupported."}
failed: [default] (item={u'comment': u'User1', u'domain': u'user1', u'type': u'soft', u'name': u'stack', u'value': 10240}) => {"failed": true, "item": {"comment": "User1", "domain": "user1", "name": "stack", "type": "soft", "value": 10240}, "msg": "Invalid configuration of '/etc/security/limits.conf'. Current value of nofile is unsupported."}
failed: [default] (item={u'comment': u'User1', u'domain': u'user1', u'type': u'hard', u'name': u'stack', u'value': 32768}) => {"failed": true, "item": {"comment": "User1", "domain": "user1", "name": "stack", "type": "hard", "value": 32768}, "msg": "Invalid configuration of '/etc/security/limits.conf'. Current value of nofile is unsupported."}

PLAY RECAP *********************************************************************
default : ok=1 changed=0 unreachable=0 failed=1