# Copy a local variable file if it exists

**URL:** <https://forum.ansible.com/t/copy-a-local-variable-file-if-it-exists/15365>\
**Category:** Ansible Project\
**Created:** [May 29, 2014, 5:04pm UTC](https://forum.ansible.com/t/copy-a-local-variable-file-if-it-exists/15365 "2014-05-29T17:04:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre\_Mavro](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pierre\_Mavro](https://forum.ansible.com/u/Pierre_Mavro)\
**Post date:** [May 29, 2014, 5:04pm UTC](https://forum.ansible.com/t/copy-a-local-variable-file-if-it-exists/15365/1 "2014-05-29T17:04:42Z")

</div>

Hi,

I’m trying to do something with Ansible and I would like to know how to do something.

Is it possible to launch an action if a local (not remote) file exists? Example:

- name: Install firewall rules  
copy: src=init.{{ ansible\_hostname }} dest=/etc/init.d/iptables owner=root group=root mode=0755 backup=yes

The problem here is my source fails (if init.{{ ansible\_hostname }} doesn’t exists), I got an error message. Is it possible to do a such thing?

Thanks

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [May 29, 2014, 11:47pm UTC](https://forum.ansible.com/t/copy-a-local-variable-file-if-it-exists/15365/2 "2014-05-29T23:47:25Z")

</div>

I haven’t tested this, but how about the stat module run as a local action?

- local\_action: stat path=init.{{ ansible\_hostname }}  
register: init\_file

- name: Install firewall rules  
copy: …  
when: init\_file.stat.exists

Nathan

---

<div class="post-metadata">

**Author:** ![Pierre\_Mavro](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@Pierre\_Mavro](https://forum.ansible.com/u/Pierre_Mavro)\
**Post date:** [May 30, 2014, 1:09pm UTC](https://forum.ansible.com/t/copy-a-local-variable-file-if-it-exists/15365/3 "2014-05-30T13:09:22Z")

</div>

Hi,

Thanks a lot it works, that was absolutely what I was waiting for!

I totally missed local\_action Ansible function.

Regards
