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

stephen picture stephen · Apr 16, 2016 · Viewed 21.5k times · Source

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?

Answer

stephen picture stephen · Apr 17, 2016

Use dot notation

"{{ ansible_all_ipv4_addresses.0 }}"