summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2020-11-09 14:45:09 -0500
committerGitHub <noreply@github.com>2020-11-09 14:45:09 -0500
commita1730af91f94552e8aaff4bedfb8dcae00bd284d (patch)
tree419be8ec7228755d54fc828bd22df191e32d096f
parente7bf0696efc39e6704312f1518c9033e0edc8e35 (diff)
downloadansible-a1730af91f94552e8aaff4bedfb8dcae00bd284d.tar.gz
Ensure blockinfile correctly returns backupfile (#72544)
* Ensure blockinfile correctly returns backupfile Fixes #27626 based on #27859 Co-authored-by: Giovanni Sciortino (@giovannisciortino)
-rw-r--r--changelogs/fragments/blockinfile_fix_no_backup_return.yml2
-rw-r--r--lib/ansible/modules/blockinfile.py9
-rw-r--r--test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml5
3 files changed, 14 insertions, 2 deletions
diff --git a/changelogs/fragments/blockinfile_fix_no_backup_return.yml b/changelogs/fragments/blockinfile_fix_no_backup_return.yml
new file mode 100644
index 0000000000..e35cd41b1b
--- /dev/null
+++ b/changelogs/fragments/blockinfile_fix_no_backup_return.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - blockinfile now returns name of backup file when this option is used.
diff --git a/lib/ansible/modules/blockinfile.py b/lib/ansible/modules/blockinfile.py
index 2f80a65edc..fde44b5d60 100644
--- a/lib/ansible/modules/blockinfile.py
+++ b/lib/ansible/modules/blockinfile.py
@@ -328,9 +328,10 @@ def main():
msg = 'Block inserted'
changed = True
+ backup_file = None
if changed and not module.check_mode:
if module.boolean(params['backup']) and path_exists:
- module.backup_local(path)
+ backup_file = module.backup_local(path)
# We should always follow symlinks so that we change the real file
real_path = os.path.realpath(params['path'])
write_changes(module, result, real_path)
@@ -345,7 +346,11 @@ def main():
attr_diff['after_header'] = '%s (file attributes)' % path
difflist = [diff, attr_diff]
- module.exit_json(changed=changed, msg=msg, diff=difflist)
+
+ if backup_file is None:
+ module.exit_json(changed=changed, msg=msg, diff=difflist)
+ else:
+ module.exit_json(changed=changed, msg=msg, diff=difflist, backup_file=backup_file)
if __name__ == '__main__':
diff --git a/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml b/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml
index dbb93ecce3..7093ed2b99 100644
--- a/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml
+++ b/test/integration/targets/blockinfile/tasks/add_block_to_existing_file.yml
@@ -12,6 +12,11 @@
backup: yes
register: blockinfile_test0
+- name: ensure we have a bcackup file
+ assert:
+ that:
+ - "'backup_file' in blockinfile_test0"
+
- name: check content
shell: 'grep -c -e "Match User ansible-agent" -e "PasswordAuthentication no" {{ output_dir_test }}/sshd_config'
register: blockinfile_test0_grep