Get IP address, replace first three octets

473 views
Skip to first unread message

LJ Medina

unread,
Oct 20, 2018, 11:38:38 AM10/20/18
to Ansible Project
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?

Dick Visser

unread,
Oct 20, 2018, 3:52:38 PM10/20/18
to ansible...@googlegroups.com
I don't know the hponcfg module, but from
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/53e0e0d5-6ec2-408a-9af9-8dce2384caba%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

LJMedina

unread,
Oct 21, 2018, 12:09:51 AM10/21/18
to ansible...@googlegroups.com
Hello!
Unfortunately, we are using version 2.4, don't think upgrading is an option (at least at this point) any other ideas ?

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/ubGw-T7WwJI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Oct 21, 2018, 4:16:17 AM10/21/18
to ansible...@googlegroups.com
On Sunday, 21 October 2018 06:09:38 CEST LJMedina wrote:
> Hello!
> Unfortunately, we are using version 2.4, don't think upgrading is an option
> (at least at this point) any other ideas ?

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

- debug: msg="11.11.11.{{ ansible_default_ipv4.address.split('.').3 }}"


--
Kai Stian Olstad


Frank Thommen

unread,
Oct 21, 2018, 3:33:54 PM10/21/18
to ansible...@googlegroups.com
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



Kai Stian Olstad

unread,
Oct 21, 2018, 3:54:12 PM10/21/18
to ansible...@googlegroups.com
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

--
Kai Stian Olstad


Brad Van Orden

unread,
Oct 22, 2018, 7:11:26 AM10/22/18
to Ansible Project
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
#
# Get the current configuration
#
hponcfg --writeconfig ilo
#
# Delete the directory information and user information portions of the xml file.
#
sed -i '/^  <DIR_INFO/,/^  <\/DIR_INFO/d' ilo
sed -i '/^  <USER_INFO/,/^  <\/USER_INFO/d' ilo
#
# Compute your new ip address.  Note, OCT4 is based on RHEL 7 type OS using eth0 as the NIC.
# Adjust as needed.
#
BASE_IP=11.11.11.
OCT4=$(ifconfig eth0 | grep inet | awk '{ print $2 }' | cut -d. -f4)
#
# You will probably also want to change the netmask and default gateway.
# The netmask can be found at: SUBNET_MASK
# and the gateway at: GATEWAY_IP_ADDRESS
# Change accordingly.
#
sed -i "s|^\(    <IP_ADDRESS VALUE = \"\).*|\1${BASE_IP}${OCT4}\"/>|" ilo
#
# Write the new ip to the iLO
#
hponcfg --file ilo

LJ Medina

unread,
Oct 24, 2018, 11:55:29 PM10/24/18
to Ansible Project
Thank you all very much for your feedback! All the replies helped me solve the task.
cheers!
Reply all
Reply to author
Forward
0 new messages