# ansible "when:" directive: check file exists, and other nifty things?

**URL:** <https://forum.ansible.com/t/ansible-when-directive-check-file-exists-and-other-nifty-things/17423>\
**Category:** Ansible Project\
**Created:** [February 2, 2015, 7:09pm UTC](https://forum.ansible.com/t/ansible-when-directive-check-file-exists-and-other-nifty-things/17423 "2015-02-02T19:09:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Feliksik1](https://avatars.discourse-cdn.com/v4/letter/e/c67d28/32.png) [@Eric\_Feliksik1](https://forum.ansible.com/u/Eric_Feliksik1)\
**Post date:** [February 2, 2015, 7:09pm UTC](https://forum.ansible.com/t/ansible-when-directive-check-file-exists-and-other-nifty-things/17423/1 "2015-02-02T19:09:37Z")

</div>

I can run a task conditionally, depending on the existence of a file, as follows:

- name: check if the file exists  
stat: path=/my/file  
register: file\_register
- name: do the actual task  
include: my\_subtask.yml  
when: not file\_register.stat.exists

Is it not possible to do smarter things in the when clause? Possible with a Jinja2 directive? I tried to search for the documentation of the lookup() function I see sometimes, but cannot find it. Is is part of Ansible or of Jinja? Is it usable here?

Thanks in advance,  
Eric

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 2, 2015, 8:05pm UTC](https://forum.ansible.com/t/ansible-when-directive-check-file-exists-and-other-nifty-things/17423/2 "2015-02-02T20:05:20Z")

</div>

lookups are part of ansible, lookups run only on the 'master' so for  
this to work all actions would be local.
