mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-11 15:15:26 +01:00
Add allow_duplicate option
When this option is specified with state:present, it allows duplicate parameters in the generated sysctl.conf file. * Addresses AAP-59588 Signed-off-by: Hideki Saito <saito@fgrep.org>
This commit is contained in:
parent
5f44339fa5
commit
08eda6a8d9
1 changed files with 29 additions and 8 deletions
|
|
@ -56,6 +56,12 @@ options:
|
||||||
- Verify token value with the sysctl command and set with C(-w) if necessary.
|
- Verify token value with the sysctl command and set with C(-w) if necessary.
|
||||||
type: bool
|
type: bool
|
||||||
default: false
|
default: false
|
||||||
|
allow_duplicate:
|
||||||
|
description:
|
||||||
|
- If C(true), allows duplicate entries in the sysctl file.
|
||||||
|
If C(false), it behaves the same as in versions prior to 2.1.0.
|
||||||
|
type: bool
|
||||||
|
default: false
|
||||||
author:
|
author:
|
||||||
- David CHANIAL (@davixx)
|
- David CHANIAL (@davixx)
|
||||||
'''
|
'''
|
||||||
|
|
@ -165,7 +171,11 @@ class SysctlModule(object):
|
||||||
self.fix_lines()
|
self.fix_lines()
|
||||||
|
|
||||||
# what do we need to do now?
|
# what do we need to do now?
|
||||||
if self.file_values[thisname] is None and self.args['state'] == "present":
|
if self.args['allow_duplicate'] and self.args['state'] == "present":
|
||||||
|
# with allow_duplicate we always add the new value
|
||||||
|
self.changed = True
|
||||||
|
self.write_file = True
|
||||||
|
elif self.file_values[thisname] is None and self.args['state'] == "present":
|
||||||
self.changed = True
|
self.changed = True
|
||||||
self.write_file = True
|
self.write_file = True
|
||||||
elif self.file_values[thisname] is None and self.args['state'] == "absent":
|
elif self.file_values[thisname] is None and self.args['state'] == "absent":
|
||||||
|
|
@ -348,6 +358,7 @@ class SysctlModule(object):
|
||||||
def fix_lines(self):
|
def fix_lines(self):
|
||||||
checked = []
|
checked = []
|
||||||
self.fixed_lines = []
|
self.fixed_lines = []
|
||||||
|
|
||||||
for line in self.file_lines:
|
for line in self.file_lines:
|
||||||
if not line.strip() or line.strip().startswith(("#", ";")) or "=" not in line:
|
if not line.strip() or line.strip().startswith(("#", ";")) or "=" not in line:
|
||||||
self.fixed_lines.append(line)
|
self.fixed_lines.append(line)
|
||||||
|
|
@ -356,16 +367,25 @@ class SysctlModule(object):
|
||||||
k, v = tmpline.split('=', 1)
|
k, v = tmpline.split('=', 1)
|
||||||
k = k.strip()
|
k = k.strip()
|
||||||
v = v.strip()
|
v = v.strip()
|
||||||
if k not in checked:
|
|
||||||
checked.append(k)
|
if self.args['allow_duplicate'] and self.args['state'] == "present":
|
||||||
if k == self.args['name']:
|
new_line = "%s=%s\n" % (k, v)
|
||||||
if self.args['state'] == "present":
|
self.fixed_lines.append(new_line)
|
||||||
new_line = "%s=%s\n" % (k, self.args['value'])
|
if k == self.args['name'] and v == self.args['value']:
|
||||||
|
checked.append(k)
|
||||||
|
else:
|
||||||
|
if k not in checked:
|
||||||
|
checked.append(k)
|
||||||
|
if k == self.args['name']:
|
||||||
|
if self.args['state'] == "present":
|
||||||
|
new_line = "%s=%s\n" % (k, self.args['value'])
|
||||||
|
self.fixed_lines.append(new_line)
|
||||||
|
else:
|
||||||
|
new_line = "%s=%s\n" % (k, v)
|
||||||
self.fixed_lines.append(new_line)
|
self.fixed_lines.append(new_line)
|
||||||
else:
|
else:
|
||||||
new_line = "%s=%s\n" % (k, v)
|
new_line = "%s=%s\n" % (k, v)
|
||||||
self.fixed_lines.append(new_line)
|
self.fixed_lines.append(new_line)
|
||||||
|
|
||||||
if self.args['name'] not in checked and self.args['state'] == "present":
|
if self.args['name'] not in checked and self.args['state'] == "present":
|
||||||
new_line = "%s=%s\n" % (self.args['name'], self.args['value'])
|
new_line = "%s=%s\n" % (self.args['name'], self.args['value'])
|
||||||
self.fixed_lines.append(new_line)
|
self.fixed_lines.append(new_line)
|
||||||
|
|
@ -401,7 +421,8 @@ def main():
|
||||||
reload=dict(default=True, type='bool'),
|
reload=dict(default=True, type='bool'),
|
||||||
sysctl_set=dict(default=False, type='bool'),
|
sysctl_set=dict(default=False, type='bool'),
|
||||||
ignoreerrors=dict(default=False, type='bool'),
|
ignoreerrors=dict(default=False, type='bool'),
|
||||||
sysctl_file=dict(default='/etc/sysctl.conf', type='path')
|
sysctl_file=dict(default='/etc/sysctl.conf', type='path'),
|
||||||
|
allow_duplicate=dict(default=False, type='bool',)
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
required_if=[('state', 'present', ['value'])],
|
required_if=[('state', 'present', ['value'])],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue