'Ansible inventory variable undefined

I've defined a variable in my inventory file, but i get error variable is undefined when using it in play condition.

inventory.yml:

[nodes]
node1 ....
node1 ....

[cluster:children]
nodes

[cluster:vars]
somevar=False

testvar=value

playbook.yml:

- include: setup_whatever.yml
  when: "testvar == 'value'"

run it with

ansible-playbook playbook.yml -i inventory

here is the error message i get

ERROR! The conditional check 'testvar == 'value'' failed. The error was: ERROR! error while evaluating conditional (testvar == 'value'): ERROR! 'testvar' is undefined

ansible version: 2.0.0.2



Solution 1:[1]

Upgrade your Ansible version to a modern one.

  • Ansible 2.0.0.2 (yours)
ERROR! The conditional check 'testvar == 'value'' failed. The error was:
ERROR! error while evaluating conditional (testvar == 'value'):
ERROR! 'testvar' is undefined
  • Ansible 2.1.3.0 (also outdated!)
PLAY [nodes]  
\*******************************************************************

TASK [debug]  
\*******************************************************************  
ok: [node1] => {  
? "msg": "Hello world!"  
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YSC