Search code examples
ansibleansible-2.xansible-inventory

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

  • 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!"  
    }