# only\_if directory/fiel does not exist

**URL:** <https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226>\
**Category:** Ansible Project\
**Created:** [January 21, 2013, 11:13pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226 "2013-01-21T23:13:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 21, 2013, 11:13pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/1 "2013-01-21T23:13:23Z")

</div>

I'm struggling with creating rules that need to be skipped if a given directory exits.

If I do this:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: create target dir  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command mkdir -p $target\_dir creates=$target\_dir  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: create\_directory

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: do my thing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command woopty woop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only\_if: '${create\_directory.changed}'

But if the directory already exists, then the first task is skipped, and the variable does not exist. I can't be the first one to have such a dependency... What's the recommended way of doing this?

Thanks

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 21, 2013, 11:20pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/2 "2013-01-21T23:20:27Z")

</div>

I made it work by adding the variable and setting it to zero:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;created\_directory: 0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: create target dir  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command mkdir -p $target\_dir creates=$target\_dir  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: created\_directory

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: do my thing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command woopty woop  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only\_if: '${created\_directory}'

This works. But now I wonder if there is a better, cleaner way of doing this.

Thanks.

---

<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:** [January 22, 2013, 12:37pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/3 "2013-01-22T12:37:44Z")

</div>

You should not need to default the registered variable if you drop the  
'creates=', the mkdir is itself harmless if run twice.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 22, 2013, 6:37pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/4 "2013-01-22T18:37:34Z")

</div>

mkdir -p, without "creates=":  
always executed, always "changed", always successful (rc=0), so it isn't useful to give me an idea of the state of the system.

I managed to make it works with mkdir, no -p, and "ignore\_errors: yes", then I capture the state with  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_integer: ${created\_directory.rc} == 0

But I don't like it, because the the mkdir task shows as failed, when really it hasn't, I'm looking for the state of the server, to make sure my playbook is idempotent. I've seen a mention of a stat module, I think it's a good idea, we need a simple way to use the existence of a file/directory as a state to trigger more tasks.

---

<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:** [January 22, 2013, 6:44pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/5 "2013-01-22T18:44:59Z")

</div>

Ah, here's the catch

"But if the directory already exists, then the first task is skipped, "

Not true, the task still runs with the 'creates=' in there. It  
decides to not execute on the remote end.

--Michael

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 22, 2013, 6:47pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/6 "2013-01-22T18:47:40Z")

</div>

Yves Dorfsman wrote:

> mkdir -p, without "creates=":  
> always executed, always "changed", always successful (rc=0), so it isn't  
> useful to give me an idea of the state of the system.
> 
> I managed to make it works with mkdir, no -p, and "ignore\_errors: yes",  
> then I  
> capture the state with  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_integer: ${created\_directory.rc} == 0
> 
> But I don't like it, because the the mkdir task shows as failed, when  
> really  
> it hasn't, I'm looking for the state of the server, to make sure my  
> playbook  
> is idempotent. I've seen a mention of a stat module, I think it's a good  
> idea,  
> we need a simple way to use the existence of a file/directory as a state  
> to  
> trigger more tasks.

Is there any reason you're not using the file module for this?

Daniel

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 22, 2013, 6:49pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/7 "2013-01-22T18:49:58Z")

</div>

It does say "skipping: [all]" when I use "creates="  
Also, ansible blows up when I try to access my variable, when the task is skipped.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 22, 2013, 6:57pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/8 "2013-01-22T18:57:12Z")

</div>

Yves Dorfsman wrote:

> I'm struggling with creating rules that need to be skipped if a given  
> directory exits.
> 
> If I do this:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: create target dir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command mkdir -p $target\_dir creates=$target\_dir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: create\_directory
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: do my thing  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: command woopty woop  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only\_if: '${create\_directory.changed}'
> 
> But if the directory already exists, then the first task is skipped, and  
> the  
> variable does not exist. I can't be the first one to have such a  
> dependency...  
> What's the recommended way of doing this?

The recommended way is using  
&nbsp;&nbsp;when\_bool: ${create\_directory.changed}

Daniel

---

<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:** [January 22, 2013, 7:02pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/9 "2013-01-22T19:02:02Z")

</div>

Can you please show me full output of the run with -v -v -v set?

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 22, 2013, 7:24pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/10 "2013-01-22T19:24:31Z")

</div>

> > It does say "skipping: [all]" when I use "creates="

---

<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:** [January 22, 2013, 7:38pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/11 "2013-01-22T19:38:45Z")

</div>

Yeah that's because it returns the skipped: True though, it is still executed.

It probably should just still set the variable but set it to skipped:  
True in this case.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 22, 2013, 7:42pm UTC](https://forum.ansible.com/t/only-if-directory-fiel-does-not-exist/12226/12 "2013-01-22T19:42:15Z")

</div>

So an alternative to setup a variable is to use when\_set, as mentioned by Daniel Hokka Zakrisson in another thread. I just tried it, I keep my "mkdir -p .../... creates=" (so the variable isn't set), and then use "when\_set" on that variable, and it works.
