Reputation: 925
I have two ansible facts, one that looks like this:
"ansible_facts": "int_table" {
"[{ "connection": "notconnected",
"port": "eth1"},
{ "connection": "connected",
"port": "eth2"}]"
and
"ansible_facts": "mac_table" {
"[{ "mac_address": "0000.c200.0101",
"port": "eth1"},
{ "mac_address": "0320.c500.0201",
"port": "eth2"}]"
I want to create a new fact that would combine the two by their port, so that it would output
"ansible_facts": "new_table" {
"[{ "mac_address": "0000.c200.0101",
"connection": "notconnected",
"port": "eth1"},
{ "mac_address": "0320.c500.0201",
"connection": "connected",
"port": "eth2"}]"
Is this possible with pure ansible? I've tried passing both to a custom filter to use python to combine the two but can't seem to pass two facts to the same filter.
Upvotes: 1
Views: 1129
Reputation: 6685
here is how to do it:
get the list of ports from 1 variable, say the int_table
, they should be unique ports (i.e only one element in eaach list can have eth1, eth2, etc)
for each of these ports, find the element from int_table
and combine it with the respective from mac_table
print the final list variable
playbook:
---
- hosts: localhost
gather_facts: false
vars:
int_table:
- connection: notconnected
port: eth1
- connection: connected
port: eth2
mac_table:
- mac_address: 0000.c200.0101
port: eth1
- mac_address: 0320.c500.0201
port: eth2
tasks:
- name: populate merged list
set_fact:
final_var: "{{ final_var | default([]) + [int_table | selectattr('port','equalto', item) | first | combine(mac_table | selectattr('port','equalto', item) | first)] }}"
with_items:
- "{{ int_table | map(attribute='port') | list }}"
- name: print merged list
debug:
var: final_var
sample output:
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [populate merged list] ********************************************************************************************************************************************************************************************
ok: [localhost] => (item=eth1)
ok: [localhost] => (item=eth2)
TASK [print merged list] ***********************************************************************************************************************************************************************************************
ok: [localhost] => {
"final_var": [
{
"connection": "notconnected",
"mac_address": "0000.c200.0101",
"port": "eth1"
},
{
"connection": "connected",
"mac_address": "0320.c500.0201",
"port": "eth2"
}
]
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[http_offline@greenhat-29 tests]$
hope it helps
Upvotes: 3