NOTE: Original question expanded on here: Is there a way in Ansible to replace a dictionary value based on k:v lookup to another dictionary?
I have 3 dictionaries, the first 2 are k:v with string:integer type values; My 3rd dictionary is a k:v of string:string which I want to loop through first with dict #1 and to replace the k with the k:v and then the same with dict #2 but replacing v with k:v.
"dict_1": {
"office-core01": 85,
"office-core02": 86,
"office-fw01": 87,
"office-fw02": 88,
"office-server-sw01": 91,
"office-vpn01": 92,
"office-vpn02": 93
}
"dict_2": {
"con1": 129,
"con2": 130,
"con3": 131,
"con4": 132,
"con5": 133,
"con6": 134,
"con7": 135,
"con8": 136,
"con9": 137
}
"dict_3": {
"office-core01": "con1",
"office-core02": "con2",
"office-fw01": "con3",
"office-fw02": "con4",
"office-server-sw01": "con7",
"office-vpn01": "con5",
"office-vpn02": "con6"
}
In the end I need a dictionary of k:v pairs of integers; For example in the first iteration I need the hostnames/keys (office-core01) in dict_3 replaced with value from dict_1 (85) and then the 2nd run to replace the ports/values (con1) replaced with key from dict_2 (129) however using code supplied by Vladimir in original question complains about object of type 'int' has no len()
.
Include task (console-portid.yml):
---
- name: Replace Console Hostname ID
set_fact:
port_mapping: "{{ port_mapping | difference([item]) +
[dict(my_value | zip(my_keys))] }}"
vars:
my_key: "{{ item.keys() | list }}"
my_value: "{{ item.values() | list }}"
my_keys: "{{ my_key | map('regex_replace', port_id.key, port_id.value) | list }}"
loop: "{{ dict_3 | dict2items }}"
Invocation:
- name: Replace Device Console Ports ID
include_tasks: console-portid.yml
loop: "{{ dict_1 | dict2items }}"
loop_control:
loop_var: port_id