Force Ansible to throw out error when vars is missing in yml?

Solution 1:

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

Solution 2:

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.

Solution 3:

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