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?

3 Answers 3

26

Use dot notation

"{{ ansible_all_ipv4_addresses.0 }}"
2
  • 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 5, 2020 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, 2021 at 5: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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.