Ansible - ansible_all_ipv4_addresses fact

The ip address command can be used to display a systems IP addresses.

ip address


Something like this should be returned. Notice in this example there are two IP addresses, (the primary IP address) and (the secondary IP address).

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 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether d0:50:99:d0:99:ef brd ff:ff:ff:ff:ff:ff
    inet brd scope global noprefixroute dynamic enp2s0
       valid_lft 26448sec preferred_lft 26448sec
    inet brd scope global secondary noprefixroute enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::d250:99ff:fed0:99ef/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether d0:50:99:d0:99:ee brd ff:ff:ff:ff:ff:ff


The ansible_all_ipv4_addresses fact contains a managed nodes (e.g. the target system) IP addresses. Refer to run tasks on the control node if you want to display the IPv4 addresses of your control node (your Ansible server). Or ansible_default_ipv4 can be used to only return the primary (default) IPv4 address.

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

- hosts: all
    - debug:
        var: ansible_all_ipv4_addresses


If you get the following output, the gather_facts module may be set to false. gather_facts must be set to true, or not defined at all in your playbook, as gather_facts is true by default.

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


Running this playbook should return something like this.

TASK [debug]
ok: [] => {
    "ansible_all_ipv4_addresses": {


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

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


Web design by yours truely - me, myself, and I   |   |