Ansible Jinja2: How to get nth value of nested dict?

marcohald

My Playbook looks like this

- set_fact:
    vcenter_cluster: "{{ cluster_info.clusters.values[cluster_input.user_input] }}"   

- debug:
    var:  vcenter_cluster

The data in cluster_info.clusters looks like this

ok: [localhost] => {
    "cluster_info.clusters": {

        "DMZ": {
            "datacenter": "dc",

        },
        "K8S": {
            "datacenter": "dc",

        },
        "Win": {
            "datacenter": "dc",
        }
    }
}

My goal is to set vcenter_cluster to K8S when cluster_input.user_input is 1.

U880D

A minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    cluster_info:
      clusters:
        DMZ:
          datacenter: dc
        K8S:
          datacenter: dc
        WIN:
          datacenter: dc

    user_input: 1

  tasks:

  - name: Show
    debug:
      msg: "{{ (cluster_info.clusters | list)[user_input] }}"

will result in the requested output of

TASK [Show] ******
ok: [localhost] =>
  msg: K8S

Please notice that under certain circumstances there might be a difference in where a list index can start counting with, 0 versus 1. As well that Ansible / Python probably will not maintain the order of the dictionary keys.

Thanks To

Further Reading

regarding the order of dictionary keys

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get dict key value format using Jinja2 in ansible using debug or set fact module

How to get interpolated value of variable in Ansible / Jinja2

Extract nested dict value with conditions from JSON using JINJA2 in Ansible

Get nested dict items using Jinja2 in Flask

Ansible/Jinja2 how to append key into list of dict

Ansible: How to create nested dictionary using Jinja2

How to get the sum of two defined variables in Jinja2 ansible

how to get nested undeclared variables in python jinja2

How to use with_nested on 2 dicts to get value in ansible?

Ansible nested loop and jinja2 filter

Yaml/Jinja2/Ansible Processing dict list with jinja template

How to get single value from jinja2 template?

default value for dictionary in jinja2 (ansible)

How can I save a nested dictionary as variable in jinja2 ansible?

How to combine 2 list based on a common key value in Ansible Jinja2

how to append to a list in jinja2 for ansible

Check if key exists in a dict in Jinja2 template on ansible

Problems with Jinja2 and ansible making a sub dict

Use Jinja2 dict as part of an Ansible modules options

Ansible + Jinja2 Loop - Dict object has no attribute

ansible - create list of dict using jinja2

How do I fetch a value from a var file using Jinja2 into an JSON file in Ansible

How to parse a nested dictionary with Jinja2

How to indent nested if/for statements in jinja2

How to compared a nested pillar key value in an if statement in jinja2 for saltstack

How to traverse a nested dict structure with Ansible?

Ansible; how to add list of dicts into nested dict?

how to use .get() in a nested dict?

Apply map filter to value of dictionary with ansible/jinja2