I'm trying to use when: item is undefined
in Ansible 2.5 to check if a list of variables have been set, as below:
- hosts: all
tasks:
- name: validate some variables
fail:
msg: "Required variable {{item}} has not been provided"
when: item is undefined
loop:
- v1
- v2
However, this never fails regardless of whether v1
or v2
are provided.
Switching the when
to use jinja2 templating works:
when: "{{item}} is undefined"
But ansible complains about this:
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{item}} is undefined
What is the correct way loop through a list of variable names and checked they have been set?