# Update variable for each loop iteration?

**URL:** <https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045>\
**Category:** Ansible Project\
**Created:** [November 30, 2021, 2:11am UTC](https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045 "2021-11-30T02:11:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_T](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@Ron\_T](https://forum.ansible.com/u/Ron_T)\
**Post date:** [November 30, 2021, 2:11am UTC](https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045/1 "2021-11-30T02:11:10Z")

</div>

Hi, I’m trying to combine two dictionaries that have lists of hashes within a loop and then performa actions upon the result of the combined dictionaries.

The first dictionary has items that are common variables, and the second has items that are unique. How would I update the final\_list variable to get the list to loop over it?

Thanks!

group\_vars directory  
common\_vars:

- listname1: common  
name: animal  
path: home  
type: dog  
legs: 4

host\_vars directory  
unique\_vars:

- listname2: unique1  
name: casper  
breed: husky

- listname2: unique2  
name: rover  
breed: boxer

- listname3: unique3  
name: max  
breed: poodle

# here is the final\_list

final\_list: “[{{ common\_vars| combine(unique\_vars,recursive=True) }}]”

- name: create animal  
template:  
src: animal.j2  
dest: “{{ item.path }}/{{ item.name }}/dog.txt”  
with\_items:
- “{{ final\_list}}”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 30, 2021, 3:39am UTC](https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045/2 "2021-11-30T03:39:20Z")

</div>

“What works” and “what you should do” don’t always overlap. Here’s an idea:

---

<div class="post-metadata">

**Author:** ![Ron\_T](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@Ron\_T](https://forum.ansible.com/u/Ron_T)\
**Post date:** [November 30, 2021, 4:31pm UTC](https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045/3 "2021-11-30T16:31:58Z")

</div>

To provide more clarity.  
For the first iteration of the loop the data should be

listname1: common  
name: animal  
path: home  
type: dog  
legs: 4  
name: casper  
breed: husky

The second iteration would be

listname1: common  
name: animal  
path: home  
type: dog  
legs: 4

name: rover  
breed: boxer

The third

listname1: common  
name: animal  
path: home  
type: dog  
legs: 4

name: max  
breed: poodle

---

<div class="post-metadata">

**Author:** ![Ron\_T](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@Ron\_T](https://forum.ansible.com/u/Ron_T)\
**Post date:** [December 1, 2021, 10:28pm UTC](https://forum.ansible.com/t/update-variable-for-each-loop-iteration/36045/4 "2021-12-01T22:28:24Z")

</div>

Thanks, that is a great help, I’ll give it a try.

Also I stumbled across the product filter. Would it also work for this issue? I tried to incorporate it into your example but haven’t figured out the syntax so far.

loop: “{{ common\_vars | product( unique\_vars )| list }}”
