2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2014, Sebastien Rohaut <sebastien.rohaut@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
DOCUMENTATION = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
---
|
|
|
|
module: pam_limits
|
|
|
|
author:
|
2021-01-01 11:45:02 +01:00
|
|
|
- "Sebastien Rohaut (@usawa)"
|
2020-03-09 10:11:07 +01:00
|
|
|
short_description: Modify Linux PAM limits
|
|
|
|
description:
|
2023-06-15 19:05:01 +02:00
|
|
|
- The M(community.general.pam_limits) module modifies PAM limits.
|
|
|
|
- The default file is V(/etc/security/limits.conf).
|
2021-01-01 11:45:02 +01:00
|
|
|
- For the full documentation, see C(man 5 limits.conf).
|
2023-02-20 17:28:47 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
version_added: 2.0.0
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
|
|
|
version_added: 2.0.0
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
domain:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-01-01 11:45:02 +01:00
|
|
|
- A username, @groupname, wildcard, UID/GID range.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
limit_type:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-01-01 11:45:02 +01:00
|
|
|
- Limit type, see C(man 5 limits.conf) for an explanation.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
choices: [ "hard", "soft", "-" ]
|
|
|
|
limit_item:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-01-01 11:45:02 +01:00
|
|
|
- The limit to be set.
|
2020-03-09 10:11:07 +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:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The value of the limit.
|
2023-06-15 19:05:01 +02:00
|
|
|
- Value must either be V(unlimited), V(infinity) or V(-1), all of which indicate no limit, or a limit of 0 or larger.
|
|
|
|
- Value must be a number in the range -20 to 19 inclusive, if O(limit_item) is set to V(nice) or V(priority).
|
2021-01-01 11:45:02 +01:00
|
|
|
- Refer to the C(man 5 limits.conf) manual pages for more details.
|
2020-03-09 10:11:07 +01:00
|
|
|
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
|
|
|
|
type: bool
|
2022-08-24 19:59:01 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
use_min:
|
|
|
|
description:
|
2023-06-15 19:05:01 +02:00
|
|
|
- If set to V(true), the minimal value will be used or conserved.
|
2021-01-01 11:45:02 +01:00
|
|
|
- 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.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
2022-08-24 19:59:01 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
use_max:
|
|
|
|
description:
|
2023-06-15 19:05:01 +02:00
|
|
|
- If set to V(true), the maximal value will be used or conserved.
|
2021-01-01 11:45:02 +01:00
|
|
|
- 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.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
type: bool
|
2022-08-24 19:59:01 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
dest:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Modify the limits.conf path.
|
|
|
|
required: false
|
|
|
|
default: "/etc/security/limits.conf"
|
|
|
|
comment:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Comment associated with the limit.
|
|
|
|
required: false
|
|
|
|
default: ''
|
|
|
|
notes:
|
2023-06-15 19:05:01 +02:00
|
|
|
- If O(dest) file does not exist, it is created.
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
EXAMPLES = r'''
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: Add or modify nofile soft limit for the user joe
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pam_limits:
|
2020-03-09 10:11:07 +01:00
|
|
|
domain: joe
|
|
|
|
limit_type: soft
|
|
|
|
limit_item: nofile
|
|
|
|
value: 64000
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
- name: Add or modify fsize hard limit for the user smith. Keep or set the maximal value
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pam_limits:
|
2020-03-09 10:11:07 +01:00
|
|
|
domain: smith
|
|
|
|
limit_type: hard
|
|
|
|
limit_item: fsize
|
|
|
|
value: 1000000
|
2022-08-24 19:59:01 +02:00
|
|
|
use_max: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
- name: Add or modify memlock, both soft and hard, limit for the user james with a comment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pam_limits:
|
2020-03-09 10:11:07 +01:00
|
|
|
domain: james
|
|
|
|
limit_type: '-'
|
|
|
|
limit_item: memlock
|
|
|
|
value: unlimited
|
|
|
|
comment: unlimited memory lock for james
|
|
|
|
|
|
|
|
- name: Add or modify hard nofile limits for wildcard domain
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.pam_limits:
|
2020-03-09 10:11:07 +01:00
|
|
|
domain: '*'
|
|
|
|
limit_type: hard
|
|
|
|
limit_item: nofile
|
|
|
|
value: 39693561
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2021-01-03 13:32:54 +01:00
|
|
|
import tempfile
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
def _assert_is_valid_value(module, item, value, prefix=''):
|
|
|
|
if item in ['nice', 'priority']:
|
|
|
|
try:
|
|
|
|
valid = -20 <= int(value) <= 19
|
|
|
|
except ValueError:
|
|
|
|
valid = False
|
|
|
|
if not valid:
|
|
|
|
module.fail_json(msg="%s Value of %r for item %r is invalid. Value must be a number in the range -20 to 19 inclusive. "
|
|
|
|
"Refer to the limits.conf(5) manual pages for more details." % (prefix, value, item))
|
|
|
|
elif not (value in ['unlimited', 'infinity', '-1'] or value.isdigit()):
|
|
|
|
module.fail_json(msg="%s Value of %r for item %r is invalid. Value must either be 'unlimited', 'infinity' or -1, all of "
|
|
|
|
"which indicate no limit, or a limit of 0 or larger. Refer to the limits.conf(5) manual pages for "
|
|
|
|
"more details." % (prefix, value, item))
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +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']
|
|
|
|
|
|
|
|
pam_types = ['soft', 'hard', '-']
|
|
|
|
|
|
|
|
limits_conf = '/etc/security/limits.conf'
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
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),
|
|
|
|
value=dict(required=True, type='str'),
|
|
|
|
use_max=dict(default=False, type='bool'),
|
|
|
|
use_min=dict(default=False, type='bool'),
|
|
|
|
backup=dict(default=False, type='bool'),
|
|
|
|
dest=dict(default=limits_conf, type='str'),
|
|
|
|
comment=dict(required=False, default='', type='str')
|
2021-01-03 11:11:06 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
2020-03-09 10:11:07 +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']
|
|
|
|
new_comment = module.params['comment']
|
|
|
|
|
|
|
|
changed = False
|
2024-03-09 13:42:09 +01:00
|
|
|
does_not_exist = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
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:
|
|
|
|
limits_conf_dir = os.path.dirname(limits_conf)
|
|
|
|
if os.path.isdir(limits_conf_dir) and os.access(limits_conf_dir, os.W_OK):
|
2024-03-09 13:42:09 +01:00
|
|
|
does_not_exist = True
|
2020-03-09 10:11:07 +01:00
|
|
|
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.")
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
_assert_is_valid_value(module, limit_item, value)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Backup
|
|
|
|
if backup:
|
|
|
|
backup_file = module.backup_local(limits_conf)
|
|
|
|
|
|
|
|
space_pattern = re.compile(r'\s+')
|
|
|
|
|
2024-03-09 13:42:09 +01:00
|
|
|
if does_not_exist:
|
|
|
|
lines = []
|
|
|
|
else:
|
|
|
|
with open(limits_conf, 'rb') as f:
|
|
|
|
lines = list(f)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
message = ''
|
|
|
|
# Tempfile
|
|
|
|
nf = tempfile.NamedTemporaryFile(mode='w+')
|
|
|
|
|
|
|
|
found = False
|
|
|
|
new_value = value
|
|
|
|
|
2024-03-09 13:42:09 +01:00
|
|
|
for line in lines:
|
2020-03-09 10:11:07 +01:00
|
|
|
line = to_native(line, errors='surrogate_or_strict')
|
|
|
|
if line.startswith('#'):
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
newline = re.sub(space_pattern, ' ', line).strip()
|
|
|
|
if not newline:
|
|
|
|
nf.write(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Remove comment in line
|
|
|
|
newline = newline.split('#', 1)[0]
|
|
|
|
try:
|
|
|
|
old_comment = line.split('#', 1)[1]
|
|
|
|
except Exception:
|
|
|
|
old_comment = ''
|
|
|
|
|
|
|
|
newline = newline.rstrip()
|
|
|
|
|
|
|
|
if not new_comment:
|
|
|
|
new_comment = old_comment
|
|
|
|
|
|
|
|
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]
|
|
|
|
actual_value = line_fields[3]
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
_assert_is_valid_value(module, line_item, actual_value,
|
|
|
|
prefix="Invalid configuration found in '%s'." % limits_conf)
|
2020-03-09 10:11:07 +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
|
|
|
|
|
2021-01-01 11:45:02 +01:00
|
|
|
if line_type not in ['nice', 'priority']:
|
|
|
|
actual_value_unlimited = actual_value in ['unlimited', 'infinity', '-1']
|
|
|
|
value_unlimited = value in ['unlimited', 'infinity', '-1']
|
|
|
|
else:
|
|
|
|
actual_value_unlimited = value_unlimited = False
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if use_max:
|
2021-01-01 11:45:02 +01:00
|
|
|
if actual_value_unlimited:
|
2020-03-09 10:11:07 +01:00
|
|
|
new_value = actual_value
|
2021-01-01 11:45:02 +01:00
|
|
|
elif value_unlimited:
|
2020-03-09 10:11:07 +01:00
|
|
|
new_value = value
|
2021-01-01 11:45:02 +01:00
|
|
|
else:
|
|
|
|
new_value = str(max(int(value), int(actual_value)))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if use_min:
|
2021-01-01 11:45:02 +01:00
|
|
|
if actual_value_unlimited and value_unlimited:
|
|
|
|
new_value = actual_value
|
|
|
|
elif actual_value_unlimited:
|
|
|
|
new_value = value
|
2020-03-09 10:11:07 +01:00
|
|
|
elif value_unlimited:
|
|
|
|
new_value = actual_value
|
|
|
|
else:
|
2021-01-01 11:45:02 +01:00
|
|
|
new_value = str(min(int(value), int(actual_value)))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# Change line only if value has changed
|
|
|
|
if new_value != actual_value:
|
|
|
|
changed = True
|
|
|
|
if new_comment:
|
|
|
|
new_comment = "\t#" + new_comment
|
|
|
|
new_limit = domain + "\t" + limit_type + "\t" + limit_item + "\t" + new_value + new_comment + "\n"
|
|
|
|
message = new_limit
|
|
|
|
nf.write(new_limit)
|
|
|
|
else:
|
|
|
|
message = line
|
|
|
|
nf.write(line)
|
|
|
|
else:
|
|
|
|
nf.write(line)
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
changed = True
|
|
|
|
if new_comment:
|
|
|
|
new_comment = "\t#" + new_comment
|
|
|
|
new_limit = domain + "\t" + limit_type + "\t" + limit_item + "\t" + new_value + new_comment + "\n"
|
|
|
|
message = new_limit
|
|
|
|
nf.write(new_limit)
|
|
|
|
|
|
|
|
nf.flush()
|
|
|
|
|
2021-01-03 11:11:06 +01:00
|
|
|
with open(nf.name, 'r') as content:
|
|
|
|
content_new = content.read()
|
|
|
|
|
|
|
|
if not module.check_mode:
|
2024-03-09 13:42:09 +01:00
|
|
|
if does_not_exist:
|
|
|
|
with open(limits_conf, 'a'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Move tempfile to newfile
|
2021-01-03 11:11:06 +01:00
|
|
|
module.atomic_move(nf.name, limits_conf)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
nf.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
res_args = dict(
|
2021-01-03 11:11:06 +01:00
|
|
|
changed=changed,
|
|
|
|
msg=message,
|
2024-03-09 13:42:09 +01:00
|
|
|
diff=dict(before=b''.join(lines), after=content_new),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if backup:
|
|
|
|
res_args['backup_file'] = backup_file
|
|
|
|
|
|
|
|
module.exit_json(**res_args)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|