diff --git a/changelogs/fragments/4154-ini_file_changed.yml b/changelogs/fragments/4154-ini_file_changed.yml new file mode 100644 index 0000000000..f3dbb6bab8 --- /dev/null +++ b/changelogs/fragments/4154-ini_file_changed.yml @@ -0,0 +1,2 @@ +bugfixes: + - ini_file - when removing nothing do not report changed (https://github.com/ansible-collections/community.general/issues/4154). diff --git a/plugins/modules/files/ini_file.py b/plugins/modules/files/ini_file.py index f25cc063ff..79d373f3a7 100644 --- a/plugins/modules/files/ini_file.py +++ b/plugins/modules/files/ini_file.py @@ -367,9 +367,10 @@ def do_ini(module, filename, section=None, option=None, values=None, section_lines = new_section_lines else: # drop the entire section - section_lines = [] - msg = 'section removed' - changed = True + if section_lines: + section_lines = [] + msg = 'section removed' + changed = True # reassemble the ini_lines after manipulation ini_lines = before + section_lines + after diff --git a/tests/integration/targets/ini_file/tasks/tests/02-values.yml b/tests/integration/targets/ini_file/tasks/tests/02-values.yml index c3ef6b61a6..645490d111 100644 --- a/tests/integration/targets/ini_file/tasks/tests/02-values.yml +++ b/tests/integration/targets/ini_file/tasks/tests/02-values.yml @@ -504,6 +504,13 @@ state: absent register: result20 +- name: test-values 20 - remove section 'drinks' again to ensure idempotency" + ini_file: + path: "{{ output_file }}" + section: drinks + state: absent + register: result20_remove_again + - name: test-values 20 - read content from output file slurp: src: "{{ output_file }}" @@ -517,6 +524,7 @@ assert: that: - result20 is changed + - result20_remove_again is not changed - result20.msg == 'section removed' - content20 == "\n"