mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 03:00:23 -07:00
ini_file: fix unmatched whitespace before comment (#10102)
* ini_file: fix unmatched whitespace before comment * Add changelog fragment * Update changelog fragment: bugfixes to minor_changes Co-authored-by: Felix Fontein <felix@fontein.de> --------- Co-authored-by: sibebleuze <> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
fcd2d8b7b7
commit
43eb342b18
3 changed files with 53 additions and 9 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- ini_file - modify an inactive option also when there are spaces in front of the comment symbol (https://github.com/ansible-collections/community.general/pull/10102, https://github.com/ansible-collections/community.general/issues/8539).
|
|
@ -268,21 +268,21 @@ from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||||
|
|
||||||
def match_opt(option, line):
|
def match_opt(option, line):
|
||||||
option = re.escape(option)
|
option = re.escape(option)
|
||||||
return re.match('([#;]?)( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
return re.match('( |\t)*([#;]?)( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
||||||
|
|
||||||
|
|
||||||
def match_active_opt(option, line):
|
def match_active_opt(option, line):
|
||||||
option = re.escape(option)
|
option = re.escape(option)
|
||||||
return re.match('()( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
return re.match('()()( |\t)*(%s)( |\t)*(=|$)( |\t)*(.*)' % option, line)
|
||||||
|
|
||||||
|
|
||||||
def update_section_line(option, changed, section_lines, index, changed_lines, ignore_spaces, newline, msg):
|
def update_section_line(option, changed, section_lines, index, changed_lines, ignore_spaces, newline, msg):
|
||||||
option_changed = None
|
option_changed = None
|
||||||
if ignore_spaces:
|
if ignore_spaces:
|
||||||
old_match = match_opt(option, section_lines[index])
|
old_match = match_opt(option, section_lines[index])
|
||||||
if not old_match.group(1):
|
if not old_match.group(2):
|
||||||
new_match = match_opt(option, newline)
|
new_match = match_opt(option, newline)
|
||||||
option_changed = old_match.group(7) != new_match.group(7)
|
option_changed = old_match.group(8) != new_match.group(8)
|
||||||
if option_changed is None:
|
if option_changed is None:
|
||||||
option_changed = section_lines[index] != newline
|
option_changed = section_lines[index] != newline
|
||||||
if option_changed:
|
if option_changed:
|
||||||
|
@ -299,7 +299,7 @@ def check_section_has_values(section_has_values, section_lines):
|
||||||
for condition in section_has_values:
|
for condition in section_has_values:
|
||||||
for line in section_lines:
|
for line in section_lines:
|
||||||
match = match_opt(condition["option"], line)
|
match = match_opt(condition["option"], line)
|
||||||
if match and (len(condition["values"]) == 0 or match.group(7) in condition["values"]):
|
if match and (len(condition["values"]) == 0 or match.group(8) in condition["values"]):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -432,8 +432,8 @@ def do_ini(module, filename, section=None, section_has_values=None, option=None,
|
||||||
for index, line in enumerate(section_lines):
|
for index, line in enumerate(section_lines):
|
||||||
if match_function(option, line):
|
if match_function(option, line):
|
||||||
match = match_function(option, line)
|
match = match_function(option, line)
|
||||||
if values and match.group(7) in values:
|
if values and match.group(8) in values:
|
||||||
matched_value = match.group(7)
|
matched_value = match.group(8)
|
||||||
if not matched_value and allow_no_value:
|
if not matched_value and allow_no_value:
|
||||||
# replace existing option with no value line(s)
|
# replace existing option with no value line(s)
|
||||||
newline = u'%s\n' % option
|
newline = u'%s\n' % option
|
||||||
|
@ -505,7 +505,7 @@ def do_ini(module, filename, section=None, section_has_values=None, option=None,
|
||||||
section_lines = new_section_lines
|
section_lines = new_section_lines
|
||||||
elif not exclusive and len(values) > 0:
|
elif not exclusive and len(values) > 0:
|
||||||
# delete specified option=value line(s)
|
# delete specified option=value line(s)
|
||||||
new_section_lines = [i for i in section_lines if not (match_active_opt(option, i) and match_active_opt(option, i).group(7) in values)]
|
new_section_lines = [i for i in section_lines if not (match_active_opt(option, i) and match_active_opt(option, i).group(8) in values)]
|
||||||
if section_lines != new_section_lines:
|
if section_lines != new_section_lines:
|
||||||
changed = True
|
changed = True
|
||||||
msg = 'option changed'
|
msg = 'option changed'
|
||||||
|
|
|
@ -120,4 +120,46 @@
|
||||||
that:
|
that:
|
||||||
- result3 is changed
|
- result3 is changed
|
||||||
- result3.msg == 'option changed'
|
- result3.msg == 'option changed'
|
||||||
- content3 == expected3
|
- content3 == expected3
|
||||||
|
|
||||||
|
|
||||||
|
- name: test-modify_inactive_option 4 - create test file with spaces before commented option
|
||||||
|
copy:
|
||||||
|
content: |
|
||||||
|
|
||||||
|
[section1]
|
||||||
|
# Uncomment the line below to enable foo
|
||||||
|
# foo = bar
|
||||||
|
|
||||||
|
dest: "{{ output_file }}"
|
||||||
|
|
||||||
|
- name: test-modify_inactive_option 4 - set value for foo with modify_inactive_option set to true
|
||||||
|
ini_file:
|
||||||
|
path: "{{ output_file }}"
|
||||||
|
section: section1
|
||||||
|
option: foo
|
||||||
|
value: bar
|
||||||
|
modify_inactive_option: true
|
||||||
|
register: result4
|
||||||
|
|
||||||
|
- name: test-modify_inactive_option 4 - read content from output file
|
||||||
|
slurp:
|
||||||
|
src: "{{ output_file }}"
|
||||||
|
register: output_content
|
||||||
|
|
||||||
|
- name: test-modify_inactive_option 4 - set expected content and get current ini file content
|
||||||
|
set_fact:
|
||||||
|
expected4: |
|
||||||
|
|
||||||
|
[section1]
|
||||||
|
# Uncomment the line below to enable foo
|
||||||
|
foo = bar
|
||||||
|
|
||||||
|
content4: "{{ output_content.content | b64decode }}"
|
||||||
|
|
||||||
|
- name: test-modify_inactive_option 4 - assert 'changed' is true and content is OK and option changed
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result4 is changed
|
||||||
|
- result4.msg == 'option changed'
|
||||||
|
- content4 == expected4
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue