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

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

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.

<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

  {{ (dal_version == '') | ternary('Hello', dal_version) }}

or like this

  {{ '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

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