1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-07-04 01:28:53 +00:00

ini_file: do not delete comment-only lines containing the option name

When state=present and exclusive=true, the cleanup loops in do_ini()
treated any line that contained the option name as a match, including
pure documentation comments such as "; output_buffering" that have no
"=" sign. As a result those lines were either rewritten in place or
deleted, even though they are not configuration entries.

Skip lines where match.group(2) is a comment character ("#" or ";") and
match.group(6) is empty (no "=" present). This keeps the existing
behaviour of replacing commented config lines like ";output_buffering = 4096"
when modify_inactive_option=true, while leaving doc comments untouched.

Adds two regression tests covering the exact scenario from the issue and
the simpler commented-config replacement case.

Fixes: https://github.com/ansible-collections/community.general/issues/11919
This commit is contained in:
AsifAd 2026-05-19 16:04:56 +05:30 committed by Asif Draxi
parent f9d4f0ad6b
commit fde6b13a7f
No known key found for this signature in database
3 changed files with 121 additions and 2 deletions

View file

@ -468,7 +468,10 @@ def do_ini(
# override option with no value to option with value if not allow_no_value
if len(values) > 0:
for index, line in enumerate(section_lines):
if not changed_lines[index] and match_function(option, line):
match = not changed_lines[index] and match_function(option, line)
# skip comment-only lines (e.g. "; output_buffering" with no "="):
# group(2) is the comment char, group(6) is "=" or "" (end-of-line match)
if match and not (match.group(2) and not match.group(6)):
newline = f"{option}{sep}{values.pop(0)}\n"
(changed, msg) = update_section_line(
option, changed, section_lines, index, changed_lines, ignore_spaces, newline, msg
@ -477,7 +480,9 @@ def do_ini(
break
# remove all remaining option occurrences from the rest of the section
for index in range(len(section_lines) - 1, 0, -1):
if not changed_lines[index] and match_function(option, section_lines[index]):
match = not changed_lines[index] and match_function(option, section_lines[index])
# skip comment-only lines (no "=") — only remove active or commented config lines
if match and not (match.group(2) and not match.group(6)):
del section_lines[index]
del changed_lines[index]
changed = True