Update variable for each loop iteration?

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}}”

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

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

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 }}”