ANSIBLE - Json_Query - 过滤结果
ANSIBLE - Json_Query - Filter result
在我的 ansible 剧本中,我正在尝试过滤 json 结果,但目前,它不起作用。
1/剧本
在我的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') }}"
也许我的 json_query 过滤器有问题?
任何的想法 ?
2/Query_result过滤前输出
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/过滤结果为空
不幸的是我的结果是空的。
TASK [Display Filtered Result] **********************************************************
{
"msg": []
}
谢谢
菊
首先,单引号和双引号在jmespath expressions specification中很重要。您用于搜索的文字字符串需要用单引号引起来。
其次,您的过滤器不匹配。您要么必须在流处理中将过滤器移动到更高级别,要么通过管道传输表达式。
下面是一些示例,可以说明上述建议并得出相同的结果
---
- 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) }}"
对于您的下一个问题,请阅读帮助部分并考虑直接在您的问题中提供 full MVCE(正如我刚刚在此回答中所做的那样)。
在我的 ansible 剧本中,我正在尝试过滤 json 结果,但目前,它不起作用。
1/剧本
在我的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') }}"
也许我的 json_query 过滤器有问题?
任何的想法 ?
2/Query_result过滤前输出
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/过滤结果为空
不幸的是我的结果是空的。
TASK [Display Filtered Result] **********************************************************
{
"msg": []
}
谢谢
菊
首先,单引号和双引号在jmespath expressions specification中很重要。您用于搜索的文字字符串需要用单引号引起来。
其次,您的过滤器不匹配。您要么必须在流处理中将过滤器移动到更高级别,要么通过管道传输表达式。
下面是一些示例,可以说明上述建议并得出相同的结果
---
- 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) }}"
对于您的下一个问题,请阅读帮助部分并考虑直接在您的问题中提供 full MVCE(正如我刚刚在此回答中所做的那样)。