# Use role default argument if playbook argument is undefined.

**URL:** <https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423>\
**Category:** Ansible Project\
**Created:** [January 5, 2018, 1:44pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423 "2018-01-05T13:44:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antony\_MECHIN](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Antony\_MECHIN](https://forum.ansible.com/u/Antony_MECHIN)\
**Post date:** [January 5, 2018, 1:44pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423/1 "2018-01-05T13:44:10Z")

</div>

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:

```auto
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`:

```auto
---
version: latest

```

`roles/tests/tasks/main.yml`:

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

```

`inventory.ini`:

```auto

[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`:

```auto

---
- 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.

---

<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:** [January 5, 2018, 2:07pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423/2 "2018-01-05T14:07:14Z")

</div>

> 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`:
> 
> ```auto
> ---
> - 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  
&nbsp;&nbsp;version: "{{ test\_version | default('latest') }}"

---

<div class="post-metadata">

**Author:** ![Antony\_MECHIN](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Antony\_MECHIN](https://forum.ansible.com/u/Antony_MECHIN)\
**Post date:** [January 5, 2018, 2:15pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423/3 "2018-01-05T14:15:08Z")

</div>

Ok but this means the default value is duplicated 😑

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

---

<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:** [January 5, 2018, 2:52pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423/4 "2018-01-05T14:52:33Z")

</div>

> Ok but this means the default value is duplicated 😑

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.

---

<div class="post-metadata">

**Author:** ![Antony\_MECHIN](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@Antony\_MECHIN](https://forum.ansible.com/u/Antony_MECHIN)\
**Post date:** [January 5, 2018, 3:35pm UTC](https://forum.ansible.com/t/use-role-default-argument-if-playbook-argument-is-undefined/26423/5 "2018-01-05T15:35:57Z")

</div>

> > Ok but this means the default value is duplicated 😑
> 
> 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.
