I have the nested dictionary below:
facter_networking: {"domain": "mylab.com", "fqdn": "mylab.com", "hostname": "mylab", "interfaces": {"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}
I can read mostly of the Keys values, but some of them I cannot.
for example:
print(myfile['facter_networking']['domain']) - it works.
print(myfile['facter_networking']['fqdn']) - it works.
print(myfile['facter_networking']['hostname']) - it works.
However if I do.
print(myfile['facter_networking']['interfaces']['ens192']['bindings']['address']) - It doens't work.
On the other hand if I do:
print(myfile['facter_networking']['interfaces']
I get as result:
{"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}
Any idea how can I access these values above?
{"bindings": [{"address":..has a list as value, not another dictionary. So you needmyfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'], assuming only one item.get_network = myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'] KeyError: 'ens192'