Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ansible - how to use json_query filter to extract all items equals to a value

Here is my json output:

{
"kind": [
    {
        "inventory": "",
        "inventory_sources": "",
        "job_templates": "",
        "workflow_job_templates": "104"
    },
    {
        "inventory": "",
        "inventory_sources": "",
        "job_templates": "114",
        "workflow_job_templates": ""
    },
    {
        "inventory": "24",
        "inventory_sources": "",
        "job_templates": "",
        "workflow_job_templates": ""
    },
    {
        "inventory": "",
        "inventory_sources": "108",
        "job_templates": "",
        "workflow_job_templates": ""
    }
]

}

I'd like to display all items name that contain a specific value. For instance, I'd like to display that the item name taht contain '104' is 'workflow_job_templates'

I tested some syntaxes without any success:

    - debug: 
      msg: "104 is {{kind|json_query(query)}}"
      vars:
        query: "[?*==`104`].workflow_job_templates"

I know it is a wrong way to do but can someone tell me how he'd do for himself?

question from:https://stackoverflow.com/questions/65903446/how-to-use-json-query-filter-to-extract-all-items-equals-to-a-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Update)

The task below

    - debug:
        msg: "{{ item }} {{ kind|
                            map('dict2items')|
                            map('json_query', query)|
                            flatten }}"
      loop: [104, 114, 108, 24]
      vars:
        query: "[?to_string(value) == to_string('{{ item }}')].key"

gives

  msg: 104 ['workflow_job_templates']
  msg: 114 ['job_templates']
  msg: 108 ['inventory_sources']
  msg: 24 ['inventory']

(For the record. Brute-force approach)

Create a unique list of the keys

    - set_fact:
        my_keys: "{{ my_keys|default([]) + item.keys()|list }}"
      loop: "{{ kind }}"
    - set_fact:
        my_keys: "{{ my_keys|unique }}"

gives

  my_keys:
  - inventory
  - inventory_sources
  - job_templates
  - workflow_job_templates

Create a dictionary with all values

    - set_fact:
        my_dict: "{{ my_dict|default({})|combine({item: values}) }}"
      loop: "{{ my_keys }}"
      vars:
        query: "[].{{ item }}"
        values: "{{ kind|json_query(query) }}"

gives

  my_dict:
    inventory:
    - ''
    - ''
    - '24'
    - ''
    inventory_sources:
    - ''
    - ''
    - ''
    - '108'
    job_templates:
    - ''
    - '114'
    - ''
    - ''
    workflow_job_templates:
    - '104'
    - ''
    - ''
    - ''

Then search the dictionary. For example

    - debug:
        msg: "{{ item }} {{ my_dict|dict2items|json_query(query) }}"
      loop: [104, 114, 108, 24]
      vars:
        query: "[?value[?contains(@, '{{ item }}')]].key"

gives

  msg: 104 ['workflow_job_templates']
  msg: 114 ['job_templates']
  msg: 108 ['inventory_sources']
  msg: 24 ['inventory']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...