# Determining the primary IP address

**URL:** <https://forum.ansible.com/t/determining-the-primary-ip-address/35908>\
**Category:** Ansible Project\
**Created:** [October 21, 2021, 10:06pm UTC](https://forum.ansible.com/t/determining-the-primary-ip-address/35908 "2021-10-21T22:06:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [October 21, 2021, 10:06pm UTC](https://forum.ansible.com/t/determining-the-primary-ip-address/35908/1 "2021-10-21T22:06:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 21, 2021, 11:43pm UTC](https://forum.ansible.com/t/determining-the-primary-ip-address/35908/2 "2021-10-21T23:43:00Z")

</div>

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](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html)

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [October 22, 2021, 12:01am UTC](https://forum.ansible.com/t/determining-the-primary-ip-address/35908/3 "2021-10-22T00:01:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [October 22, 2021, 1:50am UTC](https://forum.ansible.com/t/determining-the-primary-ip-address/35908/4 "2021-10-22T01:50:53Z")

</div>

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. 🙂

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

–John
