Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-12-2017, 04:41 AM   #1
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 307

Rep: Reputation: 36
Ansible: Kick off playbook ONLY if a certain directory contains any files

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.
Old 10-12-2017, 06:47 AM   #2
Registered: Jul 2007
Location: Knaphill, Surrey
Distribution: Linux Mint
Posts: 307

Original Poster
Rep: Reputation: 36
Found and tested some new modules and it worked....

- name: Check if a dir is empty - Exit if not
paths: /bob/test_dir
patterns: '*.*'
register: foundFiles

- debug:
msg: "Install files found. Exiting playbook"
when: foundFiles.matched > 0

- meta: end_play
when: foundFiles.matched > 0

- name: No files detected - Install code
shell: ls -lrt /bob/test_dir ## this is a dummy cmd to show things move on


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ansible playbook and need tabbed spaces in fstab fusion1275 Linux - Newbie 3 09-21-2017 02:44 PM
Using passwords in ansible playbook pgb205 Linux - Server 1 12-27-2016 12:20 AM
ansible will not execute whole playbook DimTheo Linux - Server 1 11-23-2016 11:59 PM
Vagrant+Ansible playbooks. How to use same playbook on non vagrant provision markotitel Linux - Virtualization and Cloud 1 12-06-2013 09:00 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:28 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration