Bootstrap FreeKB - Ansible - List all IP addresses using ansible_all_ipv4_addresses
Ansible - List all IP addresses using ansible_all_ipv4_addresses

Updated:   |  Ansible articles

There are a few different facts that capture a systems IP addresses.

  • ansible_all_ipv4_addresses - all of the IP addresses (default and secondary) bound to a systems network interfaces
  • ansible_default_ipv4 - the default IP address bound to a systems network interfaces
  • ansible_interfaces - the network interfaces being used by a system
  • ansible_eth0 - all of the IP addresses (default and secondary) bound to a systems eth0 network interface

For example, let's say you have the following playbook. 

---
- hosts: all
  tasks:
  - debug:
      var: ansible_all_ipv4_addresses

  - debug:
      var: ansible_default_ipv4

  - debug:
      var: ansible_default_ipv4.address

  - debug:
      var: ansible_interfaces

  - debug:
      var: ansible_eth0
...

 

Since the ansible_hostname will return the hostname from facts, gather_facts must be true. If gather_facts is false, something like this will be returned.

TASK [debug] 
fatal: [server1.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable.
The error was: 'ansible_hostname' is undefined\n\n
The error appears to be in '/home/john.doe/testing.yml': line 18, column 7, but may\n
be elsewhere in the file depending on the exact syntax problem.\n\n
The offending line appears to be:\n\n
  tasks:\n
    - debug:\n
      ^ here\n"}

 

Let's say the target server has more than one IP address. 

~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:a3:c9:ba brd ff:ff:ff:ff:ff:ff
    inet 10.11.12.13/24 brd 10.83.217.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.11.12.14/24 brd 10.83.217.255 scope global secondary eth0:0
       valid_lft forever preferred_lft forever
    inet 10.11.12.15/24 brd 10.83.217.255 scope global secondary eth0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fea3:c9ba/64 scope link 
       valid_lft forever preferred_lft forever

 

Assuming gather_facts is true, running this playbook should return something like this.

ok: [server1.example.com] => {
    "ansible_all_ipv4_addresses": [
        "10.11.12.13",
        "10.11.12.14",
        "10.11.12.15"
    ]
}

ok: [server1.example.com] => {
    "ansible_default_ipv4": {
        "address": "10.11.12.13",
        "alias": "eth0",
        "broadcast": "10.0.127.255",
        "gateway": "10.0.0.1",
        "interface": "eth0",
        "macaddress": "00:50:56:a3:c9:ba",
        "mtu": 1500,
        "netmask": "255.255.255.0",
        "network": "10.0.0.0",
        "prefix": "24",
        "type": "ether"
    }
}

ok: [server1.example.com] => {
    "ansible_default_ipv4.address": "10.11.12.13"
}

ok: [server1.example.com] => {
    "ansible_interfaces": [
        "lo",
        "eth0",
        "eth0_0"
    ]
}

ok: [server1.example.com] => {
    "ansible_eth0": {
        "active": true,
        "device": "eth0",
        "ipv4": {
            "address": "10.11.12.13",
            "broadcast": "10.0.127.255",
            "netmask": "255.255.255.0",
            "network": "10.0.0.0",
            "prefix": "24"
        },
        "ipv4_secondaries": [
            {
                "address": "10.11.12.14",
                "broadcast": "10.0.127.255",
                "netmask": "255.255.255.0",
                "network": "10.0.0.0",
                "prefix": "24"
            },
            {
                "address": "10.11.12.15",
                "broadcast": "10.0.127.255",
                "netmask": "255.255.255.0",
                "network": "10.0.0.0",
                "prefix": "24"
            }
        ]
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ea6f4c in the box below so that we can be sure you are a human.