monk
monk

Reputation: 2115

setting string as extra-var in ansible

I am wondering why string as an extravar is not working and as a number its working in the following output. It can be observed that foo is an extravar but it only works when number is passed and when I passed hi it failed.

when string is passed as extra-var:

ansible-playbook  fail.yml -e foo='hi'
PLAY [localhost] ****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [sample : debug] ***********************************************************************************************************************************************
ok: [localhost] => {
    "hi": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0

When number is passed as extra-var:

 -->ansible-playbook  fail.yml -e foo=1
PLAY [localhost] ****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [sample : debug] ***********************************************************************************************************************************************
ok: [localhost] => {
    "1": "1"
}
PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0


 -->cat fail.yml
---

- hosts: localhost

  roles:
    - sample

 -->cat sample/tasks/main.yml
---
# tasks file for sample
#
- debug: var={{ foo }}

Upvotes: 0

Views: 182

Answers (1)

Matt P
Matt P

Reputation: 2625

When using debug: var= you do not need to the braces, just the variable name.

Change your tasks file to:

- debug: var=foo

Upvotes: 1

Related Questions