简体   繁体   English

ANSIBLE - Json_Query - 过滤结果

[英]ANSIBLE - Json_Query - Filter result

Inside my ansible playbook, I' m trying to filter a json result but for now, it doesn't work.在我的 ansible 剧本中,我正在尝试过滤 json 结果,但目前它不起作用。

1/ Playbook 1/ 剧本
Below My ansible playbook to query and filter the result:在 My ansible playbook 下方查询和过滤结果:


tasks:
    - name: "Query Attributes" 
          ...
          ...
      register: query_result

    - name: Display Result
      debug: 
        var: query_result  

    - name: Display Filtered Result
      debug:
        msg: "{{ query_result.current| json_query('[].context.attributes[?name==`prod`].name') }}" 

Maybe an issue inside my json_query filter?也许我的 json_query 过滤器中有问题?
Any idea?任何想法?


2/ Query_result output before filtering 2/ 过滤前Query_result output

TASK [Display Result] ***************************************************
"query_result": {
                    "ansible_facts": {
                        "discovered_interpreter_python": "/usr/bin/python"
                    }, 
                    "changed": false, 
                    "current": [
                        {
                            "context": {
                                "attributes": {
                                    "name": "prod", 
                                    "uid": "11756"
                                }
                            }
                        }, 
                        {
....
                                }
                            }
                        }, 
                        {
                            "context": {
                                "attributes": {
                                    "name": "dev", 
                                    "uid": "14424"
                                }
                            }
                        }
                    ], 
                    "failed": false
                }
            }

*****************************      

3/ Filtered result is empty 3/ 过滤结果为空
Unfortunately my result is empty.不幸的是,我的结果是空的。

    TASK [Display Filtered Result] **********************************************************    
{    
                "msg": []    
            }    

Thank you谢谢
Ju

First of all, single and double quotes matter in jmespath expressions specification .首先,单引号和双引号在jmespath 表达式规范中很重要。 Your literal string for the search needs to be single quoted.您的搜索文字字符串需要单引号。

Second, your filter will not match.其次,您的过滤器将不匹配。 You either have to move your filter at a higher level in the the stream processing or to pipe your expression.您要么必须在 stream 处理或 pipe 处理中将过滤器移动到更高级别。

Below are some examples that illustrate the above recommendations and lead to the same result以下是一些示例,说明了上述建议并导致相同的结果

---
- name: Filter with jmespath
  hosts: localhost
  gather_facts: false

  vars:
    query_result: {
      "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
      },
      "changed": false,
      "current": [
         {
           "context": {
             "attributes": {
               "name": "prod",
               "uid": "11756"
             }
           }
         },
         {
           "context": {
             "attributes": {
               "name": "dev",
               "uid": "14424"
             }
           }
         }
      ],
      "failed": false
    }

  tasks:

    - name: Display original result
      debug: var=query_result

    - name: Display Filtered Result - One expression - one liner
      debug:
        msg: "{{ query_result.current | json_query(\"[?context.attributes.name=='prod'].context.attributes.name[]\") }}"

    - name: Display Filtered Result - One expression - Query in block var
      vars:
        query: >-
          [?context.attributes.name=='prod'].context.attributes.name[]
      debug:
        msg: "{{ query_result.current | json_query(query) }}"

    - name: Display Filtered Result - Pipe expressions - Query in block var
      vars:
        query: >-
          [].context.attributes[] | [?name=='prod'].name[]
      debug:
        msg: "{{ query_result.current | json_query(query) }}"

For your next question, please read the help section and consider providing a full MVCE directly in your question (as I just did in this answer).对于您的下一个问题,请阅读帮助部分并考虑直接在您的问题中提供完整的 MVCE (正如我在此答案中所做的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM