5

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?

user783836
  • 3,099
  • 2
  • 29
  • 34
  • 1
    Unfortunately that the way it works. I do the same way: --- - name: test hosts: localhost gather_facts: False vars: required_variables: - cloud_yy - cloud_xx tasks: - name: Validate required variables fail: msg: "No value specified for '{{ item }}'" when: ({{ item }} is undefined) or ({{ item }} is defined and {{ item|trim == '' }}) with_items: "{{ required_variables }}" – imjoseangel May 12 '18 at 15:58
  • 1
    Your are using `v1` and `v2` as strings.. if you will use ```- "{{ v1 }}" - "{{ v2 }}" ```you'll get another error saying you can't use undefined variables in loops.. – nadavbrkt May 12 '18 at 16:57
  • Possible duplicate of [How to Make Ansible variable mandatory](https://stackoverflow.com/questions/45013306/how-to-make-ansible-variable-mandatory) – Alexey Vazhnov Dec 24 '18 at 11:10

3 Answers3

10

Using the vars structure:

- name: validate some variables
  fail:
    msg: "Required variable {{item}} has not been provided"
  when: vars[item] is undefined
  loop:
    - v1
    - v2

Or, in Ansible 2.5, with the new vars lookup plugin:

- name: validate some variables
  debug:
  when: lookup('vars', item) is undefined
  loop:
    - v1
    - v2

Although not with the error message you specified, but a default error message for a lookup plugin.

The module won't even be executed, so you can use whatever ー I replaced fail with debug in the example above.

techraf
  • 64,883
  • 27
  • 193
  • 198
  • 1
    Cool one! Didn’t know about the vars lookup plugin in 2.5. Thanks techraf (+1) – imjoseangel May 12 '18 at 21:38
  • 2
    Wouldn't `assert` be a more appropriate module? – zigarn May 13 '18 at 00:26
  • 1
    Not sure if this changed, but in 2.7, the `vars` lookup will _result in an error if any of the variables is undefined_ (https://docs.ansible.com/ansible/2.5/plugins/lookup/vars.html). The first example seems to work though... – Lucas Dec 14 '18 at 20:03
6

Inside loop, you can use {{ variable | mandatory }} (see Forcing variables to be defined)

I think it looks nicer to add this as first task, it checks is v1 and v2 exist:

- name: 'Check mandatory variables are defined'
  assert:
    that:
      - v1 is defined
      - v2 is defined
Alexey Vazhnov
  • 1,291
  • 17
  • 20
-3

Try using below

  with_items:
    - v1
    - v2
Ranjit Singh
  • 37
  • 2
  • 9