Skip to Content

2 ways to Get IP address with Ansible

This article is part of the following series.

 

Get IP address using fact variable with Ansible

If you want to get the IP address of a host using Ansible, you can use the ansible_default_ipv4 variable, which is a built-in variable that contains the IPv4 address of the host.

Here’s an example playbook that uses the ansible_default_ipv4 variable to get the IP address of a host:

---
- name: Get IP Address with Ansible
  hosts: my_host
  gather_facts: yes
  tasks:
    - name: Display IP Address
      debug:
        var: ansible_default_ipv4.address

In this example, the playbook assumes that you have a host called my_host defined in your inventory file or inventory script.

The gather_facts option is set to yes, which means that Ansible will gather facts about the host, including its IP address.

The ansible_default_ipv4.address variable is used in the debug module to display the IPv4 address of the host on the console.

Note that the ansible_default_ipv4 variable will only be available if facts are gathered for the host, which requires the gather_facts option to be set to yes.

If you prefer not to gather facts, you can also use other methods to obtain the IP address, such as using the lookup plugin with the network or host options, or using a custom task to run a command that retrieves the IP address.

Get IP address using shell command with Ansible

Here’s an example of an Ansible playbook that uses the shell module to list all network interfaces and their corresponding IP addresses on a remote host:

---
- name: Gather Interface Information
  hosts: servers
  gather_facts: no
  tasks:
    - name: List Network Interfaces
      shell: "ip addr show | grep '^[0-9]' | awk '{print $2}' | sed 's/://'"
      register: interface_result

    - name: Get IP Addresses
      shell: "ip addr show {{ item }} | grep 'inet ' | awk '{print $2}'"
      register: ip_result
      loop: "{{ interface_result.stdout_lines }}"


    - name: Display Interface List
      debug:
        msg: item.stdout
      with_items: "{{ip_result.results}}"

In this playbook, the first task uses the shell module to execute the ip addr show command on the remote host, which lists all network interfaces. We then use grep, awk, and sed to extract the interface names from the output and register the result as interface_result.

Next, the second task uses the shell module again to execute the ip addr show command with each interface name from interface_result using a loop. We then use grep and awk to extract the IP addresses of each interface and register the result as ip_result.

To learn how to use list in Ansible, you can refer to this article: Ansible Lists 101: A Beginner’s Guide to Managing and Manipulating Data

How to get the interface name with Ansible

Here’s an example playbook that demonstrates how to achieve this:

---
- name: Gather IP address and interface information
  hosts: 
  gather_facts: yes
  tasks:
    - name: Display IP address and interface information
      debug:
        var: ansible_interfaces
      tags: info

 

In this playbook, the “gather_facts” module is set to “yes”, which means it will collect facts about the target host during playbook execution. The “ansible_interfaces” fact contains a list of network interfaces on the target host.

Get the IP address associated with network interface with Ansible

The ansible_eth0.ipv4.address variable is used to retrieve the IP address associated with the eth0 network interface on the remote target system.

The following is an example of how you can use it in an Ansible playbook:

- name: Retrieve IP address of eth0 interface
  hosts: <your_target_host>
  gather_facts: yes
  tasks:
    - name: Display IP address of eth0 interface
      debug:
        var: ansible_eth0.ipv4.address

 

In this example, the ansible_eth0.ipv4.address variable will contain the IP address associated with the eth0 network interface on the remote target host, which can be accessed and displayed using the debug module.

Note that the actual network interface name and the variable name for retrieving the IP address may vary depending on the operating system and network configuration of your target system. It’s always recommended to verify the correct network interface name and variable name for your specific setup before using it in your Ansible playbooks.

Get interface name and IP address with Ansible

this playbook gathers facts about the network interfaces on the remote hosts, displays the list of interfaces, and then retrieves the IP addresses associated with each network interface. The output will show the network interface names and their respective IP addresses.

---
- name: Gather IP address and interface information
  hosts: servers
  gather_facts: yes
  tasks:
    - name: Display IP address and interface information
      debug:
        var: ansible_interfaces
      tags: info
    - name: get ip adddress for each network interface
      debug:
        var: "{{ 'ansible_'+item }}.ipv4.address"
      with_items: "{{ansible_interfaces}}"
  1. Task 1: Display IP address and interface information
    • This task uses the “debug” module to print the value of the “ansible_interfaces” variable, which contains a list of all the network interfaces on the remote host.
    • It is tagged with “info” so that it can be selectively executed using the “tags” option.
  2. Task 2: Get IP addresses for each interface
    • This task uses the “debug” module to print the value of the “ipv4.addresses” attribute for each network interface.
    • The “item” loop variable is used to loop over the list of network interfaces gathered in the previous task.
    • The variable name is dynamically constructed using Jinja2 templating as “{{ ‘ansible_’+item }}.ipv4.addresses”, which retrieves the IP addresses associated with each network interface.
    • It is also tagged with “info” for selective execution.

 

 

understanding facts in ansible

In Ansible,facts provide information about the state and properties of the target system, such as its hostname, operating system, network interfaces, installed packages, and more.

Facts are collected by Ansible during the “fact gathering” process, which occurs at the beginning of a playbook run, before any tasks are executed. Fact gathering can be enabled or disabled using the gather_facts option in the playbook or in the inventory. When fact gathering is enabled (gather_facts: yes), Ansible collects facts from the target hosts and makes them available as variables that can be used in playbooks and templates.

Ansible provides a set of built-in facts, such as ansible_hostname for the hostname of the target host, ansible_os_family for the family of the operating system, ansible_eth0.ipv4.address for the IPv4 address of the eth0 network interface, and many others. These facts can be referenced in playbooks and templates using Jinja2 syntax, such as {{ ansible_hostname }}, {{ ansible_os_family }}, or {{ ansible_eth0.ipv4.address }}.

Facts can also be used in conditionals, loops, and other expressions in playbooks, allowing you to dynamically adapt the configuration and behavior of tasks based on the state and properties of the target hosts.

Ansible also allows you to define and use custom facts, which are facts that you define yourself in your playbooks or roles. Custom facts can be useful for storing and retrieving additional information about hosts, or for encapsulating complex logic that generates dynamic values during playbook execution.

how to get all the facts in ansible

In Ansible, you can gather all available facts for a remote host using the gather_facts option in your playbook or inventory. When gather_facts is set to yes, Ansible will automatically collect all available facts about the remote host and make them available as variables that can be used in playbooks and templates.

Here’s an example playbook that gathers all facts for a remote host:

---
- name: Gather All Facts with Ansible
  hosts: my_host
  gather_facts: yes
  tasks:
    - name: Display All Facts
      debug:
        var: hostvars[inventory_hostname]

 

In this example, the playbook assumes that you have a host called my_host defined in your inventory file or inventory script. The gather_facts option is set to yes, which means that Ansible will gather all available facts for the host. The hostvars[inventory_hostname] variable is used in the debug module to display all the gathered facts for the host on the console.

You can also access individual facts using the corresponding variable names. For example, to access the hostname of the target host, you can use {{ ansible_hostname }} in your playbooks or templates. Similarly, other built-in facts such as ansible_os_family, ansible_distribution, ansible_interfaces, etc., can be accessed using their respective variable names.

Note that fact gathering can impact the performance and runtime of your playbook, as it involves additional communication and processing overhead. If you only need specific facts, you can also selectively gather facts using the gather_subset option, which allows you to specify a subset of facts to gather, or disable fact gathering entirely by setting gather_facts to no if you do not need any facts.