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_default_ipv4 fact contains a managed nodes (e.g. the target system) primary network interface information, such as the default IP address, subnet mask, and default gateway. Refer to run tasks on the control node if you want to display the IPv4 address of your control node (your Ansible server). Or ansible_all_ipv4_addresses can be used to only return all of the IP addresses being used by the system.
For example, let's say you have the following playbook.
---
- hosts: all
tasks:
- debug:
var: ansible_default_ipv4
...
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. Notice in this example that only the primary (default) IP address is returned, 192.168.0.6.
TASK [debug]
ok: [server1.example.com] => {
"ansible_default_ipv4": {
"address": "192.168.0.6",
"alias": "eno16777984",
"broadcast": "192.168.0.255",
"gateway": "192.168.0.1",
"interface": "eno16777984",
"macaddress": "00:0c:29:fc:f5:79",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.0.0",
"type": "ether"
}
}
To only capture the IP address, you would append .address, like this.
---
- hosts: all
tasks:
- debug:
var: ansible_default_ipv4.address
...
The following should be returned.
TASK [debug]
ok: [server1.example.com] => {
"ansible_default_ipv4.address": "192.168.0.6"
}