mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
[PR #6575/f710a10f backport][stable-6] ini_file: try using inactive option before creating a new one (#6728)
ini_file: try using inactive option before creating a new one (#6575)
* ini_file: make inactive options as active if they exist, instead of creating a new option entry
Add changelog fragment
* Update changelogs/fragments/ini_file-use-inactive-options-when-possible.yml
Co-authored-by: Felix Fontein <felix@fontein.de>
* Fix test
* Update tests
* Fix spelling
---------
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit f710a10f25
)
Co-authored-by: njutn95 <njutn95@yahoo.com>
This commit is contained in:
parent
afd24ccd35
commit
2b88ee01d3
3 changed files with 24 additions and 22 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- ini_file - fix a bug where the inactive options were not used when possible (https://github.com/ansible-collections/community.general/pull/6575).
|
|
@ -316,14 +316,14 @@ def do_ini(module, filename, section=None, option=None, values=None,
|
||||||
# override option with no value to option with value if not allow_no_value
|
# override option with no value to option with value if not allow_no_value
|
||||||
if len(values) > 0:
|
if len(values) > 0:
|
||||||
for index, line in enumerate(section_lines):
|
for index, line in enumerate(section_lines):
|
||||||
if not changed_lines[index] and match_active_opt(option, line):
|
if not changed_lines[index] and match_opt(option, line):
|
||||||
newline = assignment_format % (option, values.pop(0))
|
newline = assignment_format % (option, values.pop(0))
|
||||||
(changed, msg) = update_section_line(changed, section_lines, index, changed_lines, newline, msg)
|
(changed, msg) = update_section_line(changed, section_lines, index, changed_lines, newline, msg)
|
||||||
if len(values) == 0:
|
if len(values) == 0:
|
||||||
break
|
break
|
||||||
# remove all remaining option occurrences from the rest of the section
|
# remove all remaining option occurrences from the rest of the section
|
||||||
for index in range(len(section_lines) - 1, 0, -1):
|
for index in range(len(section_lines) - 1, 0, -1):
|
||||||
if not changed_lines[index] and match_active_opt(option, section_lines[index]):
|
if not changed_lines[index] and match_opt(option, section_lines[index]):
|
||||||
del section_lines[index]
|
del section_lines[index]
|
||||||
del changed_lines[index]
|
del changed_lines[index]
|
||||||
changed = True
|
changed = True
|
||||||
|
|
|
@ -453,7 +453,7 @@
|
||||||
- content17 == expected17
|
- content17 == expected17
|
||||||
|
|
||||||
|
|
||||||
- name: "test-values 18 - Ensure 'beverage=coke' is 'abesent' in section '[drinks]'"
|
- name: "test-values 18 - Ensure 'beverage=coke' is 'absent' in section '[drinks]'"
|
||||||
ini_file:
|
ini_file:
|
||||||
path: "{{ output_file }}"
|
path: "{{ output_file }}"
|
||||||
section: drinks
|
section: drinks
|
||||||
|
@ -483,7 +483,7 @@
|
||||||
- content18 == expected18
|
- content18 == expected18
|
||||||
|
|
||||||
|
|
||||||
- name: "test-values 19 - Ensure non-existant 'beverage=coke' is 'abesent' in section '[drinks]'"
|
- name: "test-values 19 - Ensure non-existent 'beverage=coke' is 'absent' in section '[drinks]'"
|
||||||
ini_file:
|
ini_file:
|
||||||
path: "{{ output_file }}"
|
path: "{{ output_file }}"
|
||||||
section: drinks
|
section: drinks
|
||||||
|
@ -579,8 +579,8 @@
|
||||||
[section1]
|
[section1]
|
||||||
var1 = aaa
|
var1 = aaa
|
||||||
# comment in section
|
# comment in section
|
||||||
var2 = foo
|
# var2 = some value
|
||||||
# var2 = bar
|
# comment after section
|
||||||
|
|
||||||
[section2]
|
[section2]
|
||||||
var3 = ccc
|
var3 = ccc
|
||||||
|
@ -613,8 +613,8 @@
|
||||||
[section1]
|
[section1]
|
||||||
var1 = aaa
|
var1 = aaa
|
||||||
# comment in section
|
# comment in section
|
||||||
var2 = foo
|
# var2 = some value
|
||||||
# var2 = bar
|
# comment after section
|
||||||
|
|
||||||
[section2]
|
[section2]
|
||||||
var3 = ccc
|
var3 = ccc
|
||||||
|
@ -629,14 +629,13 @@
|
||||||
- content22 == expected22
|
- content22 == expected22
|
||||||
|
|
||||||
|
|
||||||
- name: "test-values 23 - Ensure 'var2=[foo, foobar]' is 'present' in section '[section1]'"
|
- name: "test-values 23 - Ensure 'var2=foo' is 'present' in section '[section1]', replacing commented option 'var2=some value'"
|
||||||
ini_file:
|
ini_file:
|
||||||
path: "{{ output_file }}"
|
path: "{{ output_file }}"
|
||||||
section: section1
|
section: section1
|
||||||
option: var2
|
option: var2
|
||||||
values:
|
values:
|
||||||
- foo
|
- foo
|
||||||
- foobar
|
|
||||||
state: present
|
state: present
|
||||||
register: result23
|
register: result23
|
||||||
|
|
||||||
|
@ -647,7 +646,6 @@
|
||||||
|
|
||||||
- name: test-values 23 - set expected content and get current ini file content
|
- name: test-values 23 - set expected content and get current ini file content
|
||||||
set_fact:
|
set_fact:
|
||||||
content23: "{{ output_content.content | b64decode }}"
|
|
||||||
expected23: |
|
expected23: |
|
||||||
|
|
||||||
# Some comment to test
|
# Some comment to test
|
||||||
|
@ -659,28 +657,28 @@
|
||||||
var1 = aaa
|
var1 = aaa
|
||||||
# comment in section
|
# comment in section
|
||||||
var2 = foo
|
var2 = foo
|
||||||
var2 = foobar
|
# comment after section
|
||||||
# var2 = bar
|
|
||||||
|
|
||||||
[section2]
|
[section2]
|
||||||
var3 = ccc
|
var3 = ccc
|
||||||
# comment after section
|
# comment after section
|
||||||
- name: test-values 23 - assert 'changed' and msg 'option added' and content is as expected
|
content23: "{{ output_content.content | b64decode }}"
|
||||||
|
|
||||||
|
- name: test-values 23 - assert 'changed' and msg 'option changed' and content is as expected
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result23 is changed
|
- result23 is changed
|
||||||
- result23.msg == 'option added'
|
- result23.msg == 'option changed'
|
||||||
- content23 == expected23
|
- content23 == expected23
|
||||||
|
|
||||||
|
|
||||||
- name: "test-values 24 - Ensure 'var2=[foo, foobar, bar]' is 'present' in section '[section1]' replacing commented option 'var2=bar'"
|
- name: "test-values 24 - Ensure 'var2=[foo, foobar]' is 'present' in section '[section1]'"
|
||||||
ini_file:
|
ini_file:
|
||||||
path: "{{ output_file }}"
|
path: "{{ output_file }}"
|
||||||
section: section1
|
section: section1
|
||||||
option: var2
|
option: var2
|
||||||
values:
|
values:
|
||||||
- foo
|
- foo
|
||||||
- bar
|
|
||||||
- foobar
|
- foobar
|
||||||
state: present
|
state: present
|
||||||
register: result24
|
register: result24
|
||||||
|
@ -692,7 +690,6 @@
|
||||||
|
|
||||||
- name: test-values 24 - set expected content and get current ini file content
|
- name: test-values 24 - set expected content and get current ini file content
|
||||||
set_fact:
|
set_fact:
|
||||||
content24: "{{ output_content.content | b64decode }}"
|
|
||||||
expected24: |
|
expected24: |
|
||||||
|
|
||||||
# Some comment to test
|
# Some comment to test
|
||||||
|
@ -705,16 +702,18 @@
|
||||||
# comment in section
|
# comment in section
|
||||||
var2 = foo
|
var2 = foo
|
||||||
var2 = foobar
|
var2 = foobar
|
||||||
var2 = bar
|
# comment after section
|
||||||
|
|
||||||
[section2]
|
[section2]
|
||||||
var3 = ccc
|
var3 = ccc
|
||||||
# comment after section
|
# comment after section
|
||||||
- name: test-values 24 - assert 'added' and msg 'option changed' and content is as expected
|
content24: "{{ output_content.content | b64decode }}"
|
||||||
|
|
||||||
|
- name: test-values 24 - assert 'changed' and msg 'option added' and content is as expected
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result24 is changed
|
- result24 is changed
|
||||||
- result24.msg == 'option changed'
|
- result24.msg == 'option added'
|
||||||
- content24 == expected24
|
- content24 == expected24
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue