2015-06-21 18:52:59 +02:00
|
|
|
#!/usr/bin/python
|
2014-10-30 11:03:44 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2014, Sebastien Rohaut <sebastien.rohaut@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import re
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: pam_limits
|
2015-06-21 18:52:59 +02:00
|
|
|
version_added: "2.0"
|
2016-05-20 15:19:10 +02:00
|
|
|
authors:
|
|
|
|
- "Sebastien Rohaut (@usawa)"
|
2014-10-30 11:03:44 +01:00
|
|
|
short_description: Modify Linux PAM limits
|
|
|
|
description:
|
|
|
|
- The M(pam_limits) module modify PAM limits, default in /etc/security/limits.conf.
|
|
|
|
For the full documentation, see man limits.conf(5).
|
|
|
|
options:
|
|
|
|
domain:
|
|
|
|
description:
|
|
|
|
- A username, @groupname, wildcard, uid/gid range.
|
|
|
|
required: true
|
|
|
|
limit_type:
|
|
|
|
description:
|
2015-07-28 14:50:23 +02:00
|
|
|
- Limit type, see C(man limits) for an explanation
|
2014-10-30 11:03:44 +01:00
|
|
|
required: true
|
2015-11-26 11:37:59 +01:00
|
|
|
choices: [ "hard", "soft", "-" ]
|
2014-10-30 11:03:44 +01:00
|
|
|
limit_item:
|
|
|
|
description:
|
2015-07-28 14:50:23 +02:00
|
|
|
- The limit to be set
|
2014-10-30 11:03:44 +01:00
|
|
|
required: true
|
|
|
|
choices: [ "core", "data", "fsize", "memlock", "nofile", "rss", "stack", "cpu", "nproc", "as", "maxlogins", "maxsyslogins", "priority", "locks", "sigpending", "msgqueue", "nice", "rtprio", "chroot" ]
|
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- The value of the limit.
|
|
|
|
required: true
|
|
|
|
backup:
|
|
|
|
description:
|
|
|
|
- Create a backup file including the timestamp information so you can get
|
|
|
|
the original file back if you somehow clobbered it incorrectly.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
use_min:
|
|
|
|
description:
|
|
|
|
- If set to C(yes), the minimal value will be used or conserved.
|
|
|
|
If the specified value is inferior to the value in the file, file content is replaced with the new value,
|
|
|
|
else content is not modified.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
use_max:
|
|
|
|
description:
|
|
|
|
- If set to C(yes), the maximal value will be used or conserved.
|
|
|
|
If the specified value is superior to the value in the file, file content is replaced with the new value,
|
|
|
|
else content is not modified.
|
|
|
|
required: false
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "no"
|
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Modify the limits.conf path.
|
|
|
|
required: false
|
|
|
|
default: "/etc/security/limits.conf"
|
2015-12-01 14:22:25 +01:00
|
|
|
comment:
|
|
|
|
description:
|
|
|
|
- Comment associated with the limit.
|
|
|
|
required: false
|
|
|
|
default: ''
|
2014-10-30 11:03:44 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-12-01 14:22:25 +01:00
|
|
|
# Add or modify nofile soft limit for the user joe
|
2014-10-30 11:03:44 +01:00
|
|
|
- pam_limits: domain=joe limit_type=soft limit_item=nofile value=64000
|
|
|
|
|
2015-12-01 14:22:25 +01:00
|
|
|
# Add or modify fsize hard limit for the user smith. Keep or set the maximal value.
|
|
|
|
- pam_limits: domain=smith limit_type=hard limit_item=fsize value=1000000 use_max=yes
|
|
|
|
|
|
|
|
# Add or modify memlock, both soft and hard, limit for the user james with a comment.
|
|
|
|
- pam_limits: domain=james limit_type=- limit_item=memlock value=unlimited comment="unlimited memory lock for james"
|
2014-10-30 11:03:44 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
pam_items = [ 'core', 'data', 'fsize', 'memlock', 'nofile', 'rss', 'stack', 'cpu', 'nproc', 'as', 'maxlogins', 'maxsyslogins', 'priority', 'locks', 'sigpending', 'msgqueue', 'nice', 'rtprio', 'chroot' ]
|
|
|
|
|
2015-01-23 20:07:27 +01:00
|
|
|
pam_types = [ 'soft', 'hard', '-' ]
|
2014-10-30 11:03:44 +01:00
|
|
|
|
2014-10-30 11:13:17 +01:00
|
|
|
limits_conf = '/etc/security/limits.conf'
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
# not checking because of daisy chain to file module
|
|
|
|
argument_spec = dict(
|
|
|
|
domain = dict(required=True, type='str'),
|
|
|
|
limit_type = dict(required=True, type='str', choices=pam_types),
|
|
|
|
limit_item = dict(required=True, type='str', choices=pam_items),
|
2015-10-04 09:54:25 +02:00
|
|
|
value = dict(required=True, type='str'),
|
2014-10-30 11:03:44 +01:00
|
|
|
use_max = dict(default=False, type='bool'),
|
|
|
|
use_min = dict(default=False, type='bool'),
|
|
|
|
backup = dict(default=False, type='bool'),
|
2014-12-24 15:36:54 +01:00
|
|
|
dest = dict(default=limits_conf, type='str'),
|
|
|
|
comment = dict(required=False, default='', type='str')
|
2014-10-30 11:03:44 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
domain = module.params['domain']
|
|
|
|
limit_type = module.params['limit_type']
|
|
|
|
limit_item = module.params['limit_item']
|
|
|
|
value = module.params['value']
|
|
|
|
use_max = module.params['use_max']
|
|
|
|
use_min = module.params['use_min']
|
|
|
|
backup = module.params['backup']
|
|
|
|
limits_conf = module.params['dest']
|
2014-12-24 15:36:54 +01:00
|
|
|
new_comment = module.params['comment']
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
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) )
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="%s is not visible (check presence, access rights, use sudo)" % (limits_conf) )
|
|
|
|
|
2014-12-24 15:36:54 +01:00
|
|
|
if use_max and use_min:
|
|
|
|
module.fail_json(msg="Cannot use use_min and use_max at the same time." )
|
|
|
|
|
2015-10-04 09:54:25 +02:00
|
|
|
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.")
|
|
|
|
|
2014-10-30 11:03:44 +01:00
|
|
|
# Backup
|
|
|
|
if backup:
|
|
|
|
backup_file = module.backup_local(limits_conf)
|
|
|
|
|
|
|
|
space_pattern = re.compile(r'\s+')
|
|
|
|
|
|
|
|
message = ''
|
2014-10-30 11:07:42 +01:00
|
|
|
f = open (limits_conf, 'r')
|
2014-10-30 11:03:44 +01:00
|
|
|
# Tempfile
|
|
|
|
nf = tempfile.NamedTemporaryFile(delete = False)
|
|
|
|
|
|
|
|
found = False
|
2014-10-30 11:07:42 +01:00
|
|
|
new_value = value
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
for line in f:
|
2014-12-24 15:36:54 +01:00
|
|
|
|
2014-10-30 11:03:44 +01:00
|
|
|
if line.startswith('#'):
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
newline = re.sub(space_pattern, ' ', line).strip()
|
|
|
|
if not newline:
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
2014-12-24 15:36:54 +01:00
|
|
|
# Remove comment in line
|
|
|
|
newline = newline.split('#',1)[0]
|
|
|
|
try:
|
|
|
|
old_comment = line.split('#',1)[1]
|
|
|
|
except:
|
|
|
|
old_comment = ''
|
|
|
|
|
|
|
|
newline = newline.rstrip()
|
|
|
|
|
|
|
|
if not new_comment:
|
|
|
|
new_comment = old_comment
|
|
|
|
|
|
|
|
if new_comment:
|
|
|
|
new_comment = "\t#"+new_comment
|
|
|
|
|
2014-10-30 11:03:44 +01:00
|
|
|
line_fields = newline.split(' ')
|
|
|
|
|
|
|
|
if len(line_fields) != 4:
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
line_domain = line_fields[0]
|
|
|
|
line_type = line_fields[1]
|
|
|
|
line_item = line_fields[2]
|
2015-10-04 09:54:25 +02:00
|
|
|
actual_value = line_fields[3]
|
|
|
|
|
|
|
|
if not (actual_value in ['unlimited', 'infinity', '-1'] or actual_value.isdigit()):
|
|
|
|
module.fail_json(msg="Invalid configuration of '%s'. Current value of %s is unsupported." % (limits_conf, line_item))
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
# Found the line
|
|
|
|
if line_domain == domain and line_type == limit_type and line_item == limit_item:
|
|
|
|
found = True
|
|
|
|
if value == actual_value:
|
|
|
|
message = line
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
2015-10-04 09:54:25 +02:00
|
|
|
actual_value_unlimited = actual_value in ['unlimited', 'infinity', '-1']
|
|
|
|
value_unlimited = value in ['unlimited', 'infinity', '-1']
|
|
|
|
|
2014-10-30 11:03:44 +01:00
|
|
|
if use_max:
|
2015-10-04 09:54:25 +02:00
|
|
|
if value.isdigit() and actual_value.isdigit():
|
2016-09-17 08:26:52 +02:00
|
|
|
new_value = str(max(int(value), int(actual_value)))
|
2015-10-04 09:54:25 +02:00
|
|
|
elif actual_value_unlimited:
|
|
|
|
new_value = actual_value
|
|
|
|
else:
|
|
|
|
new_value = value
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
if use_min:
|
2015-10-04 09:54:25 +02:00
|
|
|
if value.isdigit() and actual_value.isdigit():
|
2016-09-17 08:26:52 +02:00
|
|
|
new_value = str(min(int(value), int(actual_value)))
|
2015-10-04 09:54:25 +02:00
|
|
|
elif value_unlimited:
|
|
|
|
new_value = actual_value
|
|
|
|
else:
|
|
|
|
new_value = value
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
# Change line only if value has changed
|
|
|
|
if new_value != actual_value:
|
|
|
|
changed = True
|
2016-09-17 08:26:52 +02:00
|
|
|
new_limit = domain + "\t" + limit_type + "\t" + limit_item + "\t" + new_value + new_comment + "\n"
|
2014-10-30 11:03:44 +01:00
|
|
|
message = new_limit
|
|
|
|
nf.write(new_limit)
|
|
|
|
else:
|
|
|
|
message = line
|
|
|
|
nf.write(line)
|
|
|
|
else:
|
|
|
|
nf.write(line)
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
changed = True
|
2016-09-17 08:26:52 +02:00
|
|
|
new_limit = domain + "\t" + limit_type + "\t" + limit_item + "\t" + new_value + new_comment + "\n"
|
2014-10-30 11:03:44 +01:00
|
|
|
message = new_limit
|
|
|
|
nf.write(new_limit)
|
|
|
|
|
|
|
|
f.close()
|
2016-01-22 11:05:48 +01:00
|
|
|
nf.flush()
|
2014-10-30 11:03:44 +01:00
|
|
|
|
|
|
|
# Copy tempfile to newfile
|
2015-06-21 18:52:59 +02:00
|
|
|
module.atomic_move(nf.name, f.name)
|
2014-10-30 11:03:44 +01:00
|
|
|
|
2016-01-22 11:05:48 +01:00
|
|
|
try:
|
|
|
|
nf.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2014-10-30 11:03:44 +01:00
|
|
|
res_args = dict(
|
|
|
|
changed = changed, msg = message
|
|
|
|
)
|
|
|
|
|
|
|
|
if backup:
|
|
|
|
res_args['backup_file'] = backup_file
|
|
|
|
|
|
|
|
module.exit_json(**res_args)
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
main()
|