Merge pull request #3226 from jpmens/ini_file2

ini_file: add support for lists of options/values
This commit is contained in:
Michael DeHaan 2013-06-19 18:59:19 -07:00
commit c80ad67d7e

View file

@ -108,12 +108,21 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
changed = True changed = True
else: else:
if option is not None: if option is not None:
try: if type(option) == str:
if cp.get(section, option): try:
cp.remove_option(section, option) if cp.get(section, option):
changed = True cp.remove_option(section, option)
except: changed = True
pass except:
pass
else:
for o in option:
try:
if cp.get(section, o):
cp.remove_option(section, o)
changed = True
except:
pass
if state == 'present': if state == 'present':
if cp.has_section(section) == False: if cp.has_section(section) == False:
@ -124,17 +133,31 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
changed = True changed = True
if option is not None and value is not None: if option is not None and value is not None:
try: olist = []
oldvalue = cp.get(section, option) vlist = []
if str(value) != str(oldvalue): if type(option) == str and type(value) == str:
olist.append(option)
vlist.append(value)
else:
olist = list(option)
vlist = list(value)
if len(olist) != len(vlist):
module.fail_json(msg="Option and value lists must be of same lengths")
n = 0
for option in olist:
value = vlist[n]
n = n + 1
try:
oldvalue = cp.get(section, option)
if str(value) != str(oldvalue):
cp.set(section, option, value)
changed = True
except ConfigParser.NoSectionError:
cp.set(section, option, value)
changed = True
except ConfigParser.NoOptionError:
cp.set(section, option, value) cp.set(section, option, value)
changed = True changed = True
except ConfigParser.NoSectionError:
cp.set(section, option, value)
changed = True
except ConfigParser.NoOptionError:
cp.set(section, option, value)
changed = True
if changed: if changed:
if backup: if backup:
@ -157,8 +180,8 @@ def main():
argument_spec = dict( argument_spec = dict(
dest = dict(required=True), dest = dict(required=True),
section = dict(required=True), section = dict(required=True),
option = dict(required=False), option = dict(required=False, type='list'),
value = dict(required=False), value = dict(required=False, type='list'),
backup = dict(default='no', type='bool'), backup = dict(default='no', type='bool'),
state = dict(default='present', choices=['present', 'absent']) state = dict(default='present', choices=['present', 'absent'])
), ),