# Get IP address, replace first three octets

**URL:** <https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836>\
**Category:** Ansible Project\
**Created:** [October 20, 2018, 3:38pm UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836 "2018-10-20T15:38:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LJ\_Medina](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@LJ\_Medina](https://forum.ansible.com/u/LJ_Medina)\
**Post date:** [October 20, 2018, 3:38pm UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/1 "2018-10-20T15:38:38Z")

</div>

Hello,  
I'm trying to assign ILO IP addresses to a range of systems, using a different subnet than their host IP, just matching the fourth octet. For example, if their IP would be 10.10.10.10, I want their ILO IP to be 11.11.11.10.( Plan is to use "hponcfg" module to configure the host's ILO after I obtain this address)  
What is the best way to achieve this with ansible?

---

<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 20, 2018, 7:52pm UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/2 "2018-10-20T19:52:15Z")

</div>

I don't know the hponcfg module, but from  
[https://docs.ansible.com/ansible/latest/modules/hponcfg\_module.html#examples](https://docs.ansible.com/ansible/latest/modules/hponcfg_module.html#examples)  
it looks like you have to first copy an XML fragment with some  
specific configuration code, and then use that with the hponcfg  
module.  
If you are able to use ansible 2.7, then you can use this expression  
for the new ILO IP in your XML fragment:

{{ ansible\_default\_ipv4.address | ipmath(2\*\*24 + 2\*\*16 + 2\*\*8) }}"

This basically increases the first 3 octets by 1, by adding 2^24,  
2^16, and 2^8 to them.  
If you don't like your co-workers, use:

{{ ansible\_default\_ipv4.address | ipmath(16843008) }}"

Dick

---

<div class="post-metadata">

**Author:** ![LJ\_Medina](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@LJ\_Medina](https://forum.ansible.com/u/LJ_Medina)\
**Post date:** [October 21, 2018, 4:09am UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/3 "2018-10-21T04:09:38Z")

</div>

Hello!  
Unfortunately, we are using version 2.4, don’t think upgrading is an option (at least at this point) any other ideas ?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 21, 2018, 8:16am UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/4 "2018-10-21T08:16:05Z")

</div>

What are the part that you struggle with?  
For the IP you can do something like this

&nbsp;&nbsp;- debug: msg="11.11.11.{{ ansible\_default\_ipv4.address.split('.').3 }}"

---

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [October 21, 2018, 7:33pm UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/5 "2018-10-21T19:33:21Z")

</div>

sorry, dummy question: But where does the "split" come from? It's not a YAML statement, its not a standard Jinja2 filter and its not one of the special ansible Jinja2 filters. I'm confused.

frank

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 21, 2018, 7:54pm UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/6 "2018-10-21T19:54:00Z")

</div>

Jinja is a template language for Python.  
I guess a Jinja strings has some, if not all the method a Python string object has  
[https://docs.python.org/2/library/stdtypes.html#str.split](https://docs.python.org/2/library/stdtypes.html#str.split)

---

<div class="post-metadata">

**Author:** ![Brad2](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brad2](https://forum.ansible.com/u/Brad2)\
**Post date:** [October 22, 2018, 11:11am UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/7 "2018-10-22T11:11:26Z")

</div>

Not an ansible response (use command or shell to execute), but can retrieve the iLO network settings and change with:

`

# This is coded against iLO4!

# Run as root

cd /var/tmp # or where ever you would like to have your temporary files

---

<div class="post-metadata">

**Author:** ![LJ\_Medina](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@LJ\_Medina](https://forum.ansible.com/u/LJ_Medina)\
**Post date:** [October 25, 2018, 3:55am UTC](https://forum.ansible.com/t/get-ip-address-replace-first-three-octets/28836/8 "2018-10-25T03:55:29Z")

</div>

Thank you all very much for your feedback! All the replies helped me solve the task.  
cheers!
