Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to specify an array or list element fact with yaml?

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?

like image 910
tread Avatar asked Apr 16 '16 16:04

tread


People also ask

How do you set ansible facts?

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.

How do you define a variable in ansible?

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!

How do you use ansible gather facts?

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.


1 Answers

Use dot notation

"{{ ansible_all_ipv4_addresses.0 }}"
like image 88
tread Avatar answered Oct 09 '22 07:10

tread