Ansible IP address variable – host part


I have the following problem:

I’m writing playbook for setting IP address on the command line in Ansible. Lets say 10.10.10.x. I need to get the last part of my public IP lets say x.x.x.15 and assign it to the private: Is there a variable for this? Can i capture some? I’ve tried to use something like:

But it is not what i need. It works, but only for a limited number of servers.

The whole thing should be like that:

and {{ var }} should be the host part of the public IP.


Thank you! Here’s my final solution:


For using it later in the playbook.


Instead of using a system utility you can use ansible facts though you will find that interface names will vary from server to server.

You specifically mentioned the last part of my public IP

If you really mean your public IP you will need to use an external service to get it since your server may behind a NAT. Here is one option

That will give your public IP, next to get the last octet you could do something like:

Leave a Reply