Ansible:按属性过滤列表 [英] Ansible: filter a list by its attributes
本文介绍了Ansible:按属性过滤列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Ansible 中注册了名为network"的变量:
I have variable named "network" registered in Ansible:
{
"addresses": {
"private_ext": [
{
"type": "fixed",
"addr": "172.16.2.100"
}
],
"private_man": [
{
"type": "fixed",
"addr": "172.16.1.100"
},
{
"type": "floating",
"addr": "10.90.80.10"
}
]
}
}
是否可以通过 type="floating" 来获取 IP 地址(addr")?
Is it possible to get the IP address ("addr") with type="floating" doing something like this?
- debug: var={{ network.addresses.private_man | filter type="fixed" | get "addr" }}
我知道语法有误,但你懂的.
I know the syntax is wrong but you get the idea.
推荐答案
我已经提交了一个pull请求(在 Ansible 2.2+ 中可用)通过在能干.在您的情况下,它的工作方式如下:
I've submitted a pull request (available in Ansible 2.2+) that will make this kinds of situations easier by adding jmespath query support on Ansible. In your case it would work like:
- debug: msg="{{ addresses | json_query(\"private_man[?type=='fixed'].addr\") }}"
会回来:
ok: [localhost] => {
"msg": [
"172.16.1.100"
]
}
这篇关于Ansible:按属性过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文