Ansible - List assistance | 2 lists | 1 unique key value between the lists

260 Views Asked by At

First time posting, go gentle. Looking for some assistance on how to accomplish the following.

2 lists 1 shared key/value between the lists

looking how I can associate the all elements/(key/values of element?) of list1 to list2 based the one 1 shared key/value present in both lists using: usecase_key4 as they shared key / usecase_valuse4-A / B for the example data, the value changes but will be same for the data sets required to associate.

Appreciate any feedback

Revised based off of replies. I have tried some variations of [ selectattr and json_query ] unsure if that is even the correct approach.

"list-1": [
        {
            "list1_key1": "list1_value1-A"
            "list1_key2": "list1_value2-A"
            "list1_key3": "list1_value3-A"
            "usecase_key4": "usecase_value4-A"
            "list1_key5": "list1_value5-A"
            "list1_key6": "list1_value6-A"
            "list1_key7": "list1_value7-A"
            "list1_key8": "list1_value8-A"
            "list1_key9": "list1_value9-A"
            "list1_key10":"list1_value10-A"
        },
        {
            "list1_key1": "list1_value1-B"
            "list1_key2": "list1_value2-B"
            "list1_key3": "list1_value3-B"
            "usecase_key4": "usecase_value4-B"
            "list1_key5": "list1_value5-B"
            "list1_key6": "list1_value6-B"
            "list1_key7": "list1_value7-B"
            "list1_key8": "list1_value8-B"
            "list1_key9": "list1_value9-B"
            "list1_key10": "list1_value10-B"
        }
]


"list-2": [
        {
            "list2_key1": "list2_value1-A"
            "list2_key2": "list2_value2-A"
            "list2_key3": "list2_value3-A"
            "usecase_key4": "usecase_value4-A"
            "list2_key5": "list2_value5-A"
            "list2_key6": "list2_value6-A"
        },
        {
            "list2_key1": "list2_value1-B"
            "list2_key2": "list2_value2-B"
            "list2_key3": "list2_value3-B"
            "usecase_key4": "usecase-value4-B"
            "list2_key5": "list2_value5-B"
            "list2_key6": "list2_value6-B"
        }
]

Expected result:

"new_list": [
        {
            "list1_key1": "list1_value1-A"
            "list1_key2": "list1_value2-A"
            "list1_key3": "list1_value3-A"
            "usecase_key4": "usecase_value4-A"
            "list1_key5": "list1_value5-A"
            "list1_key6": "list1_value6-A"
            "list1_key7": "list1_value7-A"
            "list1_key8": "list1_value8-A"
            "list1_key9": "list1_value9-A"
            "list1_key10":"list1_value10-A"
            "list2_key1": "list2_value1-A"
            "list2_key2": "list2_value2-A"
            "list2_key3": "list2_value3-A"
            "list2_key5": "list2_value5-A"
            "list2_key6": "list2_value6-A"
        },
        {
            "list1_key1": "list1_value1-B"
            "list1_key2": "list1_value2-B"
            "list1_key3": "list1_value3-B"
            "usecase_key4": "usecase_value4-B"
            "list1_key5": "list1_value5-B"
            "list1_key6": "list1_value6-B"
            "list1_key7": "list1_value7-B"
            "list1_key8": "list1_value8-B"
            "list1_key9": "list1_value9-B"
            "list1_key10": "list1_value10-B"
            "list2_key1": "list2_value1-B"
            "list2_key2": "list2_value2-B"
            "list2_key3": "list2_value3-B"
            "list2_key5": "list2_value5-B"
            "list2_key6": "list2_value6-B"
        }
]
1

There are 1 best solutions below

0
On BEST ANSWER

you have a specific problem, this playbook does the job:

- name: testplaybook
  hosts: localhost
  gather_facts: no
  vars:
    list_1:
    - list1_key1: list1_value1-A
      list1_key2: list1_value2-A
      list1_key3: list1_value3-A
      usecase_key4: usecase_value4-A
      list1_key5: list1_value5-A
      list1_key6: list1_value6-A
      list1_key7: list1_value7-A
      list1_key8: list1_value8-A
      list1_key9: list1_value9-A
      list1_key10: list1_value10-A
    - list1_key1: list1_value1-B
      list1_key2: list1_value2-B
      list1_key3: list1_value3-B
      usecase_key4: usecase_value4-B
      list1_key5: list1_value5-B
      list1_key6: list1_value6-B
      list1_key7: list1_value7-B
      list1_key8: list1_value8-B
      list1_key9: list1_value9-B
      list1_key10: list1_value10-B
    list_2:
    - list2_key1: list2_value1-A
      list2_key2: list2_value2-A
      list2_key3: list2_value3-A
      usecase_key4: usecase_value4-A
      list2_key5: list2_value5-A
      list2_key6: list2_value6-A
    - list2_key1: list2_value1-B
      list2_key2: list2_value2-B
      list2_key3: list2_value3-B
      usecase_key4: usecase-value4-B
      list2_key5: list2_value5-B
      list2_key6: list2_value6-B
  tasks:
    - name: group
      set_fact:
        new_list: "{{ new_list | d([]) + [item.0 | combine(item.1)] }}"
      loop: "{{ list_1 | zip(list_2) }}"
    - name: group
      debug:
        var: new_list

result:

    "new_list": [
        {
            "list1_key1": "list1_value1-A",
            "list1_key10": "list1_value10-A",
            "list1_key2": "list1_value2-A",
            "list1_key3": "list1_value3-A",
            "list1_key5": "list1_value5-A",
            "list1_key6": "list1_value6-A",
            "list1_key7": "list1_value7-A",
            "list1_key8": "list1_value8-A",
            "list1_key9": "list1_value9-A",
            "list2_key1": "list2_value1-A",
            "list2_key2": "list2_value2-A",
            "list2_key3": "list2_value3-A",
            "list2_key5": "list2_value5-A",
            "list2_key6": "list2_value6-A",
            "usecase_key4": "usecase_value4-A"
        },
        {
            "list1_key1": "list1_value1-B",
            "list1_key10": "list1_value10-B",
            "list1_key2": "list1_value2-B",
            "list1_key3": "list1_value3-B",
            "list1_key5": "list1_value5-B",
            "list1_key6": "list1_value6-B",
            "list1_key7": "list1_value7-B",
            "list1_key8": "list1_value8-B",
            "list1_key9": "list1_value9-B",
            "list2_key1": "list2_value1-B",
            "list2_key2": "list2_value2-B",
            "list2_key3": "list2_value3-B",
            "list2_key5": "list2_value5-B",
            "list2_key6": "list2_value6-B",
            "usecase_key4": "usecase-value4-B"
        }
    ]
}