如何使用匹配值在ansible中进行Json查询
How to do Json query in ansible with matched value
有人能告诉我是否可行吗?
我有以下列表
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
有人能告诉我是否可行吗? 我有以下列表
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