In my payload, I have a variable that is actually a list of dictionaries, such as this one:
myvar:
- name: name1
ip_addresses:
- 10.10.10.10
- 11.11.11.11
nat_destination_addresses:
- host: 12.12.12.12
destination: 13.13.13.13
- host: 14.14.14.14
destination: 15.15.15.15
nat_source_address: 16.16.16.16
applications:
- protocol: tcp
port: 8302
- protocol: udp
port: 2000
- protocol: tcp
port: 2000-5600
- name: name2
ip_addresses:
- 17.17.17.17
- name: name3
ip_addresses:
- 18.18.18.18
- 19.19.19.19
All the values for each element in myvar
are optional, except for the name, which is mandatory.
I am trying to pad the ip addresses (ip_addresses
, nat_destination_addresses
and nat_source_address
) and ports. The ports should have a length of five characters with zeroes at the beginning (2000
becomes 02000
and 2000-5600
becomes 02000-05600
) and the ip addresses should have three characters for each subsection (18.18.18.18
becomes 018.018.018.018
).
The problem that I have is that I am not able to change only subsections of myvar
.
I have read other questions here, such as:
merging dictionaries in ansible
Using set_facts and with_items together in Ansible
But to no avail. No matter what I do, I am not able to keep the original dictionary, I end up with a list of ip_addresses if I use the combine
filter from the second StackOverflow link.
The expected result is the original myvar
variable with updated ip addresses and ports.