Conditionally execute a task if directory exists?

27,083 views
Skip to first unread message

Sebastiaan Pouyet

unread,
Jul 3, 2013, 8:02:27 AM7/3/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jul 3, 2013, 9:57:13 AM7/3/13
to ansible...@googlegroups.com
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





Sebastiaan

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Mark Mandel

unread,
Jul 3, 2013, 7:14:47 PM7/3/13
to ansible...@googlegroups.com
if you need an example, I did it this way here:

For my local oh-my-zsh provisioning.

(Short version)


- name: See if zsh is installed
  shell: "[ -d ~/.oh-my-zsh ] && echo 'Found' || echo ''"
  register: zsh_installed
- name: install oh my zsh


  when: (not zsh_installed.stdout)

Sebastiaan Pouyet

unread,
Jul 5, 2013, 4:55:27 AM7/5/13
to ansible...@googlegroups.com
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:

---
- name: Check if path exists
  stat: path=/etc/monit/conf.d
  register: check_path

- name: It exists
  debug: msg='Yay, the path exists!'
  when: check_path.stat.exists

- name: It doesn't exist
  debug: msg="Boohoo, the path doesn't exist..."
  when: check_path.stat.exists == false

Mark Mandel

unread,
Jul 7, 2013, 11:06:42 PM7/7/13
to ansible...@googlegroups.com
Oh Nice! I didn't see that come in. Very cool!

Mark

On Fri, Jul 5, 2013 at 6:55 PM, Sebastiaan Pouyet <srpo...@gmail.com> wrote:
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:

---
- name: Check if path exists
  stat: path=/etc/monit/conf.d
  register: check_path

- name: It exists
  debug: msg='Yay, the path exists!'
  when: check_path.stat.exists

- name: It doesn't exist
  debug: msg="Boohoo, the path doesn't exist..."
  when: check_path.stat.exists == false




Kevin Chow

unread,
Apr 24, 2015, 2:43:58 PM4/24/15
to ansible...@googlegroups.com
Hi Sebastiaan,

Thanks for sharing. It's helpful..

Kevin

decoding devops

unread,
Apr 12, 2019, 3:12:39 PM4/12/19
to Ansible Project

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.
Reply all
Reply to author
Forward
0 new messages