gpt4 book ai didi

json - 如何使用匹配值在ansible中进行Json查询

转载 作者:行者123 更新时间:2023-12-04 08:13:15 24 4
gpt4 key购买 nike

有人可以告诉我是否可以这样做吗?
我有以下 list

list:
- dev
- uat
我想遍历上面的列表并从下面的输出中获取相应 IpAddress 的值。
{
"Tags": [
{
"ResourceType": "instance",
"ResourceId": "i-8dh7435490fjksfd",
"Environment": "production",
"IpAddress": "10.0.0.8"
},
{
"ResourceType": "instance",
"ResourceId": "i-8dh7435dsj89jfe",
"Environment": "dev",
"IpAddress": "10.0.0.3"
},
{
"ResourceType": "instance",
"ResourceId": "i-8dsdj456ovfvfd",
"Environment": "uat",
"IpAddress": "10.0.0.7"
}
]
}
这是我正在寻找的输出
[
"10.0.0.3",
"10.0.0.7"
]
我正在尝试使用此过滤器 Tags| json_query('[*].IpAddress') 。但它给了我列表中的所有 IpAddress

最佳答案

例如

- hosts: localhost
vars:
my_list: [dev, uat]
my_Tags: [
{
"ResourceType": "instance",
"ResourceId": "i-8dh7435490fjksfd",
"Environment": "production",
"IpAddress": "10.0.0.8"
},
{
"ResourceType": "instance",
"ResourceId": "i-8dh7435dsj89jfe",
"Environment": "dev",
"IpAddress": "10.0.0.3"
},
{
"ResourceType": "instance",
"ResourceId": "i-8dsdj456ovfvfd",
"Environment": "uat",
"IpAddress": "10.0.0.7"
}
]
tasks:
- debug:
msg: "{{ my_Tags|selectattr('Environment', 'in', my_list)|
map(attribute='IpAddress')|list }}"

  msg:
- 10.0.0.3
- 10.0.0.7

关于json - 如何使用匹配值在ansible中进行Json查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65840956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com