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:
parent
086735e255
commit
bf2f09e37c
1 changed files with 18 additions and 6 deletions
|
@ -71,6 +71,12 @@ options:
|
||||||
required: false
|
required: false
|
||||||
default: "present"
|
default: "present"
|
||||||
choices: [ "present", "absent" ]
|
choices: [ "present", "absent" ]
|
||||||
|
no_extra_spaces:
|
||||||
|
description:
|
||||||
|
- do not insert spaces before and after '=' symbol
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
version_added: "2.1"
|
||||||
notes:
|
notes:
|
||||||
- While it is possible to add an I(option) without specifying a I(value), this makes
|
- While it is possible to add an I(option) without specifying a I(value), this makes
|
||||||
no sense.
|
no sense.
|
||||||
|
@ -118,7 +124,7 @@ def match_active_opt(option, line):
|
||||||
# ==============================================================
|
# ==============================================================
|
||||||
# do_ini
|
# 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):
|
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
|
within_section = not section
|
||||||
section_start = 0
|
section_start = 0
|
||||||
changed = False
|
changed = False
|
||||||
|
if no_extra_spaces:
|
||||||
|
assignment_format = '%s=%s\n'
|
||||||
|
else:
|
||||||
|
assignment_format = '%s = %s\n'
|
||||||
|
|
||||||
for index, line in enumerate(ini_lines):
|
for index, line in enumerate(ini_lines):
|
||||||
if line.startswith('[%s]' % section):
|
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 within_section:
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
# insert missing option line at the end of the section
|
# 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
|
changed = True
|
||||||
elif state == 'absent' and not option:
|
elif state == 'absent' and not option:
|
||||||
# remove the entire section
|
# remove the entire section
|
||||||
|
@ -158,7 +168,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
# change the existing option line
|
# change the existing option line
|
||||||
if match_opt(option, line):
|
if match_opt(option, line):
|
||||||
newline = '%s = %s\n' % (option, value)
|
newline = assignment_format % (option, value)
|
||||||
changed = ini_lines[index] != newline
|
changed = ini_lines[index] != newline
|
||||||
ini_lines[index] = newline
|
ini_lines[index] = newline
|
||||||
if changed:
|
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':
|
if not within_section and option and state == 'present':
|
||||||
ini_lines.append('[%s]\n' % section)
|
ini_lines.append('[%s]\n' % section)
|
||||||
ini_lines.append('%s = %s\n' % (option, value))
|
ini_lines.append(assignment_format % (option, value))
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
|
||||||
|
@ -212,7 +222,8 @@ def main():
|
||||||
option = dict(required=False),
|
option = dict(required=False),
|
||||||
value = dict(required=False),
|
value = dict(required=False),
|
||||||
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']),
|
||||||
|
no_extra_spaces = dict(required=False, default=False, type='bool')
|
||||||
),
|
),
|
||||||
add_file_common_args = True,
|
add_file_common_args = True,
|
||||||
supports_check_mode = True
|
supports_check_mode = True
|
||||||
|
@ -226,8 +237,9 @@ def main():
|
||||||
value = module.params['value']
|
value = module.params['value']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
backup = module.params['backup']
|
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)
|
file_args = module.load_file_common_arguments(module.params)
|
||||||
changed = module.set_fs_attributes_if_different(file_args, changed)
|
changed = module.set_fs_attributes_if_different(file_args, changed)
|
||||||
|
|
Loading…
Reference in a new issue