# How can I set a default that includes information from a variable?

**URL:** <https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [September 6, 2017, 9:00pm UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423 "2017-09-06T21:00:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [September 6, 2017, 9:00pm UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423/1 "2017-09-06T21:00:24Z")

</div>

I’ve tried the following code:

`

- name: ensure users exist.  
when: item.value.state is defined  
user:  
state: “{{ item.value.state }}”  
name: “{{ item.value.username }}”  
comment: “{{ item.value.fullname }}”  
password: “{{ item.value.crypted\_pass }}”  
createhome: “yes”  
**home: “{{ item.value.home|default(/home/{{ item.value.username }}) }}”**  
shell: “{{ item.value.shell }}”  
uid: “{{ item.value.uid }}”  
with\_dict: “{{ aspects\_local\_users }}”  
tags:
- aspects\_local\_users  
`

Basically, if aspects\_local\_users.user.home is set, I want to use that value. Otherwise I want to use /home/.

As I expected, you can’t nest {{ }} to get the value out of item.value.username. I, obviously, can get the value if I just do this:

`home: "{{ blah|default(item.value.username) }}"`

But that leaves off the /home.

Note, I can just create a second task and run one when item.value.home is defined and the other when it isn’t. But I would like to just use one task. 🙂

Thanks for any ideas or suggestions!

I’m on Ansible 2.3.2.0 installed from Pip. Ubuntu 16.04 64bit. Testing against vagrant vms.

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [September 6, 2017, 11:56pm UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423/2 "2017-09-06T23:56:19Z")

</div>

David -

I haven’t tried doing exactly what you’re trying to do, but I did something similar. I’ve changed the variable names to protect the innocent):

- name: remember variables  
set\_fact:  
myvar: “{{var1}}{{item.path|basename| regex\_replace(‘^’ + version\_regex + ‘$’, ‘\1’) }}” #regex parses version from jar file name

with\_items: “{{output.files}}”

Maybe that’ll give you something to go off?

-Adam

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [September 7, 2017, 12:33am UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423/3 "2017-09-07T00:33:38Z")

</div>

I probably should have explained that “version\_regex” is a variable defined in roles/role/vars/main.yml.

-Adam

---

<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:** [September 7, 2017, 1:13pm UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423/4 "2017-09-07T13:13:35Z")

</div>

Correct syntax should be:

home: "{{ item.value.home | default('/home/' + item.value.username) }}"

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [September 7, 2017, 6:45pm UTC](https://forum.ansible.com/t/how-can-i-set-a-default-that-includes-information-from-a-variable/25423/5 "2017-09-07T18:45:00Z")

</div>

Thank you! That worked perfect. 🙂
