From bf2f09e37c7bf4ea0dd66497dcd4ae02f542bd64 Mon Sep 17 00:00:00 2001 From: yarick123 Date: Wed, 13 Apr 2016 15:50:44 +0200 Subject: [PATCH] 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 --- files/ini_file.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/files/ini_file.py b/files/ini_file.py index 2dd021ad27c..ef4f8f56d90 100644 --- a/files/ini_file.py +++ b/files/ini_file.py @@ -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)