Add modify_inactive_option option to ini_file module to ignore matching commented key:value pairs (#7401)

* Add `modify_inactive_option` option to `ini_file` module to ignore matching commented key:value pairs

* Add changelog fragment

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* Minor comment fix

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
njutn95 2023-10-25 08:48:49 +02:00 committed by GitHub
commit 6ee1f27304
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 8 deletions

View file

@ -44,3 +44,6 @@
- name: include tasks to test ignore_spaces
include_tasks: tests/05-ignore_spaces.yml
- name: include tasks to test modify_inactive_option
include_tasks: tests/06-modify_inactive_option.yml

View file

@ -0,0 +1,123 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
## testing modify_inactive_option option
- name: test-modify_inactive_option 1 - create test file
copy:
content: |
[section1]
# Uncomment the line below to enable foo
# foo = bar
dest: "{{ output_file }}"
- name: test-modify_inactive_option 1 - 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: result1
- name: test-modify_inactive_option 1 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-modify_inactive_option 1 - set expected content and get current ini file content
set_fact:
expected1: |
[section1]
# Uncomment the line below to enable foo
foo = bar
content1: "{{ output_content.content | b64decode }}"
- name: test-modify_inactive_option 1 - assert 'changed' is true, content is OK and option changed
assert:
that:
- result1 is changed
- result1.msg == 'option changed'
- content1 == expected1
- name: test-modify_inactive_option 2 - create test file
copy:
content: |
[section1]
# Uncomment the line below to enable foo
# foo = bar
dest: "{{ output_file }}"
- name: test-modify_inactive_option 2 - set value for foo with modify_inactive_option set to false
ini_file:
path: "{{ output_file }}"
section: section1
option: foo
value: bar
modify_inactive_option: false
register: result2
- name: test-modify_inactive_option 2 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-modify_inactive_option 2 - set expected content and get current ini file content
set_fact:
expected2: |
[section1]
foo = bar
# Uncomment the line below to enable foo
# foo = bar
content2: "{{ output_content.content | b64decode }}"
- name: test-modify_inactive_option 2 - assert 'changed' is true and content is OK and option added
assert:
that:
- result2 is changed
- result2.msg == 'option added'
- content2 == expected2
- name: test-modify_inactive_option 3 - remove foo=bar with modify_inactive_option set to true to ensure it doesn't have effect for removal
ini_file:
path: "{{ output_file }}"
section: section1
option: foo
value: bar
modify_inactive_option: true
state: absent
register: result3
- name: test-modify_inactive_option 3 - read content from output file
slurp:
src: "{{ output_file }}"
register: output_content
- name: test-modify_inactive_option 3 - set expected content and get current ini file content
set_fact:
expected3: |
[section1]
# Uncomment the line below to enable foo
# foo = bar
content3: "{{ output_content.content | b64decode }}"
- name: test-modify_inactive_option 3 - assert 'changed' is true and content is OK and active option removed
assert:
that:
- result3 is changed
- result3.msg == 'option changed'
- content3 == expected3