Ansible:如何存储json_query的结果? [英] Ansible: how to store the result of a json_query?

查看:601
本文介绍了Ansible:如何存储json_query的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤器上的Ansible文档中,显示了以下示例,该示例在数据结构上执行JSON查询,选择某些字段nameport:

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

这将生成两个持久性事实(基于文档数据):值为9080name_server21name_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆