From 1fa7bfcd5d6c6fb9e0d1f9b1dfe0f2860f3fb5a0 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 12 Jun 2019 16:56:15 +0200 Subject: [PATCH] Fix --diff to produce output when creating a new file (#57744) * Fix --diff to produce output when creating a new file Fixes #57618 * Make the check more defensive --- .../fragments/57618-fix-diff-on-absent-files.yaml | 2 ++ lib/ansible/plugins/action/__init__.py | 2 +- test/integration/targets/copy/tasks/main.yml | 13 +++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/57618-fix-diff-on-absent-files.yaml diff --git a/changelogs/fragments/57618-fix-diff-on-absent-files.yaml b/changelogs/fragments/57618-fix-diff-on-absent-files.yaml new file mode 100644 index 00000000000..a45790319a0 --- /dev/null +++ b/changelogs/fragments/57618-fix-diff-on-absent-files.yaml @@ -0,0 +1,2 @@ +bugfixes: + - "Fix --diff to produce output when creating a new file (https://github.com/ansible/ansible/issues/57618)" diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 2f6237d007c..f633c5c2293 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -1104,7 +1104,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): if not peek_result.get('failed', False) or peek_result.get('rc', 0) == 0: - if peek_result.get('state') == 'absent': + if peek_result.get('state') in (None, 'absent'): diff['before'] = u'' elif peek_result.get('appears_binary'): diff['dst_binary'] = 1 diff --git a/test/integration/targets/copy/tasks/main.yml b/test/integration/targets/copy/tasks/main.yml index c0a48e314ba..023d872183d 100644 --- a/test/integration/targets/copy/tasks/main.yml +++ b/test/integration/targets/copy/tasks/main.yml @@ -61,6 +61,19 @@ - import_tasks: acls.yml when: ansible_system == 'Linux' + # https://github.com/ansible/ansible/issues/57618 + - name: Test diff contents + copy: + content: 'Ansible managed\n' + dest: "{{ local_temp_dir }}/file.txt" + diff: yes + register: diff_output + + - assert: + that: + - 'diff_output.diff[0].before == ""' + - '"Ansible managed" in diff_output.diff[0].after' + always: - name: Cleaning file: