# make a task conditional on a file not existing on the managed host?

**URL:** <https://forum.ansible.com/t/make-a-task-conditional-on-a-file-not-existing-on-the-managed-host/11625>\
**Category:** Ansible Project\
**Created:** [June 24, 2012, 3:15pm UTC](https://forum.ansible.com/t/make-a-task-conditional-on-a-file-not-existing-on-the-managed-host/11625 "2012-06-24T15:15:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [June 24, 2012, 3:15pm UTC](https://forum.ansible.com/t/make-a-task-conditional-on-a-file-not-existing-on-the-managed-host/11625/1 "2012-06-24T15:15:50Z")

</div>

I have a playbook that I’d like to use to populate a particular directory on the managed host by rsyncing the contents of that same directory on a different host, but only in the case where the directory in question does not yet exist on the managed host. I tried doing that first with a task like this:

- name: rsync data directory $item from prodhost if it doesn’t exist already  
action: command /usr/bin/rsync -avz $prodhost:/home/jbcsys/data/$item /home/jbcsys/data/  
only\_if: “not os.path.exists(‘/home/jbcsys/data/$item’)”  
with\_items:
- foo
- bar

…and at first I thought it was working the way I intended. But then I realized that that was an artifact of how I was testing the playbook: by running ansible-playbook on the same host that I was actually trying to manage. I thought the python expression in the only\_if was being executed on the managed host. But it’s apparently being executed on the host where I’m running ansible-playbook.

So far I’ve worked around this by writing a script that exists on the managed host that does the same thing (that is, that has internal logic to rsync the files in question if the directory does not yet exist on localhost), then I run that script on the managed host by invoking “command” in my ansible playbook. But this seems like an obvious thing to want to be able to do within ansible (to have execution of a command on the managed host be conditional on some fact about the filesystem on that managed host), so I’m wondering if there’s an obvious way to do this in ansible that I’ve overlooked. Is there?

Thanks.

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [June 24, 2012, 3:32pm UTC](https://forum.ansible.com/t/make-a-task-conditional-on-a-file-not-existing-on-the-managed-host/11625/2 "2012-06-24T15:32:17Z")

</div>

You'd probably want to use the 'creates' argument.

- name: rsync data directory $item from prodhost if it doesn't exist already  
&nbsp;&nbsp;action: command /usr/bin/rsync -avz $prodhost:/home/jbcsys/data/$item /home/jbcsys/data/ creates=/home/jbcsys/data/$item  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- foo  
&nbsp;&nbsp;&nbsp;&nbsp;- bar

---

<div class="post-metadata">

**Author:** ![John\_Callender](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@John\_Callender](https://forum.ansible.com/u/John_Callender)\
**Post date:** [June 25, 2012, 6:13pm UTC](https://forum.ansible.com/t/make-a-task-conditional-on-a-file-not-existing-on-the-managed-host/11625/3 "2012-06-25T18:13:40Z")

</div>

Ah, perfect. That was exactly what I wanted. Thanks!

John
