0
votes
---
- name: Consolidate output
  hosts: localhost
  gather_facts: no
  vars:
    data_set_1:
      host1:
        field1: '1'
        field2: '2'
      host2:
        field3: '3'
        field4: '4'
      host3:
        field1: '1'
        field3: '3'
      host4:
        field5: '5'
        field6: '6'
    data_set_2:
      - host1
      - host2
      - host3

I just need to parse through both the data sets and build the list of fields and the value for hosts in data set 2.

For example, data_set_2 has three hosts host1, 2 and 3. Corresponding data from data_set_1 for host1, 2 and 3 is host1: field1: '1' field2: '2' host2: field3: '3' field4: '4' host3: field1: '1' field3: '3'

From this, I just need to build the result like this.

result = {
  field1: "1",
  field2: "2",
  field3: "3",
  field4: "4"
}

How can I do this from ansible playbook?

1
Hi Anees, welcome to SO. This site is not a "write code for me" site; you will want to include the code you have tried and the error you are getting when you run it.mdaniel
Hints to where to look at in the documentation for possible ways to solve your problem: extracting values from containers, unique filter, json_query filter...Zeitounator

1 Answers

0
votes

combine dictionaries in the loop of the list created by map and extract. For example

- set_fact:
    result: "{{ result|default({})|combine(item) }}"
  loop: "{{ data_set_2|map('extract', data_set_1)|list }}"
- debug:
    var: result|to_nice_json

give (export ANSIBLE_STDOUT_CALLBACK=yaml)

  result|to_nice_json: |-
    {
        "field1": "1",
        "field2": "2",
        "field3": "3",
        "field4": "4"
    }