Get the public IP address of a network with Ansible

less than 1 minute read

June 2020


Short post on how to use Ansible to get the public IP address of the network you are currently using, querying the ipify api.

Now the public IP address is stored in the public_ip variable, it can be accessed and used in later tasks. The returned result is a dictionary with numerous keys and one of those keys is json which has the value of a dictionary. To access the IP address value requires access the nested dictionary: public_ip.json.ip.

The IP address can then be used in other tasks such as creating the inbound rules in an Azure Network Security Group to lock down ssh access to a VM to your IP address.