"What works" and "what you should do" don't always overlap. Here's an idea:

---
- name: Combining common list with unique lists
  hosts: localhost
  vars:

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

    unique_vars:
      - listname2: unique1
        name: casper
        breed: husky

      - listname2: unique2
        name: rover
        breed: boxer

      - listname3: unique3
        name: max
        breed: poodle

  tasks:
    - name: combine lists
      set_fact:
        final_list: "{%- set foo = [] -%}
                     {%- for item in unique_vars -%}
                     {%- set _ = foo.append( 
common_vars|combine(item,recursive=True) ) -%}
                     {%- endfor -%}{{ foo }}"

    - name: create animal
      copy:
        content: "{{ item|to_yaml() }}"
        dest: "./dogs-{{ item['path'] }}-{{ item['name'] }}-dog.txt"
      with_items:
        - "{{ final_list}}"


On Monday, November 29, 2021 at 9:11:10 PM UTC-5 ronte...@gmail.com wrote:

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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/7b0a10bc-adb5-4bee-a0c9-457a300d9c84n%40googlegroups.com.

Reply via email to