Come si ottiene l'indirizzo IP dell'host corrente in un ruolo?
So che è possibile ottenere l'elenco dei gruppi di cui l'host è membro e il nome dell'host, ma non riesco a trovare una soluzione per ottenere l'indirizzo IP.
Puoi ottenere l'hostname usando {{inventario_hostname}}
e il gruppo usando {{nome_gruppo}}
Ho provato cose come {{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }}
e ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}"
Una lista di tutti gli indirizzi è memorizzata in un fatto ansible_all_ipv4_addresses
, un indirizzo predefinito in ansible_default_ipv4.address
.
---
- hosts: localhost
connection: local
tasks:
- debug: var=ansible_all_ipv4_addresses
- debug: var=ansible_default_ipv4.address
Poi ci sono indirizzi assegnati ad ogni interfaccia di rete... In questi casi puoi visualizzare tutti i fatti e trovare quello che ha il valore che vuoi usare.
Puoi ottenere l'indirizzo IP da hostvars
, dict ansible_default_ipv4
e chiave address
.
hostvars[inventory_hostname]['ansible_default_ipv4']['address']
e indirizzo IPv6 rispettivamente
hostvars[inventory_hostname]['ansible_default_ipv6']['address']
Un esempio di playbook:
---
- hosts: localhost
tasks:
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv4']['address']
- debug: var=hostvars[inventory_hostname]['ansible_default_ipv6']['address']
Potete usare nel vostro template.j2 {{ ansible_eth0.ipv4.address }}
allo stesso modo in cui usate {{inventario_hostname}}
.
ps: Fate riferimento al seguente blogpost per avere maggiori informazioni su COME RACCOGLIERE INFORMAZIONI SUGLI OSPITI REMOTI CON FATTI DI GATHERS ANSIBILI .
'sperando che possa aiutare qualcuno un giorno ッ