Ansible:如何存储json_query的结果? [英] Ansible: how to store the result of a json_query?
问题描述
在过滤器上的Ansible文档中,显示了以下示例,该示例在数据结构上执行JSON查询,选择某些字段name
和port
:
In the Ansible documentation on filters, the following example is shown, which executes a JSON query on a data structure, selecting certain fields name
and port
:
- name: "Display all server ports and names from cluster1"
debug: var=item
with_items: "{{domain_definition|json_query(server_query)}}"
vars:
server_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}"
我设法使用此逻辑来解析REST服务的JSON响应,但我不仅希望打印出结果,而且在我的剧本中再次重复使用几次.
I managed to use this logic to parse the JSON response of a REST service, but I would like to not only print out the result, but reuse it several times again during my playbook.
如何持久保存上述变量var
以便以后使用?
How is it possible to persist the above variable var
for later use?
推荐答案
只需将debug
调用替换为set_fact
.例如:
Just replace debug
call with set_fact
. For example:
- name: "Display all server ports and names from cluster1"
set_fact:
'name_{{ item.name }}': '{{ item.port }}'
with_items: "{{domain_definition|json_query(server_query)}}"
vars:
server_query: "domain.server[?cluster=='cluster2'].{name: name, port: port}"
这将生成两个持久性事实(基于文档数据):值为9080
的name_server21
和name_server22
= 9090
.
This will generate two persistent facts (based on docs data): name_server21
with value 9080
and name_server22
= 9090
.
这篇关于Ansible:如何存储json_query的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!