tmgstevens
tmgstevens

Reputation: 114

How to reference sub dictionaries in Jinja2/Ansible?

I'm doing some work based on a provided YAML structure as follows:

cdh_services:

  - type: hdfs
    dfs_data_dir_list: /dfs/dn
    fs_checkpoint_dir_list: /dfs/snn
    dfs_name_dir_list: /dfs/nn
    dfs_journalnode_edits_dir: /dfs/jn

  - type: impala
    scratch_dirs: /tmp/impala

I'd like to have a way to refer to dfs_journalnode_edits_dir without relying on the place order of the services, so something like {{ cdh_services[type='hdfs']dfs_journalnode_edits_dir }}

Is there such a way to do this?

Upvotes: 0

Views: 74

Answers (1)

Alassane Ndiaye
Alassane Ndiaye

Reputation: 4767

Yes it's possible using the json_query filter. For example:

- hosts: all
  vars:
    cdh_services:
    - type: hdfs
      dfs_data_dir_list: /dfs/dn
      fs_checkpoint_dir_list: /dfs/snn
      dfs_name_dir_list: /dfs/nn
      dfs_journalnode_edits_dir: /dfs/jn
    - type: impala
      scratch_dirs: /tmp/impala
  tasks:
  - debug:
      msg: "{{ cdh_services | json_query(query) }}"
    vars:
      query: "[?type=='hdfs'].dfs_journalnode_edits_dir"

Upvotes: 1

Related Questions