I have a custom fact that is a list of dictionaries. I need to look at the value of each dictionary item and potentially change some of the values. below is an example of the fact, my loop looks at each item and queries an api for information related to bt_device and potentially change the value
[
{
"at_device": "sw",
"at_name": "GigabitEthernet0/5",
"at_type": "dcim.interface",
"bt_device": "sw2",
"bt_name": "GigabitEthernet1/0/2",
"bt_type": "dcim.interface"
},
{
"at_device": "sw",
"at_name": "GigabitEthernet0/8",
"at_type": "dcim.interface",
"bt_device": "sw3",
"bt_name": "GigabitEthernet1/0/5",
"bt_type": "dcim.interface"
}
]
so based on the query I need to change “sw3” to “fp3”
the below tasks work to show the data I want to change to on the screen, but I cannot make the leap to modifying the value in the fact as needed.
- name: load custom fact
ansible.builtin.set_fact:
custom_fact: "{{ custom_fact | default([]) + [{ 'at_device' : ansible_net_hostname, 'at_name' : item.key, 'at_type' : 'dcim.interface', 'bt_device' : item.value[0].host, 'bt_name' : item.value[0].port, 'bt_type' : 'dcim.interface'}] }}"
loop: "{{ ansible_facts | dict2items }}"
- name: modify custom fact via query
debug:
msg: "{{ query(NetBoxy stuff).device.name }}"
when: "query(NetBoxy stuff) | length > 0"
loop: "{{ custom_fact }}"
Thanks
I have attempted many things however I am new enough to have no idea how to get there.