16

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": "admin@surfer190.com", 
        "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?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
tread
  • 10,133
  • 17
  • 95
  • 170

3 Answers3

26

Use dot notation

"{{ ansible_all_ipv4_addresses.0 }}"
tread
  • 10,133
  • 17
  • 95
  • 170
  • If you find yourself in a situation where this isn't working, check the array structure. VMWare disk info gets returned as an array, but the array index is a numeric string, so I had to do this to get it to work : "{{ disk_info.guest_disk_info['0'].backing_filename }}" in Ansible 2.9.5 – Joshua K Jun 05 '20 at 15:20
  • Remember that if a dictionary key has a dash in it `-`, you must quote and enclose it in brackets. Eg. `stats.output.0['rpc-reply'].statistics` – tread Feb 18 '21 at 05:28
8

This should work just like it would in Python. Meaning you can access the keys with quotes and the index with an integer.

  - set_fact:
      ip_address_1: "{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'][0] }}"
      ip_address_2: "{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'][1] }}"

  - name: Display 1st ipaddress
    debug:
      var: ip_address_1
  - name: Display 2nd ipaddress
    debug:
      var: ip_address_2
linuxdynasty
  • 376
  • 2
  • 5
0

I had this same challenge when trying to parse the result of a command in Ansible.

So the result was:

{
  "changed": true,
  "instance_ids": [
    "i-0a243240353e84829"
  ],
  "instances": [
    {
      "id": "i-0a243240353e84829",
      "state": "running",
      "hypervisor": "xen",
      "tags": {
        "Backup": "FES",
        "Department": "Research"
      },
      "tenancy": "default"
    }
    ],
    "tagged_instances": [],
  "_ansible_no_log": false
}

And I wanted to parse the value of state into the result register in the ansible playbook.

Here's how I did it:

Since the result is an hash of array of hashes, that is state is in the index (0) hash of the instances array, I modified my playbook to look this way:

---
- name: Manage AWS EC2 instance
  hosts: localhost
  connection: local
  # gather_facts: false
  tasks:
  - name: AWS EC2 Instance Restart
    ec2:
      instance_ids: '{{ instance_id }}'
      region: '{{ aws_region }}'
      state: restarted
      wait: True
    register: result

  - name: Show result of task
    debug:
      var: result.instances.0.state

I saved the value of the command using register in a variable called result and then got the value of state in the variable using:

result.instances.0.state

This time when the command ran, I got the result as:

TASK [Show result of task] *****************************************************
ok: [localhost] => {
    "result.instances.0.state": "running"
}

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143