# Conditionally execute a task if directory exists?

**URL:** <https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057>\
**Category:** Ansible Project\
**Created:** [July 3, 2013, 12:02pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057 "2013-07-03T12:02:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastiaan\_Pouyet](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Sebastiaan\_Pouyet](https://forum.ansible.com/u/Sebastiaan_Pouyet)\
**Post date:** [July 3, 2013, 12:02pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/1 "2013-07-03T12:02:27Z")

</div>

Hi all,

I’ve searched the group and the ansible documentation, but haven’t been able to find a clear answer.

I want to conditionally upload a monit config file in a nginx playbook, but only when the monnit conf directory is present.

tasks:- name: “add monit nginx config”  
copy: src=nginx.monit dest=/etc/monit/conf.d owner=root group=root mode=0700  
when: ???

The when condition should return True if the directory /etc/monit/conf.d/ exists.

How can I achieve this? Is the an expression I can use, or should I set a “monit\_installed” fact in the monit role, or use facter?  
Also, is there a difference between only\_if and when?

Cheers and one zillion gallactic kudos for developing Ansible!

Sebastiaan

---

<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:** [July 3, 2013, 1:57pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/2 "2013-07-03T13:57:13Z")

</div>

Sure thing.

You probably want to use the register module to save the result of a test and act upon it. What command you want to execute is pretty wide open, maybe it’s rpm -qa, etc.

- shell: …  
register: some\_result

Then you might do:

- copy: …  
when: some\_result.rc == 0

In 1.3, there’s also a ‘stat’ module you can use which returns lots of useful attributes and works really well with register.

Should you want to use facts instead to write a “monit installed fact”, you could do that too … there shouldn’t be any need to use facter, as you could write your own ansible facts and just call the module.

There is also the new “facts.d” feature in Ansible 1.3 (not yet documented as it’s the development version), where you can drop a JSON file or executable script that returns JSON into /etc/ansible/facts.d/\*.fact

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [July 3, 2013, 11:14pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/3 "2013-07-03T23:14:47Z")

</div>

if you need an example, I did it this way here:  
[https://github.com/markmandel/dotfiles/blob/master/provision/roles/core/tasks/\_oh\_my\_zsh.yml#L4](https://github.com/markmandel/dotfiles/blob/master/provision/roles/core/tasks/_oh_my_zsh.yml#L4)

For my local oh-my-zsh provisioning.

(Short version)

```auto

- name: See if zsh is installed

  shell: "[-d ~/.oh-my-zsh] && echo 'Found' || echo ''"

  register: zsh_installed
- name: install oh my zsh

```

---

<div class="post-metadata">

**Author:** ![Sebastiaan\_Pouyet](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@Sebastiaan\_Pouyet](https://forum.ansible.com/u/Sebastiaan_Pouyet)\
**Post date:** [July 5, 2013, 8:55am UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/4 "2013-07-05T08:55:27Z")

</div>

Thanks for your help Michael and Mark, I appreciate it!  
In addition to Mark’s example, another way of checking the existence of a path is with the stat module which was added in 1.3:

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [July 8, 2013, 3:06am UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/5 "2013-07-08T03:06:42Z")

</div>

Oh Nice! I didn't see that come in. Very cool!

Mark

---

<div class="post-metadata">

**Author:** ![Kevin\_Chow](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@Kevin\_Chow](https://forum.ansible.com/u/Kevin_Chow)\
**Post date:** [April 24, 2015, 6:08pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/6 "2015-04-24T18:08:21Z")

</div>

Hi Sebastiaan,

Thanks for sharing. It’s helpful…

Kevin

---

<div class="post-metadata">

**Author:** ![decoding\_devops](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@decoding\_devops](https://forum.ansible.com/u/decoding_devops)\
**Post date:** [April 12, 2019, 7:12pm UTC](https://forum.ansible.com/t/conditionally-execute-a-task-if-directory-exists/13057/7 "2019-04-12T19:12:39Z")

</div>

Hi Sebastiaan Pouyet,

once read this articale how to know if the directory is already existed or not, you can implement this condition in your case.

[**Ansible Create directory if not exists**](https://www.decodingdevops.com/ansible-create-directory-if-not-exists/)
