How to get current target host's IP address in Ansible
Ansible is an open-source automation tool that simplifies the process of managing configuration, deployments, and orchestrating infrastructure. It uses a simple and declarative language called YAML to describe the desired state of systems and services, making it easy to automate repetitive tasks, increase efficiency, and ensure consistency across servers and applications.
Getting IP address in Ansible
In some scenarios, you may need to retrieve the IP address of the target host being managed by Ansible. This information can be useful for various tasks, such as configuring networking settings, generating dynamic inventories, or performing conditional operations based on the IP address.
In the following examples, we will explore two methods to obtain the IP address of the current target host in Ansible.
Using Python and ansible_playbook_python variable
In this method, we leverage Python to fetch the IP address of the localhost, and to ensure compatibility with different Ansible versions, we use the ansible_playbook_python variable to get the path to the Python interpreter. ansible_playbook_python is a configuration parameter in Ansible that specifies the path to the Python executable on the target machine.
---
- name: Get current target host's IP address using Python
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Fetch IP address using Python
shell: "{{ ansible_playbook_python }} -c 'import socket; print(socket.gethostbyname(socket.gethostname()))'"
register: ip_address_result
- name: Display the IP address
debug:
var: ip_address_result.stdout
Code explanation
Line 1–5: We define the Ansible playbook to get the IP address of the
localhostusing Python. The playbook runs on the localhost (hosts: localhost) and executes locally (connection: local). We disable gathering facts (gather_facts: no) since certain facts might not be available when executing locally.Line 7–10: We create a task named
Fetch IP address using Python. In this task, we use theshellmodule to execute a . ThePython one-liner In Python, a one-liner refers to a single line of code that accomplishes a particular task or operation. ansible_playbook_pythonvariable provides the path to the Python interpreter that Ansible uses for playbook execution. We use this Python interpreter to run the Python code that fetches the IP address usingsocket.gethostbyname(socket.gethostname()). The result of the Python command is registered in theip_address_resultvariable.Line 12–14: We create another task named
Display the IP address. In this task, we use thedebugmodule to print the value of theip_address_result.stdoutvariable. This variable contains the fetched IP address, which is displayed in the playbook output.
Using shell command with pipe character
The method utilizes the shell module and pipe character '|' in Ansible to run a shell command directly on the target host for fetching the IP address. The specific shell command hostname -I | awk '{print $1}' is used to retrieve a list of IP addresses assigned to the host, and then awk is used to extract the first IP address from the list.
---
- name: Get current target host's IP address using shell command
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Fetch IP address using shell command
vars:
command: "hostname -I | awk '{print $1}'"
shell: "{{ command }}"
register: ip_address_result
- name: Display the IP address
debug:
var: ip_address_result.stdout
Code explanation
Line 1–5: We define the Ansible playbook to get the IP address of the localhost using a shell command. The playbook runs on the localhost (
hosts: localhost) and executes locally (connection: local). We disable gathering facts (gather_facts: no) since certain facts might not be available when executing locally.Line 7–12: We create a task named
Fetch IP address using shell command.In this task, we use theshellmodule to execute a shell command. The command is specified in thevarssection, where we define thecommandvariable. The command consists of two parts:hostname -I, which retrieves a list of IP addresses assigned to the localhost, andawk '{print $1}', which uses theawkcommand to extract the first IP address from the list. The output of the command is registered in theip_address_resultvariable.Line 14–16: We create another task named
Display the IP address. In this task, we use thedebugmodule to print the value of theip_address_result.stdoutvariable. This variable contains the fetched IP address, which is displayed in the playbook output.
Conclusion
We explored two methods to retrieve the IP address of the target host using Ansible. Method 1 leverages Python and the ansible_playbook_python variable, while method 2 utilizes pipe character with a shell command.
Free Resources