'Ansible: get current target host's IP address
How do you get the current host's IP address in a role?
I know you can get the list of groups the host is a member of and the hostname of the host but I am unable to find a solution to getting the IP address.
You can get the hostname by using {{inventory_hostname}} and the group by using {{group_names}}
I have tried things like {{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }}
and ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}"
Solution 1:[1]
You can get the IP address from hostvars, dict ansible_default_ipv4 and key address
hostvars[inventory_hostname]['ansible_default_ipv4']['address']
and IPv6 address respectively
hostvars[inventory_hostname]['ansible_default_ipv6']['address']
An example playbook:
---
- hosts: localhost
tasks:
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv6']['address']
Solution 2:[2]
You can use in your template.j2 {{ ansible_eth0.ipv4.address }} the same way you use {{inventory_hostname}}.
ps: Please refer to the following blogpost to have more information about HOW TO COLLECT INFORMATION ABOUT REMOTE HOSTS WITH ANSIBLE GATHERS FACTS .
'hoping it’ll help someone one day ?
Solution 3:[3]
Just use ansible_ssh_host variable
playbook_example.yml
- hosts: host1
tasks:
- name: Show host's ip
debug:
msg: "{{ ansible_ssh_host }}"
hosts.yml
[hosts]
host1 ansible_host=1.2.3.4
Result
TASK [Show host's ip] *********************************************************************************************************************************************************************************************
ok: [host1] => {
"msg": "1.2.3.4"
}
Solution 4:[4]
Simple debug command:
ansible -i inventory/hosts.yaml -m debug -a "var=hostvars[inventory_hostname]" all
output:
"hostvars[inventory_hostname]": {
"ansible_check_mode": false,
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_host": "192.168.10.125",
"ansible_inventory_sources": [
"/root/workspace/ansible-minicros/inventory/hosts.yaml"
],
"ansible_playbook_python": "/usr/bin/python2",
"ansible_port": 65532,
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.8.5",
"major": 2,
"minor": 8,
"revision": 5,
"string": "2.8.5"
},
get host ip address:
ansible -i inventory/hosts.yaml -m debug -a "var=hostvars[inventory_hostname].ansible_host" all
zk01 | SUCCESS => {
"hostvars[inventory_hostname].ansible_host": "192.168.10.125"
}
Solution 5:[5]
Another way to find public IP would be to use uri module:
- name: Find my public ip
uri:
url: http://ifconfig.me/ip
return_content: yes
register: ip_response
Your IP will be in ip_response.content
Solution 6:[6]
If you want the external public IP and you're in a cloud environment like AWS or Azure, you can use the ipify_facts module:
# TODO: SECURITY: This requires that we trust ipify to provide the correct public IP. We could run our own ipify server.
- name: Get my public IP from ipify.org
ipify_facts:
This will place the public IP into the variable ipify_public_ip.
Solution 7:[7]
http://docs.ansible.com/ansible/latest/plugins/lookup/dig.html
so in template, for e. g.:
{{ lookup('dig', ansible_host) }}
Notes:
- Since not only DNS name could be used in inventory a check if it's not IP already better be added
- Obviously enough this receipt wouldn't work as intended for indirect host specifications (like using jump hosts, for e. g.)
But still it serves 99 % (figuratively speaking) of use cases.
Solution 8:[8]
Plain ansible_default_ipv4.address might not be what you think in some cases, use:
ansible_default_ipv4.address|default(ansible_all_ipv4_addresses[0])
Solution 9:[9]
The following snippet will return the public ip of the remote machine and also default ip(i.e: LAN)
This will print ip's in quotes also to avoid confusion in using config files.
>> main.yml
---
- hosts: localhost
tasks:
- name: ipify
ipify_facts:
- debug: var=hostvars[inventory_hostname]['ipify_public_ip']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- name: template
template:
src: debug.j2
dest: /tmp/debug.ansible
>> templates/debug.j2
public_ip={{ hostvars[inventory_hostname]['ipify_public_ip'] }}
public_ip_in_quotes="{{ hostvars[inventory_hostname]['ipify_public_ip'] }}"
default_ipv4={{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}
default_ipv4_in_quotes="{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"
Solution 10:[10]
In my case, I had needed to keep the IP and then run a command using delegate_to, so I wrote something as follows in variables:
leader_ip: '{{ansible_ssh_host}}'
and then I used the leader_ip.
the other solution is to use hostvar:
hostvars[INVENTORY_HOSTNAME]['ansible_host']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pasi H |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | NOZUONOHIGH |
| Solution 5 | chava |
| Solution 6 | Simon Woodside |
| Solution 7 | |
| Solution 8 | RafalS |
| Solution 9 | |
| Solution 10 |
