Ansible check if variables are set

Viewed 14597

I want to automate the installation process of our software for our client. Therefore I wrote an Ansible playbook which has a task which should check if all the mandatory variables are set:

- name: Check environment variables.
  hosts: all
  vars_files: 
    - required_vars.yml
  tasks:
    - fail: msg="Variable '{{ item }}' is not defined"
      when: item not in hostvars[inventory_hostname]
      with_items: 
        - required_vars

The required_vars.yml looks like this:

required_vars:
  - APPHOME: /home/foo/bar
  - TMPDIR: /home/foo/bar/tmp

When I execute the playbook via ansible-playbook -i inventory/dev.yml playbook.yml I get the following error:

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************ok: [localhost]

TASK [fail] ************************************************************************************************************************************************************************************************************************************failed: 
[localhost] (item=required_vars) => {"changed": false, "failed": true, "item": "required_vars", "msg": "Variable 'required_vars' is not defined"}

It is obvious that I am doing something wrong, but I cannot point to the error. Can you help me please?

Edit: the accepted answer helped me out. Thank you. But I have two more questions:

  1. The executed task says:

    TASK [fail]
    skipping: [some_ip] => (item=/root)
    skipping: [some_ip] => (item=TMPDIR: /home/foo/bar/tmp)
    

It is getting skipped because all variables are set, correct?

  1. I think I figured out how to print the correct message, if the variable is not set:

    - name: Check environment variables.
      hosts: all
      vars_files: 
        - required_vars.yml
      tasks:
        - fail: 
            msg: "Variable '{{ item }}' is not defined"
          with_items: "{{ required_vars }}"
          when: item is undefined
    

Correct? Or is there a better solution?

2 Answers

Two problems here:

  1. You want to iterate over the value of required_vars variable value, so you need to provide it as an argument to with_items: "{{ required_vars }}":

    with_items: "{{ required_vars }}"
    

    Currently you are providing a list of a single element with a statically defined string required_vars.

  2. You need to change the data type of the elements in your required_vars list to strings:

    required_vars:
      - "APPHOME: /home/foo/bar"
      - "TMPDIR: /home/foo/bar/tmp"
    

    Currently (because of : followed by space) you defined dictionaries, so for example in the first iteration item will have a value of { "APPHOME": "/home/foo/bar" }, which will then always fail on the when condition.


Bonus problem:

  • you defined a message in the form "Variable '{{ item }}' is not defined";

  • Ansible reports Variable 'required_vars' is not defined;

  • the above is not an error, as you think ("I get the following error"), but a correct result of the fail module with the message you defined yourself.

Since you have only one value for 'with_items' I think it should look like this:

 with_items: "{{ required_vars }}"

On one line and with the brackets and quotation marks. Once you have more then one item, you can use the list like you did:

with_items:
  - "{{ one }}"
  - "{{ two }}"
Related