# Default variables in with\_items dict

**URL:** <https://forum.ansible.com/t/default-variables-in-with-items-dict/14862>\
**Category:** Ansible Project\
**Created:** [March 26, 2014, 10:24am UTC](https://forum.ansible.com/t/default-variables-in-with-items-dict/14862 "2014-03-26T10:24:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [March 26, 2014, 10:24am UTC](https://forum.ansible.com/t/default-variables-in-with-items-dict/14862/1 "2014-03-26T10:24:24Z")

</div>

I have a line like this, where “configs” variable is defined as a list of dicts/hashes:

template: src={{ item.name }}.j2 dest=“/etc/config/{{ item.target\_name|item.name }}”

with\_items: configs

It basically allows the generated filename to be customised, but fall back to the template name if none is provided. The problem is it doesn’t work, the generated filename is literally {{ item.target\_name|item.name }}. But it works fine without the default syntax (ie just {{ item.name }}). Is that expected behaviour and is there any workaround?

---

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [March 26, 2014, 10:41am UTC](https://forum.ansible.com/t/default-variables-in-with-items-dict/14862/2 "2014-03-26T10:41:11Z")

</div>

I thought of a workaround, but also doesn’t work:

- set\_fact: item.target\_name = item.name  
when: item.target\_name is not defined  
with\_items: configs

This way I could just always use item.target\_name. It runs on the correct items, but seems that set\_fact doesn’t work as I expected for an item dict, as item.target\_name is still undefined on the next task.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 26, 2014, 1:29pm UTC](https://forum.ansible.com/t/default-variables-in-with-items-dict/14862/3 "2014-03-26T13:29:21Z")

</div>

I think what you want, is to use the ‘default’ filter.

Such as:

template: src={{ item.name }}.j2 dest=“/etc/config/{{ item[‘target\_name’]|default(item.name) }}”

with\_items: configs

I also changed target\_name to not use dot notation, as that can sometimes still create issues with the default filter. In the above case, if item does not have a ‘target\_name’ key, it will default to ‘item.name’.

---

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [March 26, 2014, 4:55pm UTC](https://forum.ansible.com/t/default-variables-in-with-items-dict/14862/4 "2014-03-26T16:55:47Z")

</div>

I see, that worked fine, thanks.
