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

查看:28
本文介绍了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}"

这将生成两个持久性事实(基于文档数据):name_server21 值为 9080name_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天全站免登陆