Run a task only if a variable is defined

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!

`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

You can do this:

when: foo is defined

Thanks a lot guys ;)!