Hello everybody,
I’ve been searching for a solution a longer time now and hope you can help me.
In our environment we have VM’s with two interfaces, each with several ip addresses and what I’d like to do is to get the interface-name by searching an ip address, just using the ansible-facts.
My test-machine has only one interface and the facts-structure looks like this if I’m right:
"ansible_facts": {
"ansible_interfaces": [
"lo",
"ens192",
"ens192_24"
]
"ansible_ens192": {
"active": true,
"device": "ens192",
"hw_timestamp_filters": [],
"ipv4": {
"address": "10.0.0.23",
"broadcast": "10.0.0.255",
"netmask": "255.255.254.0",
"network": "10.0.0.0"
},
"ipv4_secondaries": [
{
"address": "10.0.0.24",
"broadcast": "10.0.0.255",
"netmask": "255.255.255.0",
"network": "10.0.0.0"
}
],
}
What I’ve found until now is this solution:
- name: Get interface for ip
set_fact:
interface_of_ip: "{{ ansible_interfaces | map('regex_replace', '^', 'ansible_') | map('extract', vars) | selectattr('ipv4.address', 'match', '10\\.0\\.0\\.23') | map(attribute='device') | first}}"
But in our case the ip address I’m searching for is the secondary ip “10.0.0.24” and replacing “ipv4.address” with “ipv4_secondaries.address” doesn’t work.
The best solution for me would be, that I can search for an ip, no matter whether it is the first or a secondary ip and get the interface name “ens192” as result.
But for this actual situation it would suffice to get the interface name of a secondary ip address.
Thank you very much in advance
Best regards
Martin