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 localhost using 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 the shell module to execute a Python one-linerIn Python, a one-liner refers to a single line of code that accomplishes a particular task or operation.. The ansible_playbook_python variable 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 using socket.gethostbyname(socket.gethostname()). The result of the Python command is registered in the ip_address_result variable.

  • Line 1214: We create another task named Display the IP address. In this task, we use the debug module to print the value of the ip_address_result.stdout variable. 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 the shell module to execute a shell command. The command is specified in the vars section, where we define the command variable. The command consists of two parts: hostname -I, which retrieves a list of IP addresses assigned to the localhost, and awk '{print $1}', which uses the awk command to extract the first IP address from the list. The output of the command is registered in the ip_address_result variable.

  • Line 14–16: We create another task named Display the IP address. In this task, we use the debug module to print the value of the ip_address_result.stdout variable. 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

Copyright ©2025 Educative, Inc. All rights reserved