Copy local file if exists, using ansible

A more comprehensive answer:

If you want to check the existence of a local file before performing some task, here is the comprehensive snippet:

- name: get file stat to be able to perform a check in the following task
  local_action: stat path=/path/to/file
  register: file

- name: copy file if it exists
  copy: src=/path/to/file dest=/destination/path
  when: file.stat.exists

If you want to check the existence of a remote file before performing some task, this is the way to go:

- name: get file stat to be able to perform check in the following task
  stat: path=/path/to/file
  register: file

- name: copy file if it exists
  copy: src=/path/to/file dest=/destination/path
  when: file.stat.exists

If you don't wont to set up two tasks, you could use 'is file' to check if local files exists:

tasks:
- copy: src=/a/b/filetocopy.zip dest=/tmp/filetocopy.zip
  when: '/a/b/filetocopy.zip' is file

The path is relative to the playbook directory, so using the magic variable role_path is recommended if you are referring to files inside the role directory.

Ref: http://docs.ansible.com/ansible/latest/playbooks_tests.html#testing-paths


Change your first step into the following on

- name: copy local filetocopy.zip to remote if exists
  local_action: stat path="../filetocopy.zip"
  register: result