Created
August 12, 2019 20:35
-
-
Save itdependsnetworks/70a32d5cf60c10110d94cce20fb4ae9c to your computer and use it in GitHub Desktop.
Various ways to restructure data in ansible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@c4cc2b4306fd:/tmp/data-manipulation# ansible-playbook -i localhost, data-manipulation.yml | |
PLAY [SHOW THE VARIOUS WAYS TO RESTRUCTURE DATA] *********************************************************************************************************************************************************** | |
TASK [100 - SET FACT TO NORMALIZE DATA] ******************************************************************************************************************************************************************** | |
ok: [localhost] => (item={u'destination_address': u'aabb.cc00.6000', u'vlan': u'153', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'ca03.3efb.0000', u'vlan': u'100', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'f44d.3063.34f3', u'vlan': u'22', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/35'}) | |
ok: [localhost] => (item={u'destination_address': u'2c6b.f553.9d80', u'vlan': u'254', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/48'}) | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": [ | |
"aabb.cc00.6000", | |
"ca03.3efb.0000", | |
"f44d.3063.34f3", | |
"2c6b.f553.9d80" | |
] | |
} | |
TASK [101 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": [ | |
"aabb.cc00.6000", | |
"ca03.3efb.0000", | |
"f44d.3063.34f3", | |
"2c6b.f553.9d80" | |
] | |
} | |
TASK [102 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": [ | |
"aabb.cc00.6000", | |
"ca03.3efb.0000", | |
"f44d.3063.34f3", | |
"2c6b.f553.9d80" | |
] | |
} | |
TASK [103 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [104 - SET FACT TO NORMALIZE DATA] ******************************************************************************************************************************************************************** | |
ok: [localhost] => (item={u'destination_address': u'aabb.cc00.6000', u'vlan': u'153', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'ca03.3efb.0000', u'vlan': u'100', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'f44d.3063.34f3', u'vlan': u'22', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/35'}) | |
ok: [localhost] => (item={u'destination_address': u'2c6b.f553.9d80', u'vlan': u'254', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/48'}) | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": [ | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"mac": "aabb.cc00.6000", | |
"vlan": "153" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"mac": "ca03.3efb.0000", | |
"vlan": "100" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/35", | |
"mac": "f44d.3063.34f3", | |
"vlan": "22" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/48", | |
"mac": "2c6b.f553.9d80", | |
"vlan": "254" | |
} | |
] | |
} | |
TASK [105 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": [ | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"mac": "aabb.cc00.6000", | |
"vlan": "153" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"mac": "ca03.3efb.0000", | |
"vlan": "100" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/35", | |
"mac": "f44d.3063.34f3", | |
"vlan": "22" | |
}, | |
{ | |
"device": "localhost", | |
"interface": "Fa1/0/48", | |
"mac": "2c6b.f553.9d80", | |
"vlan": "254" | |
} | |
] | |
} | |
TASK [106 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [107 - SET FACT TO NORMALIZE DATA] ******************************************************************************************************************************************************************** | |
ok: [localhost] => (item={u'destination_address': u'aabb.cc00.6000', u'vlan': u'153', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'ca03.3efb.0000', u'vlan': u'100', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'f44d.3063.34f3', u'vlan': u'22', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/35'}) | |
ok: [localhost] => (item={u'destination_address': u'2c6b.f553.9d80', u'vlan': u'254', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/48'}) | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": { | |
"2c6b.f553.9d80": { | |
"device": "localhost", | |
"interface": "Fa1/0/48", | |
"vlan": "254" | |
}, | |
"aabb.cc00.6000": { | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"vlan": "153" | |
}, | |
"ca03.3efb.0000": { | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"vlan": "100" | |
}, | |
"f44d.3063.34f3": { | |
"device": "localhost", | |
"interface": "Fa1/0/35", | |
"vlan": "22" | |
} | |
} | |
} | |
TASK [108 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [109 - SET FACT TO NORMALIZE DATA] ******************************************************************************************************************************************************************** | |
ok: [localhost] => (item={u'destination_address': u'aabb.cc00.6000', u'vlan': u'153', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'ca03.3efb.0000', u'vlan': u'100', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'f44d.3063.34f3', u'vlan': u'22', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/35'}) | |
ok: [localhost] => (item={u'destination_address': u'2c6b.f553.9d80', u'vlan': u'254', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/48'}) | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": { | |
"2c6b.f553.9d80": { | |
"destination_address": "2c6b.f553.9d80", | |
"destination_port": "Fa1/0/48", | |
"type": "DYNAMIC", | |
"vlan": "254" | |
}, | |
"aabb.cc00.6000": { | |
"destination_address": "aabb.cc00.6000", | |
"destination_port": "Fa1/0/36", | |
"type": "DYNAMIC", | |
"vlan": "153" | |
}, | |
"ca03.3efb.0000": { | |
"destination_address": "ca03.3efb.0000", | |
"destination_port": "Fa1/0/36", | |
"type": "DYNAMIC", | |
"vlan": "100" | |
}, | |
"f44d.3063.34f3": { | |
"destination_address": "f44d.3063.34f3", | |
"destination_port": "Fa1/0/35", | |
"type": "DYNAMIC", | |
"vlan": "22" | |
} | |
} | |
} | |
TASK [110 - SET FACT TO NORMALIZE DATA IN JINJA] *********************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [111 - SET FACT TO NORMALIZE DATA] ******************************************************************************************************************************************************************** | |
ok: [localhost] => (item={u'destination_address': u'aabb.cc00.6000', u'vlan': u'153', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'ca03.3efb.0000', u'vlan': u'100', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/36'}) | |
ok: [localhost] => (item={u'destination_address': u'f44d.3063.34f3', u'vlan': u'22', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/35'}) | |
ok: [localhost] => (item={u'destination_address': u'2c6b.f553.9d80', u'vlan': u'254', u'type': u'DYNAMIC', u'destination_port': u'Fa1/0/48'}) | |
TASK [debug] *********************************************************************************************************************************************************************************************** | |
ok: [localhost] => { | |
"mac_table_normalized": { | |
"2c6b.f553.9d80": { | |
"device": "localhost", | |
"interface": "Fa1/0/48", | |
"vlan": "254" | |
}, | |
"aabb.cc00.6000": { | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"vlan": "153" | |
}, | |
"ca03.3efb.0000": { | |
"device": "localhost", | |
"interface": "Fa1/0/36", | |
"vlan": "100" | |
}, | |
"f44d.3063.34f3": { | |
"device": "localhost", | |
"interface": "Fa1/0/35", | |
"vlan": "22" | |
} | |
} | |
} | |
PLAY RECAP ************************************************************************************************************************************************************************************************* | |
localhost : ok=20 changed=0 unreachable=0 failed=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: "SHOW THE VARIOUS WAYS TO RESTRUCTURE DATA" | |
hosts: "localhost" | |
connection: "local" | |
gather_facts: "no" | |
vars: | |
show_mac_address_table: | |
- destination_address: "aabb.cc00.6000" | |
destination_port: "Fa1/0/36" | |
type: "DYNAMIC" | |
vlan: "153" | |
- destination_address: "ca03.3efb.0000" | |
destination_port: "Fa1/0/36" | |
type: "DYNAMIC" | |
vlan: "100" | |
- destination_address: "f44d.3063.34f3" | |
destination_port: "Fa1/0/35" | |
type: "DYNAMIC" | |
vlan: "22" | |
- destination_address: "2c6b.f553.9d80" | |
destination_port: "Fa1/0/48" | |
type: "DYNAMIC" | |
vlan: "254" | |
tasks: | |
- name: "100 - SET FACT TO NORMALIZE DATA" | |
set_fact: | |
mac_table_normalized: "{{ mac_table_normalized | default([]) }} + [ '{{ item.destination_address }}' ]" | |
loop: "{{ show_mac_address_table }}" | |
- debug: var=mac_table_normalized | |
- name: "101 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: "{{ lookup('template', 'template1.j2') }}" | |
- debug: var=mac_table_normalized | |
- name: "102 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: "{{ lookup('template', 'template2.j2') }}" | |
- debug: var=mac_table_normalized | |
- name: "103 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: [] | |
- name: "104 - SET FACT TO NORMALIZE DATA" | |
set_fact: | |
mac_table_normalized: "{{ mac_table_normalized | default([]) + [ { 'device': inventory_hostname, 'mac': item.destination_address, 'vlan': item.vlan, 'interface': item.destination_port } ] }}" | |
loop: "{{ show_mac_address_table }}" | |
- debug: var=mac_table_normalized | |
- name: "105 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: "{{ lookup('template', 'template3.j2') }}" | |
- debug: var=mac_table_normalized | |
- name: "106 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: {} | |
- name: "107 - SET FACT TO NORMALIZE DATA" | |
set_fact: | |
mac_table_normalized: "{{ mac_table_normalized | default({}) | combine( {item.destination_address: { 'device': inventory_hostname, 'vlan': item.vlan, 'interface': item.destination_port }} )}}" | |
loop: "{{ show_mac_address_table }}" | |
- debug: var=mac_table_normalized | |
- name: "108 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: {} | |
- name: "109 - SET FACT TO NORMALIZE DATA" | |
set_fact: | |
mac_table_normalized: "{{ show_mac_address_table | rekey_on_member('destination_address') }}" | |
loop: "{{ show_mac_address_table }}" | |
- debug: var=mac_table_normalized | |
- name: "110 - SET FACT TO NORMALIZE DATA IN JINJA" | |
set_fact: | |
mac_table_normalized: {} | |
- name: "111 - SET FACT TO NORMALIZE DATA" | |
set_fact: | |
mac_table_normalized: "{{ lookup('template', 'template4.j2') }}" | |
loop: "{{ show_mac_address_table }}" | |
- debug: var=mac_table_normalized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@c4cc2b4306fd:/tmp/data-manipulation# cat template1.j2 | |
{% set mac_table_normalized = [] %} | |
{% for mac in show_mac_address_table %} | |
{% set _ = mac_table_normalized.append(mac["destination_address"]) %} | |
{% endfor %} | |
{{ mac_table_normalized }} | |
root@c4cc2b4306fd:/tmp/data-manipulation# cat template2.j2 | |
{% set mac_table_normalized = [] %} | |
{% for mac in show_mac_address_table %} | |
{% if mac_table_normalized.append(mac["destination_address"]) %}{% endif %} | |
{% endfor %} | |
{{ mac_table_normalized }} | |
root@c4cc2b4306fd:/tmp/data-manipulation# cat template3.j2 | |
{% set mac_table_normalized = [] %} | |
{% for mac in show_mac_address_table %} | |
{% set key_changer = { "device": inventory_hostname, "mac": mac["destination_address"], "vlan": mac["vlan"], "interface": mac["destination_port"] } %} | |
{% set _ = mac_table_normalized.append(key_changer) %} | |
{% endfor %} | |
{{ mac_table_normalized }} | |
root@c4cc2b4306fd:/tmp/data-manipulation# cat template4.j2 | |
{% set mac_table_normalized = {} %} | |
{% for mac in show_mac_address_table %} | |
{% set key_changer = { "device": inventory_hostname, "vlan": mac["vlan"], "interface": mac["destination_port"] } %} | |
{% set _ = mac_table_normalized.update({mac["destination_address"]: key_changer} ) %} | |
{% endfor %} | |
{{ mac_table_normalized }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment