Search code examples
serveransibleansible-inventory

Ansible vars inventory


I'm learning Ansible. And I was following the official docs:

But I have a little question. How use the vars in the inventories?

I have try to use some of the default parameters like self_destruct_countdown.

[pruebascomandos]
MY-SERVER-IP self_destruct_countdown=60
OTHER-MY-SERVER-IP

And using the apply variables to all group. With a own var.

[pruebascomandos:vars]
example=true

But my problem is that in both cases I try to check the var with:

$ ansible pruebascomandos -m shell -a "echo $self_destruct_countdown"

$ ansible pruebascomandos -m shell -a "echo $example"

And in both cases I get a blank response. I don't sure why.

If someone can explain why or tell me where to read it it would be great. Thank to everyone!


Solution

  • Double braces {{ }} are needed to evaluate the variable. Try this

    shell> ansible pruebascomandos -i hosts -m shell -a "echo {{ example }}"
    test_01 | CHANGED | rc=0 >>
    true
    test_02 | CHANGED | rc=0 >>
    true
    
    shell> ansible pruebascomandos -i hosts -m shell -a "echo {{ self_destruct_countdown }}"
    test_02 | FAILED | rc=-1 >>
    The task includes an option with an undefined variable. The error was: self_destruct_countdown is undefined
    test_01 | CHANGED | rc=0 >>
    60
    

    The host test_02 failed because the variable self_destruct_countdown had been defined for test_01 only.

    shell> cat hosts
    [pruebascomandos]
    test_01 self_destruct_countdown=60
    test_02
    
    [pruebascomandos:vars]
    example=true