PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 13, 2022

[FIXED] How to fetch certain value from Dictionary in ansible

 August 13, 2022     ansible, dictionary, json, list     No comments   

Issue

This is my list of dictionary

json:
    datasets:
    - createTime: '2020-01-10T18:18:29.010162Z'
      displayName: video_dataset
      name: projects/000111/locations/us-central1/datasets/123456789
    - createTime: '2020-01-10T18:18:29.010162Z'
      displayName: manual_dataset
      name: projects/000111/locations/us-central1/datasets/00556685
    - createTime: '2020-01-10T18:18:29.010162Z'
      displayName: wrong_dataset
      name: projects/000111/locations/us-central1/datasets/19967845

I am trying to fetch 5th element from "name" key if the list has displayName = video_dataset.

so the out put here would be 123456789.

Here is my ansible script and one of the method is using json_query:

- name: get dataset id
  set_fact : 
    dataset_ID: "{{ dataset_list.json.datasets | json_query([displayName=='video_dataset'].name.split('/')[5]) }}"

which gives me error saying,

Undefine Variable : 'displayName'

I tried several methods but no luck. Any advice would be appreciable.

Thanks in advance.


Solution

Q: "Fetch 5th element from 'name' key."

A: There is no split function in JmesPath. You can map the Ansible filter instead. For example,

dataset_ID: "{{ json.datasets|json_query(dataset_ID_query)|
                              map('split', '/')|
                              map(attribute=5)|
                              list }}"
dataset_ID_query: '[?displayName == `video_dataset`].name'

Notes

  • Example of a complete playbook
- hosts: localhost

  vars:
    json:
      datasets:
      - createTime: '2020-01-10T18:18:29.010162Z'
        displayName: video_dataset
        name: projects/000111/locations/us-central1/datasets/123456789
      - createTime: '2020-01-10T18:18:29.010162Z'
        displayName: manual_dataset
        name: projects/000111/locations/us-central1/datasets/00556685
      - createTime: '2020-01-10T18:18:29.010162Z'
        displayName: wrong_dataset
        name: projects/000111/locations/us-central1/datasets/19967845

    dataset_ID: "{{ json.datasets|json_query(dataset_ID_query)|
                                  map('split', '/')|
                                  map(attribute=5)|
                                  list }}"
    dataset_ID_query: '[?displayName == `video_dataset`].name'

  tasks:
    - debug:
        var: dataset_ID
    - debug:
        var: dataset_ID.0
    - debug:
        var: dataset_ID|first

gives (abridged)

dataset_ID:
  - '123456789'
dataset_ID.0: '123456789'
dataset_ID|first: '123456789'

  • Make the code more robust and use the filter last if you want to fetch the last element. For example, the declaration below gives the same result
dataset_ID: "{{ json.datasets|json_query(dataset_ID_query)|
                              map('split', '/')|
                              map('last')|
                              list }}"

  • Simplify the code and use the filter basename to get the last element from the path. For example, the declaration below gives the same result
dataset_ID: "{{ json.datasets|json_query(dataset_ID_query)|
                              map('basename')|
                              list }}"


Answered By - Vladimir Botka
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing