From 260b9f648ca763ec493dc7e1f33c39f9b2bca708 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Mon, 14 Sep 2015 14:21:52 -0400 Subject: [PATCH] Fix fetch action plugin to not fail if file is missing and fail_if_missing=False (the default). Add tests to test_fetch role to verify it works as expected. --- lib/ansible/plugins/action/fetch.py | 2 ++ .../roles/test_fetch/tasks/main.yml | 31 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/action/fetch.py b/lib/ansible/plugins/action/fetch.py index 71bf68defd9..f963a07cb06 100644 --- a/lib/ansible/plugins/action/fetch.py +++ b/lib/ansible/plugins/action/fetch.py @@ -62,6 +62,8 @@ class ActionModule(ActionBase): if remote_checksum in ('1', '2') or self._play_context.become: slurpres = self._execute_module(module_name='slurp', module_args=dict(src=source), task_vars=task_vars, tmp=tmp) if slurpres.get('failed'): + if remote_checksum == '1' and not fail_on_missing: + return dict(msg="the remote file does not exist, not transferring, ignored", file=source, changed=False) return slurpres else: if slurpres['encoding'] == 'base64': diff --git a/test/integration/roles/test_fetch/tasks/main.yml b/test/integration/roles/test_fetch/tasks/main.yml index 47669d6c6d1..e14dc2fdadd 100644 --- a/test/integration/roles/test_fetch/tasks/main.yml +++ b/test/integration/roles/test_fetch/tasks/main.yml @@ -38,5 +38,34 @@ that: 'diff.stdout == ""' - +- name: attempt to fetch a non-existent file - do not fail on missing + fetch: src={{ output_dir }}/doesnotexist dest={{ output_dir }}/fetched + register: fetch_missing_nofail +- name: check fetch missing no fail result + assert: + that: + - "fetch_missing_nofail.msg" + - "not fetch_missing_nofail|changed" + +- name: attempt to fetch a non-existent file - fail on missing + fetch: src={{ output_dir }}/doesnotexist dest={{ output_dir }}/fetched fail_on_missing=yes + register: fetch_missing + ignore_errors: true + +- name: check fetch missing with failure + assert: + that: + - "fetch_missing|failed" + - "fetch_missing.msg" + - "not fetch_missing|changed" + +- name: attempt to fetch a directory - should not fail but return a message + fetch: src={{ output_dir }} dest={{ output_dir }}/somedir + register: fetch_dir + +- name: check fetch directory result + assert: + that: + - "not fetch_dir|changed" + - "fetch_dir.msg"