Repository: ansible/ansible

Issue:
pam_limits can not create custom file (/etc/security/limits.d/cdb.conf)
<!---
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: -->

 - Feature Idea


##### COMPONENT NAME
Pam_limits
##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Aug  2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
RHEL  OS

-->

##### SUMMARY
<!--- Explain the problem briefly -->
CUSTOM pam module file under  /etc/security/limits.d/
when destination path is mentioned and it is not available on destination. it gives error message.
there should be an option to create, if doesnt exisit on the destination


<!---
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 -->
```- name:  Resource | Limits
  pam_limits:
    domain: "{{ item.domain }}"
    limit_type: "{{ item.limit_type }}"
    limit_item: "{{ item.limit_item }}"
    value: "{{ item.value }}"
    dest: /etc/security/limits.d/cdb.conf
  with_items: "{{ cas_resource }}"
  ```

<!--- 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 file should be created with the given values.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```

failed: [10.8.67.37] (item={u'domain': u'cdb', u'limit_item': u'memlock', u'limit_type': u'-', u'value': u'unlimited'}) => {
    "failed": true,
    "invocation": {
        "module_args": {
            "backup": false,
            "comment": "",
            "dest": "/etc/security/limits.d/cdb.conf",
            "domain": "cdb",
            "limit_item": "memlock",
            "limit_type": "-",
            "use_max": false,
            "use_min": false,
            "value": "unlimited"
        }
    },
    "item": {
        "domain": "cdb",
        "limit_item": "memlock",
        "limit_type": "-",
        "value": "unlimited"
    },
    "msg": "/etc/security/limits.d/cdb.conf is not visible (check presence, access rights, use sudo)"
}

```



Discussion:
cc @giovannisciortino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
nice request, I will work on this

Resolving patch:
diff --git a/lib/ansible/modules/system/pam_limits.py b/lib/ansible/modules/system/pam_limits.py
--- a/lib/ansible/modules/system/pam_limits.py
+++ b/lib/ansible/modules/system/pam_limits.py
@@ -94,6 +94,8 @@
       - Comment associated with the limit.
     required: false
     default: ''
+notes:
+  - If dest file doesn't exists, it is created.
 '''
 
 EXAMPLES = '''
@@ -168,12 +170,17 @@ def main():
 
     if os.path.isfile(limits_conf):
         if not os.access(limits_conf, os.W_OK):
-            module.fail_json(msg="%s is not writable. Use sudo" % (limits_conf) )
+            module.fail_json(msg="%s is not writable. Use sudo" % limits_conf)
     else:
-        module.fail_json(msg="%s is not visible (check presence, access rights, use sudo)" % (limits_conf) )
+        limits_conf_dir = os.path.dirname(limits_conf)
+        if os.path.isdir(limits_conf_dir) and os.access(limits_conf_dir, os.W_OK):
+            open(limits_conf, 'a').close()
+            changed = True
+        else:
+            module.fail_json(msg="directory %s is not writable (check presence, access rights, use sudo)" % limits_conf_dir)
 
     if use_max and use_min:
-        module.fail_json(msg="Cannot use use_min and use_max at the same time." )
+        module.fail_json(msg="Cannot use use_min and use_max at the same time.")
 
     if not (value in ['unlimited', 'infinity', '-1'] or value.isdigit()):
         module.fail_json(msg="Argument 'value' can be one of 'unlimited', 'infinity', '-1' or positive number. Refer to manual pages for more details.")


Test patch: