Determining the primary IP address

Good afternoon.
I have this snippet of code, that sets a proxy variable if the default ipv4 IP address is in 10 net space. I had read somewhere that using the ansible_default_ipv4 fact, wasn’t always reliable. So is there a better way to grab the primary IP other than what I did below?

  • name: PreTask | Configure proxy
    set_fact:
    _zabbix_proxy: “{{zabbix_http_proxy}}”
    when: hostvars[inventory_hostname][‘ansible_default_ipv4’][‘address’] | select(‘match’, ‘^10.[0-9]+’)

Thanks for the help!!

–John

I don’t know about any such unreliability.
As for determining if the address is in a specific prefix, I would definitely use the ipaddr filter:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html

I hadn’t heard about any inconsistencies either, which is why I figured I’d ask here. I thought about the ipaddr filter, but you need to install a python module for it to work. (netaddr I think) and I was just trying to figure it out using the base set of modules, etc. Thanks for the info though, I appreciate it

–John

Ok… so some more info… I think I have a workable solution. I"ll be testing it tomorrow, but I am 99% sure I have it figured out. The code above, actually didn’t work as I thought… so, I changed this:

when: hostvars[inventory_hostname][‘ansible_default_ipv4’][‘address’] | select(‘match’, ‘^10.[0-9]+’)

to this:
when: hostvars[inventory_hostname][‘ansible_default_ipv4’][‘address’] | regex_search(‘^10.[0-9]+’)

That seems to have gotten me the result I was looking for.

As for the “default ipv4” not being consistent, I went back and re-read the article. Basically, the author said that if you don’t set a default route, then the default.ipv4.address will be empty. I decided this was not a huge deal, since I can’t think of a situation where I have installed a server without a default route. :slight_smile:

So anyway, thanks again for the help, I do appreciate it.

–John