8

Is it possible to force Ansible when replacing the var in yml files, which is undefined, throw out an error instead silently replaced by an empty string?

8

Add this line to the [defaults] section of your ansible.cfg:

error_on_undefined_vars = True

You'll now get an error message if a variable is undefined.

  • I believe that option requires 1.3, which is coming soon but has not yet been released. – mblakele Aug 16 '13 at 15:52
8

Yes, it is possible. Check the online documentation, under accessing complex variable data.

An example is provided to do exactly that:

tasks:
    - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - fail: msg="Bailing out: this play requires 'bar'"
      when: bar is not defined
1

Define your variables in

roles/<role_name>/defaults/main.yml

like:

SUPERVAR:
  VAR1:foo
  VAR2:bar

and then do in

roles/<role_name>/tasks/main.yml

like:

- fail: msg="{{ item }} is not defined"
  when: not {{ item }}
  with_items:
    - SUPERVAR.VAR1
    - SUPERVAR.VAR2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.