Skip to content

Commit

Permalink
ini_file: add support for lists of options/values
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmens committed Jun 15, 2013
1 parent cf2ddb6 commit af051d6
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions library/files/ini_file
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,21 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
changed = True
else:
if option is not None:
try:
if cp.get(section, option):
cp.remove_option(section, option)
changed = True
except:
pass
if type(option) == str:
try:
if cp.get(section, option):
cp.remove_option(section, option)
changed = True
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 cp.has_section(section) == False:
Expand All @@ -123,17 +132,31 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
changed = True

if option is not None and value is not None:
try:
oldvalue = cp.get(section, option)
if str(value) != str(oldvalue):
olist = []
vlist = []
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)
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 backup:
Expand All @@ -156,8 +179,8 @@ def main():
argument_spec = dict(
dest = dict(required=True),
section = dict(required=True),
option = dict(required=False),
value = dict(required=False),
option = dict(required=False, type='list'),
value = dict(required=False, type='list'),
backup = dict(default='no', type='bool'),
state = dict(default='present', choices=['present', 'absent'])
),
Expand Down

0 comments on commit af051d6

Please sign in to comment.