From f527bb61f903fa9835a5cc5b84f1bfb315bf5170 Mon Sep 17 00:00:00 2001 From: James Livulpi Date: Thu, 17 Feb 2022 07:35:46 -0500 Subject: [PATCH] ini_file: Don't report changed=true when removing if nothing is changed. (#4155) * don't report changed when nothing is removed * add change log * linter happy * Update plugins/modules/files/ini_file.py Co-authored-by: Felix Fontein * Update changelogs/fragments/4154-ini_file_changed.yml Co-authored-by: Felix Fontein * add absent idempotency test Co-authored-by: Felix Fontein --- changelogs/fragments/4154-ini_file_changed.yml | 2 ++ plugins/modules/files/ini_file.py | 7 ++++--- .../targets/ini_file/tasks/tests/02-values.yml | 8 ++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/4154-ini_file_changed.yml 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"