FreeKB - Ansible ansible_all_ipv4_addresses
Ansible - ansible_all_ipv4_addresses

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, 192.168.0.6 (the primary IP address) and 192.168.0.7 (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 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: 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 192.168.0.6/24 brd 192.168.0.255 scope global noprefixroute dynamic enp2s0
       valid_lft 26448sec preferred_lft 26448sec
    inet 192.168.0.7/24 brd 192.168.0.255 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
  tasks:
    - 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: [server1.example.com]: 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: [server1.example.com] => {
    "ansible_all_ipv4_addresses": {
        "192.168.0.6",
        "192.168.0.7"
    }
}

 



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 c907c in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |