summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2020-06-16 20:27:57 -0400
committerGitHub <noreply@github.com>2020-06-16 17:27:57 -0700
commit6abe3a889d63f25db28dd2826276e3163af08381 (patch)
treec08fd097437371fa83b65e8043a67c3746eceaea
parent9e00214fb435e6b9ea223d966c2e4d9feecac35f (diff)
downloadansible-6abe3a889d63f25db28dd2826276e3163af08381.tar.gz
[stable-2.9] file - return 'state': 'absent' when a file does not exist (#66503) (#69657)
This was changed in ansible/ansible#51350. (cherry picked from commit cd8920af99) Co-authored-by: Sam Doran <sdoran@redhat.com>
-rw-r--r--changelogs/fragments/file-return-state-when-file-does-not-exist.yaml2
-rw-r--r--lib/ansible/modules/files/file.py2
-rw-r--r--test/integration/targets/file/tasks/main.yml21
3 files changed, 23 insertions, 2 deletions
diff --git a/changelogs/fragments/file-return-state-when-file-does-not-exist.yaml b/changelogs/fragments/file-return-state-when-file-does-not-exist.yaml
new file mode 100644
index 0000000000..c51abb29c5
--- /dev/null
+++ b/changelogs/fragments/file-return-state-when-file-does-not-exist.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+ - "file - return ``'state': 'absent'`` when a file does not exist (https://github.com/ansible/ansible/issues/66171)"
diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py
index 33a61516bf..c0729ff358 100644
--- a/lib/ansible/modules/files/file.py
+++ b/lib/ansible/modules/files/file.py
@@ -588,7 +588,7 @@ def ensure_file_attributes(path, follow, timestamps):
if prev_state not in ('file', 'hard'):
# file is not absent and any other state is a conflict
raise AnsibleModuleError(results={'msg': 'file (%s) is %s, cannot continue' % (path, prev_state),
- 'path': path})
+ 'path': path, 'state': prev_state})
diff = initial_diff(path, 'file', prev_state)
changed = module.set_fs_attributes_if_different(file_args, False, diff, expand=False)
diff --git a/test/integration/targets/file/tasks/main.yml b/test/integration/targets/file/tasks/main.yml
index 8731c55474..4e1d88e6ba 100644
--- a/test/integration/targets/file/tasks/main.yml
+++ b/test/integration/targets/file/tasks/main.yml
@@ -61,6 +61,25 @@
- "file_result.changed == false"
- "file_result.state == 'file'"
+- name: Make sure file does not exist
+ file:
+ path: /tmp/ghost
+ state: absent
+
+- name: Target a file that does not exist
+ file:
+ path: /tmp/ghost
+ ignore_errors: yes
+ register: ghost_file_result
+
+- name: Validate ghost file results
+ assert:
+ that:
+ - ghost_file_result is failed
+ - ghost_file_result is not changed
+ - ghost_file_result.state == 'absent'
+ - "'cannot continue' in ghost_file_result.msg"
+
- name: verify that we are checking an absent file
file: path={{output_dir}}/bar.txt state=absent
register: file2_result
@@ -269,7 +288,7 @@
copy: src=foobar dest={{output_dir}}
- name: check what would be removed if folder state was absent and diff is enabled
- file:
+ file:
path: "{{ item }}"
state: absent
check_mode: yes