2012-12-31 18:17:47 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, David "DaviXX" CHANIAL <david.chanial@gmail.com>
|
2014-01-13 23:53:02 +00:00
|
|
|
# (c) 2014, James Tanner <tanner.jc@gmail.com>
|
2012-12-31 18:17:47 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: sysctl
|
2013-10-14 07:13:00 +00:00
|
|
|
short_description: Manage entries in sysctl.conf.
|
2012-12-31 18:17:47 +00:00
|
|
|
description:
|
2013-10-14 07:13:00 +00:00
|
|
|
- This module manipulates sysctl entries and optionally performs a C(/sbin/sysctl -p) after changing them.
|
2013-02-03 13:01:26 +00:00
|
|
|
version_added: "1.0"
|
2012-12-31 18:17:47 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2013-10-14 07:13:00 +00:00
|
|
|
- The dot-separated path (aka I(key)) specifying the sysctl variable.
|
2012-12-31 18:17:47 +00:00
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: [ 'key' ]
|
|
|
|
value:
|
|
|
|
description:
|
2013-10-14 07:13:00 +00:00
|
|
|
- Desired value of the sysctl key.
|
2012-12-31 18:17:47 +00:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: [ 'val' ]
|
|
|
|
state:
|
|
|
|
description:
|
2014-01-13 23:53:02 +00:00
|
|
|
- Whether the entry should be present or absent in the sysctl file.
|
2012-12-31 18:17:47 +00:00
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
default: present
|
|
|
|
reload:
|
|
|
|
description:
|
2013-10-14 07:13:00 +00:00
|
|
|
- If C(yes), performs a I(/sbin/sysctl -p) if the C(sysctl_file) is
|
|
|
|
updated. If C(no), does not reload I(sysctl) even if the
|
|
|
|
C(sysctl_file) is updated.
|
2013-03-12 12:18:12 +00:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
default: "yes"
|
2012-12-31 18:17:47 +00:00
|
|
|
sysctl_file:
|
|
|
|
description:
|
2013-10-14 07:13:00 +00:00
|
|
|
- Specifies the absolute path to C(sysctl.conf), if not C(/etc/sysctl.conf).
|
2012-12-31 18:17:47 +00:00
|
|
|
required: false
|
|
|
|
default: /etc/sysctl.conf
|
2014-01-13 23:53:02 +00:00
|
|
|
sysctl_set:
|
|
|
|
description:
|
|
|
|
- Verify token value with the sysctl command and set with -w if necessary
|
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
required: false
|
|
|
|
version_added: 1.5
|
|
|
|
default: False
|
2012-12-31 18:17:47 +00:00
|
|
|
notes: []
|
|
|
|
requirements: []
|
|
|
|
author: David "DaviXX" CHANIAL <david.chanial@gmail.com>
|
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Set vm.swappiness to 5 in /etc/sysctl.conf
|
|
|
|
- sysctl: name=vm.swappiness value=5 state=present
|
|
|
|
|
|
|
|
# Remove kernel.panic entry from /etc/sysctl.conf
|
|
|
|
- sysctl: name=kernel.panic state=absent sysctl_file=/etc/sysctl.conf
|
|
|
|
|
2014-01-13 23:53:02 +00:00
|
|
|
# Set kernel.panic to 3 in /tmp/test_sysctl.conf
|
|
|
|
- sysctl: name=kernel.panic value=3 sysctl_file=/tmp/test_sysctl.conf reload=no
|
|
|
|
|
|
|
|
# Set ip fowarding on in /proc and do not reload the sysctl file
|
|
|
|
- sysctl: name="net.ipv4.ip_forward" value=1 sysctl_set=yes
|
|
|
|
|
|
|
|
# Set ip forwarding on in /proc and in the sysctl file and reload if necessary
|
|
|
|
- sysctl: name="net.ipv4.ip_forward" value=1 sysctl_set=yes state=present reload=yes
|
2013-06-14 09:53:43 +00:00
|
|
|
'''
|
|
|
|
|
2012-12-31 18:17:47 +00:00
|
|
|
# ==============================================================
|
|
|
|
|
|
|
|
import os
|
|
|
|
import tempfile
|
2013-02-15 18:14:01 +00:00
|
|
|
import re
|
2012-12-31 18:17:47 +00:00
|
|
|
|
2014-01-13 23:53:02 +00:00
|
|
|
class SysctlModule(object):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.args = self.module.params
|
|
|
|
|
|
|
|
self.sysctl_cmd = self.module.get_bin_path('sysctl', required=True)
|
|
|
|
self.sysctl_file = self.args['sysctl_file']
|
|
|
|
|
|
|
|
self.proc_value = None # current token value in proc fs
|
|
|
|
self.file_value = None # current token value in file
|
|
|
|
self.file_lines = [] # all lines in the file
|
|
|
|
self.file_values = {} # dict of token values
|
|
|
|
|
|
|
|
self.changed = False # will change occur
|
|
|
|
self.set_proc = False # does sysctl need to set value
|
|
|
|
self.write_file = False # does the sysctl file need to be reloaded
|
|
|
|
|
|
|
|
self.process()
|
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# LOGIC
|
|
|
|
# ==============================================================
|
|
|
|
|
|
|
|
def process(self):
|
|
|
|
|
|
|
|
# Whitespace is bad
|
|
|
|
self.args['name'] = self.args['name'].strip()
|
2014-01-27 19:26:11 +00:00
|
|
|
self.args['value'] = self._parse_value(self.args['value'])
|
2014-01-13 23:53:02 +00:00
|
|
|
|
|
|
|
thisname = self.args['name']
|
|
|
|
|
|
|
|
# get the current proc fs value
|
|
|
|
self.proc_value = self.get_token_curr_value(thisname)
|
|
|
|
|
|
|
|
# get the currect sysctl file value
|
|
|
|
self.read_sysctl_file()
|
|
|
|
if thisname not in self.file_values:
|
|
|
|
self.file_values[thisname] = None
|
|
|
|
|
|
|
|
# update file contents with desired token/value
|
|
|
|
self.fix_lines()
|
|
|
|
|
|
|
|
# what do we need to do now?
|
|
|
|
if self.file_values[thisname] is None and self.args['state'] == "present":
|
|
|
|
self.changed = True
|
|
|
|
self.write_file = True
|
|
|
|
elif self.file_values[thisname] != self.args['value']:
|
|
|
|
self.changed = True
|
|
|
|
self.write_file = True
|
|
|
|
if self.args['sysctl_set']:
|
|
|
|
if self.proc_value is None:
|
|
|
|
self.changed = True
|
2014-01-18 10:04:15 +00:00
|
|
|
elif not self._values_is_equal(self.proc_value, self.args['value']):
|
2014-01-13 23:53:02 +00:00
|
|
|
self.changed = True
|
|
|
|
self.set_proc = True
|
|
|
|
|
|
|
|
# Do the work
|
|
|
|
if not self.module.check_mode:
|
|
|
|
if self.write_file:
|
|
|
|
self.write_sysctl()
|
|
|
|
if self.write_file and self.args['reload']:
|
|
|
|
self.reload_sysctl()
|
|
|
|
if self.set_proc:
|
|
|
|
self.set_token_value(self.args['name'], self.args['value'])
|
|
|
|
|
2014-01-18 10:04:15 +00:00
|
|
|
def _values_is_equal(self, a, b):
|
2014-01-18 09:50:24 +00:00
|
|
|
"""Expects two string values. It will split the string by whitespace
|
|
|
|
and compare each value. It will return True if both lists are the same,
|
|
|
|
contain the same elements and the same order."""
|
|
|
|
if a is None or b is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
a = a.split()
|
|
|
|
b = b.split()
|
|
|
|
|
|
|
|
if len(a) != len(b):
|
|
|
|
return False
|
|
|
|
|
2014-01-18 10:04:15 +00:00
|
|
|
return len([i for i, j in zip(a, b) if i == j]) == len(a)
|
2014-01-18 09:50:24 +00:00
|
|
|
|
2014-01-27 19:26:11 +00:00
|
|
|
def _parse_value(self, value):
|
|
|
|
if value is None:
|
|
|
|
return ''
|
|
|
|
elif value.lower() in BOOLEANS_TRUE:
|
|
|
|
return '1'
|
|
|
|
elif value.lower() in BOOLEANS_FALSE:
|
|
|
|
return '0'
|
|
|
|
else:
|
|
|
|
return value.strip()
|
|
|
|
|
2014-01-13 23:53:02 +00:00
|
|
|
# ==============================================================
|
|
|
|
# SYSCTL COMMAND MANAGEMENT
|
|
|
|
# ==============================================================
|
|
|
|
|
|
|
|
# Use the sysctl command to find the current value
|
|
|
|
def get_token_curr_value(self, token):
|
|
|
|
thiscmd = "%s -e -n %s" % (self.sysctl_cmd, token)
|
|
|
|
rc,out,err = self.module.run_command(thiscmd)
|
|
|
|
if rc != 0:
|
|
|
|
return None
|
|
|
|
else:
|
2014-01-17 16:30:07 +00:00
|
|
|
return out
|
2014-01-13 23:53:02 +00:00
|
|
|
|
|
|
|
# Use the sysctl command to set the current value
|
|
|
|
def set_token_value(self, token, value):
|
2014-01-16 21:30:13 +00:00
|
|
|
if len(value.split()) > 0:
|
|
|
|
value = '"' + value + '"'
|
2014-01-13 23:53:02 +00:00
|
|
|
thiscmd = "%s -w %s=%s" % (self.sysctl_cmd, token, value)
|
|
|
|
rc,out,err = self.module.run_command(thiscmd)
|
|
|
|
if rc != 0:
|
2014-01-17 21:31:12 +00:00
|
|
|
self.module.fail_json(msg='setting %s failed: %s' % (token, out + err))
|
2014-01-13 23:53:02 +00:00
|
|
|
else:
|
|
|
|
return rc
|
|
|
|
|
|
|
|
# Run sysctl -p
|
|
|
|
def reload_sysctl(self):
|
|
|
|
# do it
|
|
|
|
if get_platform().lower() == 'freebsd':
|
|
|
|
# freebsd doesn't support -p, so reload the sysctl service
|
|
|
|
rc,out,err = self.module.run_command('/etc/rc.d/sysctl reload')
|
2013-09-11 18:13:32 +00:00
|
|
|
else:
|
2014-01-13 23:53:02 +00:00
|
|
|
# system supports reloading via the -p flag to sysctl, so we'll use that
|
|
|
|
rc,out,err = self.module.run_command([self.sysctl_cmd, '-p', self.sysctl_file])
|
2014-01-14 15:48:57 +00:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(msg="Failed to reload sysctl: %s" % str(out) + str(err))
|
2014-01-13 23:53:02 +00:00
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# SYSCTL FILE MANAGEMENT
|
|
|
|
# ==============================================================
|
|
|
|
|
|
|
|
# Get the token value from the sysctl file
|
|
|
|
def read_sysctl_file(self):
|
|
|
|
lines = open(self.sysctl_file, "r").readlines()
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
self.file_lines.append(line)
|
|
|
|
|
|
|
|
# don't split empty lines or comments
|
|
|
|
if not line or line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
|
|
|
k, v = line.split('=',1)
|
|
|
|
k = k.strip()
|
|
|
|
v = v.strip()
|
|
|
|
self.file_values[k] = v.strip()
|
|
|
|
|
|
|
|
# Fix the value in the sysctl file content
|
|
|
|
def fix_lines(self):
|
|
|
|
checked = []
|
|
|
|
self.fixed_lines = []
|
|
|
|
for line in self.file_lines:
|
|
|
|
if not line.strip() or line.strip().startswith("#"):
|
|
|
|
self.fixed_lines.append(line)
|
|
|
|
continue
|
|
|
|
tmpline = line.strip()
|
|
|
|
k, v = line.split('=',1)
|
|
|
|
k = k.strip()
|
|
|
|
v = v.strip()
|
|
|
|
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)
|
|
|
|
|
|
|
|
if self.args['name'] not in checked and self.args['state'] == "present":
|
|
|
|
new_line = "%s = %s\n" % (self.args['name'], self.args['value'])
|
|
|
|
self.fixed_lines.append(new_line)
|
|
|
|
|
|
|
|
# Completely rewrite the sysctl file
|
|
|
|
def write_sysctl(self):
|
|
|
|
# open a tmp file
|
|
|
|
fd, tmp_path = tempfile.mkstemp('.conf', '.ansible_m_sysctl_', os.path.dirname(self.sysctl_file))
|
|
|
|
f = open(tmp_path,"w")
|
|
|
|
try:
|
|
|
|
for l in self.fixed_lines:
|
2014-01-15 15:25:27 +00:00
|
|
|
f.write(l.strip() + "\n")
|
2014-01-13 23:53:02 +00:00
|
|
|
except IOError, e:
|
|
|
|
self.module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
|
|
|
f.flush()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# replace the real one
|
|
|
|
self.module.atomic_move(tmp_path, self.sysctl_file)
|
2013-09-11 18:13:32 +00:00
|
|
|
|
2012-12-31 18:17:47 +00:00
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
# defining module
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(aliases=['key'], required=True),
|
|
|
|
value = dict(aliases=['val'], required=False),
|
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
2013-02-23 21:56:45 +00:00
|
|
|
reload = dict(default=True, type='bool'),
|
2014-01-16 21:30:13 +00:00
|
|
|
sysctl_set = dict(default=False, type='bool'),
|
2012-12-31 18:17:47 +00:00
|
|
|
sysctl_file = dict(default='/etc/sysctl.conf')
|
2014-01-13 23:53:02 +00:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
2012-12-31 18:17:47 +00:00
|
|
|
)
|
|
|
|
|
2014-01-13 23:53:02 +00:00
|
|
|
result = SysctlModule(module)
|
2012-12-31 18:17:47 +00:00
|
|
|
|
2014-01-13 23:53:02 +00:00
|
|
|
module.exit_json(changed=result.changed)
|
2012-12-31 18:17:47 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2013-12-02 20:13:49 +00:00
|
|
|
# import module snippets
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2012-12-31 18:17:47 +00:00
|
|
|
main()
|