Use role default argument if playbook argument is undefined.

Hi.

As mentioned in the title, I want Ansible to use the role default argument if a playbook argument is undefined.

Here is my project:

main.yml
inventory.ini
roles/
test/
defaults/
main.yml
tasks/
main.yml

I have a basic structure like this, with:

roles/tests/default/main.yml:

---
version: latest

roles/tests/tasks/main.yml:

---
- debug:
msg: "{{ foo }}"

inventory.ini:


[all]
localhost 127.0.0.1

What I want:

I want my role to have a variable version, defaulted to latest. Because of potential conflict with the name version, I want my playbook to look like:

main.yml:


---
- hosts: localhost
roles:
- role: test
vars:
version: "{{ test_version }}"

So invoking ansible-playbook -i inventory.ini -e test_version=1.0.0 main.yml will result on version to be equal to 1.0.0, but use roles/test/default/main.yml:version default if omitted.

I’ve tried filters like omit or default and I can’t get my expected behavior. Any suggestions?

Results, with:
version: "{{ test_version | default(omit) }}" and -e test_version=1.0.0: “1.0.0”
version: "{{ test_version | default(omit) }}" without -e: “Hello World!”
version: "{{ test_version | default() }}" and -e test_version=1.0.0: “1.0.0”
version: "{{ test_version | default() }}" without -e: “”

Thanks a lot.

What I want:

I want my role to have a variable `version`, defaulted to latest. Because
of potential conflict with the name `version`, I want my playbook to look
like:

I recommend to always prefix variable name in the role with role name and underscore.
Since you can't have two roles with the same name your variables will never conflict with other roles.

`main.yml`:

---
- hosts: localhost
  roles:
    - role: test
  vars:
    version: "{{ test_version }}"

So invoking `ansible-playbook -i inventory.ini -e test_version=1.0.0
main.yml ` will result on `version` to be equal to `1.0.0`, but use
`roles/test/default/main.yml:version` default if omitted.

I've tried filters like `omit` or `default` and I can't get my expected
behavior. Any suggestions?

Results, with:
`version: "{{ test_version | default(omit) }}"` and ` -e
test_version=1.0.0`: "1.0.0"
`version: "{{ test_version | default(omit) }}"` without `-e`: "Hello World!"
`version: "{{ test_version | default() }}"` and ` -e test_version=1.0.0`:
"1.0.0"
`version: "{{ test_version | default() }}"` without `-e`: ""

If you still wanna go this route you would need to provide the value that you would like in default
  version: "{{ test_version | default('latest') }}"

Ok but this means the default value is duplicated :expressionless:

Maybe it’s too dangerous, due to variable precedence, to use variables like version.

Ok but this means the default value is duplicated :expressionless:

Unfortunately yes.

Maybe it's too dangerous, due to variable precedence, to use variables like
`version`.

You also might like to set the variables in the host_vars and/or group_vars in the future, to do that you need unique variables.

Ok but this means the default value is duplicated :expressionless:

Unfortunately yes.

Maybe it’s too dangerous, due to variable precedence, to use variables like
version.

You also might like to set the variables in the host_vars and/or group_vars in the future, to do that you need unique variables.

True. Thanks for pointing this.