# Changing an extra-vars variable to something else if it gets passed as a blank

**URL:** <https://forum.ansible.com/t/changing-an-extra-vars-variable-to-something-else-if-it-gets-passed-as-a-blank/28482>\
**Category:** Ansible Project\
**Created:** [August 31, 2018, 1:36pm UTC](https://forum.ansible.com/t/changing-an-extra-vars-variable-to-something-else-if-it-gets-passed-as-a-blank/28482 "2018-08-31T13:36:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [August 31, 2018, 1:36pm UTC](https://forum.ansible.com/t/changing-an-extra-vars-variable-to-something-else-if-it-gets-passed-as-a-blank/28482/1 "2018-08-31T13:36:31Z")

</div>

I would like to change the variable format, if it gets passed as a blank.

When running the playbook:

`  
ansible-playbook -vvv -i host deploy.yml --extra-vars “dal\_version=”

`

Which has a task (where I want to change the context of a blank variable):

`

- set\_fact:  
dal\_version: ‘hello’  
when: dal\_version == “”

- debug:  
msg: Dal version is {{ dal\_version }}  
`

I should expect to see the new format of the variable, in this case “hello”. However, I continue to see the blank.

However, I see the following Result:

`  
TASK [deploy : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path:  
ok: [linux.local] =\> {  
“ansible\_facts”: {  
“dal\_version”: “hello”  
},  
“changed”: false  
}

TASK [deploy : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path:  
ok: [linux.local] =\> {  
“msg”: "Dal version is "

}  
`

However, if I change the new variable to dal\_version2, I see the correct result:

When running the playbook:

`  
ansible-playbook -vvv -i host deploy.yml --extra-vars “dal\_version=”

`

Which has the task

`

- set\_fact:  
dal\_version2: ‘hello’  
when: dal\_version == “”

- debug:  
msg: Dal version is {{ dal\_version2 }}  
`

Result is as expected.

`  
TASK [deploy : set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path:  
ok: [linux.local] =\> {  
“ansible\_facts”: {  
“dal\_version2”: “hello”  
},  
“changed”: false  
}

TASK [deploy : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path:  
ok: [linux.local] =\> {  
“msg”: “Dal version is hello”  
}  
`

Is there a way I can set the same extra-vars variable to something else if it is passed as a blank?

Thanks!  
JS

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 31, 2018, 3:38pm UTC](https://forum.ansible.com/t/changing-an-extra-vars-variable-to-something-else-if-it-gets-passed-as-a-blank/28482/2 "2018-08-31T15:38:53Z")

</div>

> I would like to change the variable format, if it gets passed as a blank.
> 
> When running the playbook:
> 
> ansible-playbook -vvv -i host deploy.yml --extra-vars "dal\_version="
> 
> Which has a task (where I want to change the context of a blank variable):  
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dal\_version: 'hello'  
> &nbsp;&nbsp;when: dal\_version == ""
> 
> - debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Dal version is {{ dal\_version }}
> 
> I should expect to see the new format of the variable, in this case  
> "hello". However, I continue to see the blank.

\<snip /\>

> Is there a way I can set the same extra-vars variable to something else if  
> it is passed as a blank?

No, the extra vars has the highest precedence[1] so it's not possible to overwrite it.

If you don't wanna use a second variable you need to check it every time it's used like this

&nbsp;&nbsp;{{ (dal\_version == '') | ternary('Hello', dal\_version) }}

or like this

&nbsp;&nbsp;{{ 'Hello' if dal\_version == '' else dal\_version }}

[1] [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [September 4, 2018, 2:05pm UTC](https://forum.ansible.com/t/changing-an-extra-vars-variable-to-something-else-if-it-gets-passed-as-a-blank/28482/3 "2018-09-04T14:05:19Z")

</div>

Hi Kai

Many thanks for the swift response! So I didn’t end up using the ternary option has I had also setup a regex defined for the main variable.

However, I have resolved the issue in a slightly different way:

I’ve changed the main variable to “dali\_version”. If dal\_version gets entered as an extra-vars i.e. if “dal\_version is defined and is not blank” it can get assigned to dali\_version - otherwise dali\_version uses the default regex pattern I have set in the variables file.

`

- set\_fact:  
dali\_version: ‘{{ dal\_version }}’  
when: (dal\_version is defined) and (dal\_version != “”)  
`

Thanks a lot for your assistance!

Regards  
JS
