Here’s my playbook snippet:
- name: Get switch configuration
uri:
url: https://{{ ansible_host }}/api/v1/sites/{{ site_id }}/devices/{{ switch }}
method: GET
return_content: yes
headers:
Content-Type: "application/json"
Authorization: "Token {{ mist_token }}"
validate_certs: no
ignore_errors: yes
register: switch_configuration_response
- name: print switch_configuration_response
debug:
var: switch_configuration_response
- name: Parse JSON response
set_fact:
switch_configuration: "{{ switch_configuration_response.json }}"
- name: Define the target description
set_fact:
target_description: "INET_{{ link }}"
- name: Find the port key with the specified description
set_fact:
interface_name: "{{ item.key }}"
loop: "{{ switch_configuration.port_config | dict2items }}"
when: item.value.description == target_description
- name: print interface_name
debug:
var: interface_name
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: switch_configuration.port_config.{{ interface_name }}.usage
value: disabled
register: updated_switch_configuration
- name: print switch_configuration
debug:
var: switch_configuration
- name: print switch_configuration
debug:
var: updated_switch_configuration
Pay atention on this block:
- name: Update the fact
ansible.utils.update_fact:
updates:
- path: switch_configuration.port_config.{{ interface_name }}.usage
value: disabled
register: updated_switch_configuration
- name: print switch_configuration
debug:
var: switch_configuration
- name: print updated switch_configuration
debug:
var: updated_switch_configuration
When I run the playbook I expect that only the specific “usage” field is updated and the rest of the json remains untouched, but instead, it is creating a new key “switch_configuration” and putting all the values inside of it, as a redundance.
Output snippet:
TASK [print switch_configuration] **********************************************
ok: [JUNIPER_MIST] => {
"switch_configuration": {
"additional_config_cmds": [
""
],
"bgp_config": null,
"changed": true,
"created_time": 1706718685,
"deviceprofile_id": null,
"disable_auto_config": false,
"evpn_scope": null,
"evpntopo_id": null,
"failed": false,
"hw_rev": "D",
"id": "00000000-0000-0000-1000-bc0ffef85364",
"ip_config": {
"network": "default",
"type": "dhcp"
},
"mac": "bc0ffef85364",
"managed": true,
"map_id": null,
"model": "EX2300-24P",
"modified_time": 1720131177,
"name": "SW-MAG036-CORE1-TESTE2",
"networks": {},
"notes": "TESTE2",
"oob_ip_config": {
"type": "dhcp",
"use_mgmt_vrf": false
},
"org_id": "5d568cec-dcd9-4fe9-ad1a-65c93640f9ad",
"port_config": {
"ge-0/0/0": {
"critical": false,
"description": "INET_EBT",
"dynamic_usage": null,
"no_local_overwrite": true,
"usage": "vlan_52"
},
"ge-0/0/1": {
"critical": false,
"description": "INET_ALGAR",
"dynamic_usage": null,
"no_local_overwrite": true,
"usage": "vlan_51"
}
},
"port_usages": {},
"role": "",
"routing_policies": {},
"serial": "JY4823370892",
"site_id": "29ca1e1c-8958-4159-9568-f5070be1239a",
"st_ip_base": "",
"switch_config": {},
"tag_id": 3452277,
"tag_uuid": "5d568cec-dcd9-4fe9-ad1a-65c93640f9ad",
"type": "switch"
}
}
TASK [print updated switch_configuration] **********************************************
ok: [JUNIPER_MIST] => {
"updated_switch_configuration": {
"changed": true,
"failed": false,
"switch_configuration": {
"additional_config_cmds": [
""
],
"bgp_config": null,
"changed": true,
"created_time": 1706718685,
"deviceprofile_id": null,
"disable_auto_config": false,
"evpn_scope": null,
"evpntopo_id": null,
"failed": false,
"hw_rev": "D",
"id": "00000000-0000-0000-1000-bc0ffef85364",
"ip_config": {
"network": "default",
"type": "dhcp"
},
"mac": "bc0ffef85364",
"managed": true,
"map_id": null,
"model": "EX2300-24P",
"modified_time": 1720131177,
"name": "SW-MAG036-CORE1-TESTE2",
"networks": {},
"notes": "TESTE2",
"oob_ip_config": {
"type": "dhcp",
"use_mgmt_vrf": false
},
"org_id": "5d568cec-dcd9-4fe9-ad1a-65c93640f9ad",
"port_config": {
"ge-0/0/0": {
"critical": false,
"description": "INET_EBT",
"dynamic_usage": null,
"no_local_overwrite": true,
"usage": "vlan_52"
},
"ge-0/0/1": {
"critical": false,
"description": "INET_ALGAR",
"dynamic_usage": null,
"no_local_overwrite": true,
"usage": "disabled"
}
},
"port_usages": {},
"role": "",
"routing_policies": {},
"serial": "JY4823370892",
"site_id": "29ca1e1c-8958-4159-9568-f5070be1239a",
"st_ip_base": "",
"switch_config": {},
"tag_id": 3452277,
"tag_uuid": "5d568cec-dcd9-4fe9-ad1a-65c93640f9ad",
"type": "switch"
}
}
}
Pay attention on the new key “switch_configuration” created inside updated_switch_configuration. Any guesses on why is that happening?