So I have 2 json array files
one with some simple data (could be any number of servers):
[
{
"playbook": "simplerun.yml",
"server": "abc",
"status": "success"
},
{
"playbook": "simplerun.yml",
"server": "def",
"status": "success"
}
etc
]
and a master file (hundreds of thousands of servers) with enhanced data about the server (but is called Name)
[
{
"name": "abc",
"fqdn": "abc.com",
"env": "UAT",
"ip_address": "0.0.0.0",
"owner": "John Doe"
},
{
"name": "def",
"fqdn": "def.com",
"env": "PROD",
"ip_address": "0.0.0.1",
"owner": "Jane Doe"
}
etc
]
what I would like to have is
[
{
"playbook": "simplerun.yml",
"server": "abc",
"status": "success",
"fqdn": "abc.com",
"env": "UAT",
"ip_address": "0.0.0.0",
"owner": "John Doe"
},
{
"playbook": "simplerun.yml",
"server": "def",
"status": "success",
"fqdn": "def.com",
"env": "PROD",
"ip_address": "0.0.0.1",
"owner": "Jane Doe"
}
]
I know it must be simple, but
Any help is greatly appreciated.
I have tried combine
- name: set fact
set_fact:
key1: "server"
key2: "name"
- name: join data
set_fact:
joined_data: >-
{{ jsonfile1 | map ('combine',
jsonfile2 |selectattr (key2,'eq', item[key1]|list|first|default ({}))
}}
loop: "{{ jsonfile1 }}"
loop_control:
loop_var: item
however that only produced the same values for all the entries in the simple data.
2
Answers
As jq is tagged, you seem to be looking for a
JOIN
:Demo
If you want the
name
field removed from the output, replaceadd
withadd | del(.name)
.Read the files, for example
gives
Presumably, there might be more status items for the same server group the status items and create a dictionary
gives
Convert the list of names to a dictionary too. It is expected that the names are unique
gives
After you have converted the lists to dictionaries the search will be more efficient. Select names
gives the same items here in testing, but there are a lot more items in production
Convert the dictionary pb_status_group to a list and merge the lists by name
gives what want
Example of a complete playbook for testing