# file exists check with only\_if

**URL:** <https://forum.ansible.com/t/file-exists-check-with-only-if/12818>\
**Category:** Ansible Project\
**Created:** [May 17, 2013, 1:40pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818 "2013-05-17T13:40:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shirou](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@shirou](https://forum.ansible.com/u/shirou)\
**Post date:** [May 17, 2013, 1:40pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818/1 "2013-05-17T13:40:45Z")

</div>

Hello,

In order to make a command task idempotence, I want to check a file  
exists by using "only\_if" something like this

- name: do something  
&nbsp;&nbsp;command: do\_something.sh  
&nbsp;&nbsp;only\_if: not os.path.exists("result.file")

I know make one more task and use "register" can realize this, but I  
want to keep a playbook simple.

Is there a way to evaluate more programmable expression at the "only\_if" ?

Regards,

WAKAYAMA Shirou

---

<div class="post-metadata">

**Author:** ![Patrick\_Regan](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@Patrick\_Regan](https://forum.ansible.com/u/Patrick_Regan)\
**Post date:** [May 17, 2013, 2:34pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818/2 "2013-05-17T14:34:30Z")

</div>

What you want is already built into the command module. See the "creates"  
option.

[http://ansible.cc/docs/modules.html#command](http://ansible.cc/docs/modules.html#command)

So your call would look like this:

- name: do something  
&nbsp;&nbsp;command: do\_something.sh creates=/path/to/result.file

This cause the command to be skipped if the file is present.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 17, 2013, 2:40pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818/3 "2013-05-17T14:40:22Z")

</div>

So “only\_if” is the old way do conditionals, we have “when:” in 1.2 (the “when\_integer” and so on were a stopgap in 1.1).

Anyway, it executes locally, not remotely.

It seems like you can benefit from the “creates=” and “removes=” parameters to the command and shell modules for what you want to do as long as it involves file existence/absence.

---

<div class="post-metadata">

**Author:** ![shirou](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@shirou](https://forum.ansible.com/u/shirou)\
**Post date:** [May 18, 2013, 12:55pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818/4 "2013-05-18T12:55:19Z")

</div>

Hello,

> What you want is already built into the command module. See the "creates" option.  
> - name: do something  
> command: do\_something.sh creates=/path/to/result.file

Thank you, it works!

But, these "creates" and "removes" are little misunderstanding names I think.  
How about "if\_exists" and "not\_exists" ?

Regards,  
Shirou

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 18, 2013, 7:31pm UTC](https://forum.ansible.com/t/file-exists-check-with-only-if/12818/5 "2013-05-18T19:31:23Z")

</div>

Not going to change them.
