简体   繁体   English

Ansible:如何存储json_query的结果?

[英]Ansible: how to store the result of a json_query?

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 : 在有关过滤器的Ansible文档中,显示了以下示例,该示例对数据结构执行JSON查询,选择某些字段nameport

- 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}"

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. 我设法使用此逻辑来解析REST服务的JSON响应,但我不仅希望打印出结果,而且在我的剧本中再次重复使用几次。

How is it possible to persist the above variable var for later use? 如何保留上述变量var以便以后使用?

Just replace debug call with set_fact . 只需将debug调用替换为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}"

This will generate two persistent facts (based on docs data): name_server21 with value 9080 and name_server22 = 9090 . 这将产生两个持久事实(基于文档数据): name_server21用值9080name_server22 = 9090

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

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