When we check hostvars with:
- name: Display all variables/facts known for a host
debug: var=hostvars[inventory_hostname]
We get:
ok: [default] => {
"hostvars[inventory_hostname]": {
"admin_email": "[email protected]",
"admin_user": "root",
"ansible_all_ipv4_addresses": [
"192.168.35.19",
"10.0.2.15"
],...
How would I specify the first element of the "ansible_all_ipv4_addresses"
list?
The setup module in Ansible automatically discovers a standard set of facts about each host. If you want to add custom values to your facts, you can write a custom facts module, set temporary facts with a ansible. builtin. set_fact task, or provide permanent custom facts using the facts.
To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks. Here's a simple playbook example: - hosts: all vars: greeting: Hello world!
Ansible Facts List or Index. Here is the list of facts would be returned when you run the ansible hostgroup -m setup command against any host group. Ansible setup module does the same action what gathering_facts does during the ansible playbook execution. in fact gathering_facts use setup module to collect the facts.
Use dot notation
"{{ ansible_all_ipv4_addresses.0 }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With