mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-11 23:25:28 +01:00
system_wide in defining module
This commit is contained in:
parent
d70d2aaaa7
commit
505a4aaa09
1 changed files with 21 additions and 8 deletions
|
|
@ -386,15 +386,27 @@ class SysctlModule(object):
|
||||||
# Completely rewrite the sysctl file
|
# Completely rewrite the sysctl file
|
||||||
def write_sysctl(self):
|
def write_sysctl(self):
|
||||||
# open a tmp file
|
# open a tmp file
|
||||||
fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(os.path.realpath(self.sysctl_file)))
|
if self.system_Wide:
|
||||||
f = open(tmp_path, "w")
|
sysctl_files_dir = '/etc/sysctl.d/'
|
||||||
|
fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', sysctl_files_dir)
|
||||||
|
os.close(fd=fd)
|
||||||
|
else:
|
||||||
|
fd, tmp_path = tempfile.mkstemp(dir=os.path.dirname(self.sysctl_file))
|
||||||
|
os.close(fd)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for l in self.fixed_lines:
|
with open(tmp_path, 'w') as write_file:
|
||||||
f.write(l.strip() + "\n")
|
for line in self.fixed_lines:
|
||||||
|
write_file.write("%s\n" % line)
|
||||||
|
os.rename(tmp_path, self.sysctl_file)
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e)))
|
self.module.fail_json(msg="Failed to write %s: %s" % (to_native(tmp_path), to_native(e)))
|
||||||
f.flush()
|
finally:
|
||||||
f.close()
|
try:
|
||||||
|
os.remove(tmp_path)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# replace the real one
|
# replace the real one
|
||||||
self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file))
|
self.module.atomic_move(tmp_path, os.path.realpath(self.sysctl_file))
|
||||||
|
|
@ -414,7 +426,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'),
|
||||||
|
system_wide=dict(default=False, type='bool'), # system_wide parameter
|
||||||
),
|
),
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
required_if=[('state', 'present', ['value'])],
|
required_if=[('state', 'present', ['value'])],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue