# checking if particular file exists locally before go

**URL:** <https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577>\
**Category:** Ansible Project\
**Created:** [June 30, 2014, 9:50pm UTC](https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577 "2014-06-30T21:50:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Evgeniev](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nick\_Evgeniev](https://forum.ansible.com/u/Nick_Evgeniev)\
**Post date:** [June 30, 2014, 9:50pm UTC](https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577/1 "2014-06-30T21:50:09Z")

</div>

hi,

given ansible role directory layout:  
files  
vars  
tasks  
…

and having files/foo/hostname/somefile I want not only be able to copy this file like:

- copy src=foo/{{ ansible\_hostname }}/somefile dest=…

but also to limit certain steps depending whether such file exist or not… basically to define variable and condition other steps on it. unfortunately stat module is limited to remote host… what’s the other way around?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 30, 2014, 10:03pm UTC](https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577/2 "2014-06-30T22:03:35Z")

</div>

“I want not only be able to copy this file”

copy module, but you’ve got that

“but also to limit certain steps depending whether such file exist or not.”

when statement

“basically to define variable and condition other steps on it.”

include\_vars + when statement

“unfortunately stat module is limited to remote host…”

local\_action

---

<div class="post-metadata">

**Author:** ![Nick\_Evgeniev](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nick\_Evgeniev](https://forum.ansible.com/u/Nick_Evgeniev)\
**Post date:** [June 30, 2014, 10:18pm UTC](https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577/3 "2014-06-30T22:18:42Z")

</div>

the idea was to define variable automatically based on whether file exist or not. as a temporary solution i had to declare variable in host\_vars. and yes i’m using when statement 🙂

it looks like ‘stat’ module doesn’t understand the ‘magic’ of role directory layout, so I would have to specify full path… am I missing something?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 30, 2014, 11:10pm UTC](https://forum.ansible.com/t/checking-if-particular-file-exists-locally-before-go/15577/4 "2014-06-30T23:10:01Z")

</div>

“it looks like ‘stat’ module doesn’t understand the ‘magic’ of role directory layout, so I would have to specify full path… am I missing something?”

{{ playbook\_dir}}/roles/foo/files/bar.txt
