FreeKB - ansible_default_ipv4.address variable
Ansible - ansible_default_ipv4.address variable

The ansible_default_ipv4.address variable will return the default IPV4 address of your Ansible server. For example, let's say you have the following playbook.

---
- hosts: all
  tasks:
    - debug:
        msg: "{{ ansible_default_ipv4.address }}"

 

Running this playbook should return the IPv4 address of the Ansible server.

TASK [debug]
ok: [server1.example.com] => {
    "msg: "10.1.2.3"
}

 

You could also use hostvars to return the distribution of the target server. For example, let's say you have the following playbook.

---
- hosts: all
  tasks:
    - debug:
        msg: "{{ hostvars[inventory_hostname].ansible_default_ipv4.address }}"

 

Running this playbook should return the IPv4 address of the target server

TASK [debug]
ok: [server1.example.com] => {
    "msg: "192.168.1.2"
}

 



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




Comments