2012-09-26 11:18:20 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
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.
|
2013-11-08 14:57:22 +00:00
|
|
|
- Comments are discarded when the source file is read, and therefore will not
|
|
|
|
show up in the destination file.
|
2012-09-26 11:18:20 +00:00
|
|
|
version_added: "0.9"
|
|
|
|
options:
|
2012-09-28 01:06:31 +00: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 01:30:32 +00:00
|
|
|
default: null
|
2012-09-28 01:06:31 +00: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 01:30:32 +00:00
|
|
|
default: null
|
2012-09-28 01:06:31 +00: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 12:18:12 +00:00
|
|
|
default: "no"
|
2012-09-28 01:06:31 +00:00
|
|
|
choices: [ "yes", "no" ]
|
|
|
|
others:
|
|
|
|
description:
|
|
|
|
- all arguments accepted by the M(file) module also work here
|
|
|
|
required: false
|
2012-09-26 11:18:20 +00:00
|
|
|
notes:
|
|
|
|
- While it is possible to add an I(option) without specifying a I(value), this makes
|
|
|
|
no sense.
|
2012-11-27 10:29:21 +00: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 11:18:20 +00:00
|
|
|
requirements: [ ConfigParser ]
|
|
|
|
author: Jan-Piet Mens
|
|
|
|
'''
|
|
|
|
|
2013-06-14 09:53:43 +00: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 11:18:20 +00:00
|
|
|
|
|
|
|
import ConfigParser
|
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# do_ini
|
|
|
|
|
|
|
|
def do_ini(module, filename, section=None, option=None, value=None, state='present', backup=False):
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
cp = ConfigParser.ConfigParser()
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(filename)
|
|
|
|
cp.readfp(f)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if option is None and value is None:
|
|
|
|
if cp.has_section(section):
|
|
|
|
cp.remove_section(section)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
if option is not None:
|
2013-07-05 16:04:07 +00:00
|
|
|
try:
|
|
|
|
if cp.get(section, option):
|
|
|
|
cp.remove_option(section, option)
|
|
|
|
changed = True
|
|
|
|
except:
|
|
|
|
pass
|
2012-09-26 11:18:20 +00:00
|
|
|
|
|
|
|
if state == 'present':
|
2013-10-10 13:01:00 +00:00
|
|
|
|
2013-10-10 13:10:00 +00:00
|
|
|
# DEFAULT section is always there by DEFAULT, so never try to add it.
|
|
|
|
if cp.has_section(section) == False and section.upper() != 'DEFAULT':
|
2012-11-27 10:29:21 +00:00
|
|
|
|
2013-10-10 13:10:00 +00:00
|
|
|
cp.add_section(section)
|
2012-09-26 11:18:20 +00:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
if option is not None and value is not None:
|
2013-07-05 16:04:07 +00:00
|
|
|
try:
|
|
|
|
oldvalue = cp.get(section, option)
|
|
|
|
if str(value) != str(oldvalue):
|
2012-09-26 11:18:20 +00:00
|
|
|
cp.set(section, option, value)
|
|
|
|
changed = True
|
2013-07-05 16:04:07 +00:00
|
|
|
except ConfigParser.NoSectionError:
|
|
|
|
cp.set(section, option, value)
|
|
|
|
changed = True
|
|
|
|
except ConfigParser.NoOptionError:
|
|
|
|
cp.set(section, option, value)
|
|
|
|
changed = True
|
2012-09-26 11:18:20 +00:00
|
|
|
|
|
|
|
if changed:
|
|
|
|
if backup:
|
|
|
|
module.backup_local(filename)
|
|
|
|
|
|
|
|
try:
|
|
|
|
f = open(filename, 'w')
|
|
|
|
cp.write(f)
|
|
|
|
except:
|
|
|
|
module.fail_json(msg="Can't creat %s" % filename)
|
|
|
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
# ==============================================================
|
|
|
|
# main
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
dest = dict(required=True),
|
|
|
|
section = dict(required=True),
|
2013-07-05 16:04:07 +00:00
|
|
|
option = dict(required=False),
|
|
|
|
value = dict(required=False),
|
2013-02-23 21:56:45 +00:00
|
|
|
backup = dict(default='no', type='bool'),
|
2012-09-26 11:18:20 +00:00
|
|
|
state = dict(default='present', choices=['present', 'absent'])
|
2013-01-28 16:46:35 +00:00
|
|
|
),
|
|
|
|
add_file_common_args = True
|
2012-09-26 11:18:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
info = dict()
|
|
|
|
|
|
|
|
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 18:59:52 +00:00
|
|
|
backup = module.params['backup']
|
2012-09-26 11:18:20 +00:00
|
|
|
|
|
|
|
changed = do_ini(module, dest, section, option, value, state, backup)
|
|
|
|
|
2013-01-28 16:46:35 +00:00
|
|
|
file_args = module.load_file_common_arguments(module.params)
|
|
|
|
changed = module.set_file_attributes_if_different(file_args, changed)
|
2012-09-26 11:18:20 +00:00
|
|
|
|
|
|
|
# Mission complete
|
2013-01-28 16:46:35 +00:00
|
|
|
module.exit_json(dest=dest, changed=changed, msg="OK")
|
2012-09-26 11:18:20 +00:00
|
|
|
|
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-09-26 11:18:20 +00:00
|
|
|
main()
|