2012-09-26 13:18:20 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
2015-10-29 06:04:32 +01:00
|
|
|
# (c) 2015, Ales Nosek <anosek.nosek () gmail.com>
|
2012-09-26 13:18:20 +02: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: ini_file
|
|
|
|
short_description: Tweak settings in INI files
|
2014-10-02 03:30:25 +02:00
|
|
|
extends_documentation_fragment: files
|
2012-09-26 13:18:20 +02:00
|
|
|
description:
|
|
|
|
- Manage (add, remove, change) individual settings in an INI-style file without having
|
|
|
|
to manage the file as a whole with, say, M(template) or M(assemble). Adds missing
|
|
|
|
sections if they don't exist.
|
2015-11-07 14:23:35 +01:00
|
|
|
- Before version 2.0, comments are discarded when the source file is read, and therefore will not show up in the destination file.
|
2012-09-26 13:18:20 +02:00
|
|
|
version_added: "0.9"
|
|
|
|
options:
|
2012-09-28 03:06:31 +02:00
|
|
|
dest:
|
|
|
|
description:
|
|
|
|
- Path to the INI-style file; this file is created if required
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
section:
|
|
|
|
description:
|
|
|
|
- Section name in INI file. This is added if C(state=present) automatically when
|
|
|
|
a single value is being set.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
option:
|
|
|
|
description:
|
|
|
|
- if set (required for changing a I(value)), this is the name of the option.
|
|
|
|
- May be omitted if adding/removing a whole I(section).
|
|
|
|
required: false
|
2012-09-28 03:30:32 +02:00
|
|
|
default: null
|
2012-09-28 03:06:31 +02:00
|
|
|
value:
|
|
|
|
description:
|
|
|
|
- the string value to be associated with an I(option). May be omitted when removing an I(option).
|
|
|
|
required: false
|
2012-09-28 03:30:32 +02:00
|
|
|
default: null
|
2012-09-28 03:06:31 +02:00
|
|
|
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
|
2013-03-12 13:18:12 +01:00
|
|
|
default: "no"
|
2012-09-28 03:06:31 +02:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
2015-10-26 21:01:01 +01:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- If set to C(absent) the option or section will be removed if present instead of created.
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ "present", "absent" ]
|
2016-04-13 15:50:44 +02:00
|
|
|
no_extra_spaces:
|
|
|
|
description:
|
|
|
|
- do not insert spaces before and after '=' symbol
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "2.1"
|
2012-09-26 13:18:20 +02:00
|
|
|
notes:
|
|
|
|
- While it is possible to add an I(option) without specifying a I(value), this makes
|
|
|
|
no sense.
|
2012-11-27 11:29:21 +01:00
|
|
|
- A section named C(default) cannot be added by the module, but if it exists, individual
|
|
|
|
options within the section can be updated. (This is a limitation of Python's I(ConfigParser).)
|
|
|
|
Either use M(template) to create a base INI file with a C([default]) section, or use
|
|
|
|
M(lineinfile) to add the missing line.
|
2012-09-26 13:18:20 +02:00
|
|
|
requirements: [ ConfigParser ]
|
2015-11-07 14:23:35 +01:00
|
|
|
author:
|
|
|
|
- "Jan-Piet Mens (@jpmens)"
|
|
|
|
- "Ales Nosek (@noseka1)"
|
2012-09-26 13:18:20 +02:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Ensure "fav=lemonade is in section "[drinks]" in specified file
|
|
|
|
- ini_file: dest=/etc/conf section=drinks option=fav value=lemonade mode=0600 backup=yes
|
|
|
|
|
|
|
|
- ini_file: dest=/etc/anotherconf
|
|
|
|
section=drinks
|
|
|
|
option=temperature
|
|
|
|
value=cold
|
|
|
|
backup=yes
|
|
|
|
'''
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2015-11-23 14:51:08 +01:00
|
|
|
import os
|
2016-09-07 18:18:33 +02:00
|
|
|
import re
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2015-12-03 05:31:27 +01:00
|
|
|
# ==============================================================
|
|
|
|
# match_opt
|
|
|
|
|
|
|
|
def match_opt(option, line):
|
|
|
|
option = re.escape(option)
|
2016-07-01 00:15:49 +02:00
|
|
|
return re.match('%s( |\t)*=' % option, line) \
|
|
|
|
or re.match('# *%s( |\t)*=' % option, line) \
|
|
|
|
or re.match('; *%s( |\t)*=' % option, line)
|
2015-12-03 05:31:27 +01:00
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# match_active_opt
|
|
|
|
|
|
|
|
def match_active_opt(option, line):
|
|
|
|
option = re.escape(option)
|
2016-07-01 00:15:49 +02:00
|
|
|
return re.match('%s( |\t)*=' % option, line)
|
2015-12-03 05:31:27 +01:00
|
|
|
|
2012-09-26 13:18:20 +02:00
|
|
|
# ==============================================================
|
|
|
|
# do_ini
|
|
|
|
|
2016-04-13 15:50:44 +02:00
|
|
|
def do_ini(module, filename, section=None, option=None, value=None, state='present', backup=False, no_extra_spaces=False):
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2013-10-10 15:01:00 +02:00
|
|
|
|
2015-11-23 14:51:08 +01:00
|
|
|
if not os.path.exists(filename):
|
|
|
|
try:
|
|
|
|
open(filename,'w').close()
|
|
|
|
except:
|
|
|
|
module.fail_json(msg="Destination file %s not writable" % filename)
|
2015-10-31 05:57:25 +01:00
|
|
|
ini_file = open(filename, 'r')
|
|
|
|
try:
|
2015-10-29 06:04:32 +01:00
|
|
|
ini_lines = ini_file.readlines()
|
|
|
|
# append a fake section line to simplify the logic
|
|
|
|
ini_lines.append('[')
|
2015-10-31 05:57:25 +01:00
|
|
|
finally:
|
|
|
|
ini_file.close()
|
2012-11-27 11:29:21 +01:00
|
|
|
|
2015-10-29 06:04:32 +01:00
|
|
|
within_section = not section
|
|
|
|
section_start = 0
|
|
|
|
changed = False
|
2016-04-13 15:50:44 +02:00
|
|
|
if no_extra_spaces:
|
|
|
|
assignment_format = '%s=%s\n'
|
|
|
|
else:
|
|
|
|
assignment_format = '%s = %s\n'
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2015-10-29 06:04:32 +01:00
|
|
|
for index, line in enumerate(ini_lines):
|
|
|
|
if line.startswith('[%s]' % section):
|
|
|
|
within_section = True
|
|
|
|
section_start = index
|
|
|
|
elif line.startswith('['):
|
|
|
|
if within_section:
|
|
|
|
if state == 'present':
|
|
|
|
# insert missing option line at the end of the section
|
2016-04-13 15:50:44 +02:00
|
|
|
ini_lines.insert(index, assignment_format % (option, value))
|
2015-10-29 06:04:32 +01:00
|
|
|
changed = True
|
|
|
|
elif state == 'absent' and not option:
|
|
|
|
# remove the entire section
|
|
|
|
del ini_lines[section_start:index]
|
2012-09-26 13:18:20 +02:00
|
|
|
changed = True
|
2015-10-29 06:04:32 +01:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if within_section and option:
|
|
|
|
if state == 'present':
|
|
|
|
# change the existing option line
|
2015-12-03 05:31:27 +01:00
|
|
|
if match_opt(option, line):
|
2016-04-13 15:50:44 +02:00
|
|
|
newline = assignment_format % (option, value)
|
2015-10-29 06:04:32 +01:00
|
|
|
changed = ini_lines[index] != newline
|
|
|
|
ini_lines[index] = newline
|
|
|
|
if changed:
|
|
|
|
# remove all possible option occurences from the rest of the section
|
|
|
|
index = index + 1
|
|
|
|
while index < len(ini_lines):
|
|
|
|
line = ini_lines[index]
|
|
|
|
if line.startswith('['):
|
|
|
|
break
|
2015-12-03 05:31:27 +01:00
|
|
|
if match_active_opt(option, line):
|
2015-10-29 06:04:32 +01:00
|
|
|
del ini_lines[index]
|
|
|
|
else:
|
|
|
|
index = index + 1
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# comment out the existing option line
|
2015-12-03 05:31:27 +01:00
|
|
|
if match_active_opt(option, line):
|
2015-10-29 06:04:32 +01:00
|
|
|
ini_lines[index] = '#%s' % ini_lines[index]
|
|
|
|
changed = True
|
|
|
|
break
|
|
|
|
|
|
|
|
# remove the fake section line
|
|
|
|
del ini_lines[-1:]
|
|
|
|
|
|
|
|
if not within_section and option and state == 'present':
|
|
|
|
ini_lines.append('[%s]\n' % section)
|
2016-04-13 15:50:44 +02:00
|
|
|
ini_lines.append(assignment_format % (option, value))
|
2015-10-29 06:04:32 +01:00
|
|
|
changed = True
|
|
|
|
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2016-09-07 18:18:33 +02:00
|
|
|
backup_file = None
|
2015-02-23 12:14:38 +01:00
|
|
|
if changed and not module.check_mode:
|
2012-09-26 13:18:20 +02:00
|
|
|
if backup:
|
2016-09-07 18:18:33 +02:00
|
|
|
backup_file = module.backup_local(filename)
|
2015-10-31 05:57:25 +01:00
|
|
|
ini_file = open(filename, 'w')
|
|
|
|
try:
|
2015-10-29 06:04:32 +01:00
|
|
|
ini_file.writelines(ini_lines)
|
2015-10-31 05:57:25 +01:00
|
|
|
finally:
|
|
|
|
ini_file.close()
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2016-09-07 18:18:33 +02:00
|
|
|
return (changed, backup_file)
|
2012-09-26 13:18:20 +02:00
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest = dict(required=True),
|
|
|
|
section = dict(required=True),
|
2013-07-05 18:04:07 +02:00
|
|
|
option = dict(required=False),
|
|
|
|
value = dict(required=False),
|
2013-02-23 22:56:45 +01:00
|
|
|
backup = dict(default='no', type='bool'),
|
2016-04-13 15:50:44 +02:00
|
|
|
state = dict(default='present', choices=['present', 'absent']),
|
|
|
|
no_extra_spaces = dict(required=False, default=False, type='bool')
|
2013-01-28 17:46:35 +01:00
|
|
|
),
|
2015-02-23 12:14:38 +01:00
|
|
|
add_file_common_args = True,
|
|
|
|
supports_check_mode = True
|
2012-09-26 13:18:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
dest = os.path.expanduser(module.params['dest'])
|
|
|
|
section = module.params['section']
|
|
|
|
option = module.params['option']
|
|
|
|
value = module.params['value']
|
|
|
|
state = module.params['state']
|
2013-02-23 19:59:52 +01:00
|
|
|
backup = module.params['backup']
|
2016-04-13 15:50:44 +02:00
|
|
|
no_extra_spaces = module.params['no_extra_spaces']
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2016-09-07 18:18:33 +02:00
|
|
|
(changed,backup_file) = do_ini(module, dest, section, option, value, state, backup, no_extra_spaces)
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2013-01-28 17:46:35 +01:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
2014-03-19 03:39:45 +01:00
|
|
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2016-09-07 18:18:33 +02:00
|
|
|
results = { 'changed': changed, 'msg': "OK", 'dest': dest }
|
|
|
|
if backup_file is not None:
|
|
|
|
results['backup_file'] = backup_file
|
|
|
|
|
2012-09-26 13:18:20 +02:00
|
|
|
# Mission complete
|
2016-09-07 18:18:33 +02:00
|
|
|
module.exit_json(**results)
|
2012-09-26 13:18:20 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2015-10-26 21:01:01 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|