Run a task only if a variable is defined

31 views
Skip to first unread message

Simón Muñoz

unread,
Nov 17, 2014, 1:29:07 PM11/17/14
to ansible...@googlegroups.com
Hi all,

I'm trying to run a task only if a variable is defined (for example, I have some groups where it's defined and some others where it's not). I got to this (adding when: nginx_sites), that seems to work, but just asking in case there is a better way. 

- name: Enabling nginx sites according to group and host_vars
  file
: >
    src
=/etc/nginx/sites-available/{{ item }}
    dest
=/etc/nginx/sites-enabled/{{ item }}
    state
=link
  with_items
: random_var
 
when: random_var
  notify
:
   
- reload nginx

Thanks in advance!

Michael DeHaan

unread,
Nov 17, 2014, 3:16:21 PM11/17/14
to ansible...@googlegroups.com
You can do this:

when: foo is defined




--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a3f9f779-be19-4745-8d57-b51d37359051%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rek...@voodoowarez.com

unread,
Nov 17, 2014, 5:37:26 PM11/17/14
to ansible...@googlegroups.com
`when: foo is defined` is what i used to do.

these days though, i've found that more often i want to still be able to signal a falsity.

unless extremely carefully considered, i try to use:

`when: foo|default(False)`

HH, -r

Simón Muñoz

unread,
Nov 18, 2014, 3:38:59 AM11/18/14
to ansible...@googlegroups.com
Thanks a lot guys ;)!
Reply all
Reply to author
Forward
0 new messages