ini_file: added option 'noextraspaces' to turn off inserting extra spaces around '=' symbol

* ini_file: added option 'noextraspaces' to turn off inserting extra spaces around '=' symbol

* ini_file: noextraspaces, python 2.4 compatibility

* ini_file: noextraspaces - yes/no => true/false

* ini_file: noextraspaces - added 'version_added' option

* ini_file: noextraspaces => no_extra_spaces
This commit is contained in:
yarick123 2016-04-13 15:50:44 +02:00 committed by René Moser
parent 086735e255
commit bf2f09e37c

View file

@ -71,6 +71,12 @@ options:
required: false
default: "present"
choices: [ "present", "absent" ]
no_extra_spaces:
description:
- do not insert spaces before and after '=' symbol
required: false
default: false
version_added: "2.1"
notes:
- While it is possible to add an I(option) without specifying a I(value), this makes
no sense.
@ -118,7 +124,7 @@ def match_active_opt(option, line):
# ==============================================================
# do_ini
def do_ini(module, filename, section=None, option=None, value=None, state='present', backup=False):
def do_ini(module, filename, section=None, option=None, value=None, state='present', backup=False, no_extra_spaces=False):
if not os.path.exists(filename):
@ -137,6 +143,10 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
within_section = not section
section_start = 0
changed = False
if no_extra_spaces:
assignment_format = '%s=%s\n'
else:
assignment_format = '%s = %s\n'
for index, line in enumerate(ini_lines):
if line.startswith('[%s]' % section):
@ -146,7 +156,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
if within_section:
if state == 'present':
# insert missing option line at the end of the section
ini_lines.insert(index, '%s = %s\n' % (option, value))
ini_lines.insert(index, assignment_format % (option, value))
changed = True
elif state == 'absent' and not option:
# remove the entire section
@ -158,7 +168,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
if state == 'present':
# change the existing option line
if match_opt(option, line):
newline = '%s = %s\n' % (option, value)
newline = assignment_format % (option, value)
changed = ini_lines[index] != newline
ini_lines[index] = newline
if changed:
@ -185,7 +195,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
if not within_section and option and state == 'present':
ini_lines.append('[%s]\n' % section)
ini_lines.append('%s = %s\n' % (option, value))
ini_lines.append(assignment_format % (option, value))
changed = True
@ -212,7 +222,8 @@ def main():
option = dict(required=False),
value = dict(required=False),
backup = dict(default='no', type='bool'),
state = dict(default='present', choices=['present', 'absent'])
state = dict(default='present', choices=['present', 'absent']),
no_extra_spaces = dict(required=False, default=False, type='bool')
),
add_file_common_args = True,
supports_check_mode = True
@ -226,8 +237,9 @@ def main():
value = module.params['value']
state = module.params['state']
backup = module.params['backup']
no_extra_spaces = module.params['no_extra_spaces']
changed = do_ini(module, dest, section, option, value, state, backup)
changed = do_ini(module, dest, section, option, value, state, backup, no_extra_spaces)
file_args = module.load_file_common_arguments(module.params)
changed = module.set_fs_attributes_if_different(file_args, changed)