# Ansible: Kick off playbook ONLY if a certain directory contains any files.

**URL:** <https://forum.ansible.com/t/ansible-kick-off-playbook-only-if-a-certain-directory-contains-any-files/25731>\
**Category:** Ansible Project\
**Created:** [October 12, 2017, 9:34am UTC](https://forum.ansible.com/t/ansible-kick-off-playbook-only-if-a-certain-directory-contains-any-files/25731 "2017-10-12T09:34:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Searle](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@David\_Searle](https://forum.ansible.com/u/David_Searle)\
**Post date:** [October 12, 2017, 9:34am UTC](https://forum.ansible.com/t/ansible-kick-off-playbook-only-if-a-certain-directory-contains-any-files/25731/1 "2017-10-12T09:34:16Z")

</div>

Hi all,

I am fairly new to ansible but have been googling around for the answer to this and just cannot find anything that matches my request. So I wonder if you can assist please??

Basically I have a very long playbook to build an environment and set permissions etc etc etc and I have been instructed that at the very beginning of the PB I need to set a condition that IF a certain directory already contains files (any files at all) then the PB should exit, else it should continue with the rest of the installation and other bits.

How on earth do I do that sort of thing?? I just cannot see a module that helps me apart from it something that checks that the directory exists which is not what I want.

Thanks for any constructive input.

---

<div class="post-metadata">

**Author:** ![Goetz\_Leupold](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Goetz\_Leupold](https://forum.ansible.com/u/Goetz_Leupold)\
**Post date:** [October 12, 2017, 10:24am UTC](https://forum.ansible.com/t/ansible-kick-off-playbook-only-if-a-certain-directory-contains-any-files/25731/2 "2017-10-12T10:24:07Z")

</div>

With 2 to 3 tasks i guess:

# [http://docs.ansible.com/ansible/latest/find\_module.html](http://docs.ansible.com/ansible/latest/find_module.html)

- name: Find files in {{ path }}  
find:  
paths: “{{ path }}”

```auto
      patterns: '*.*'

```

register: foundFiles

# [http://docs.ansible.com/ansible/latest/meta\_module.html](http://docs.ansible.com/ansible/latest/meta_module.html)

- meta: end\_play  
when: foundFiles.matched \> 0

Optionally you could insert a debug msg task between these two, to write sth. like “ending play”.

---

<div class="post-metadata">

**Author:** ![David\_Searle](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@David\_Searle](https://forum.ansible.com/u/David_Searle)\
**Post date:** [October 12, 2017, 10:27am UTC](https://forum.ansible.com/t/ansible-kick-off-playbook-only-if-a-certain-directory-contains-any-files/25731/3 "2017-10-12T10:27:03Z")

</div>

Wow new modules I have not used before. I shall test that out.

Thank you so much for your input.
