在 Ansible 中搜索字典值 [英] Search Dictionary Values in Ansible
本文介绍了在 Ansible 中搜索字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一本这样的字典:
ossec_datacenter:
atlanta:
hostname: 'server1.fakedomain.net'
ip: '192.168.12.170'
port: '1515'
miami:
hostname: 'server2.fakedomain.net'
ip: '192.168.20.31'
port: '1514'
dallas:
hostname: 'server2.fakedomain.net'
ip: '192.168.20.20'
port: '1515'
如何在我的 when
子句中搜索此字典中的所有值?
How would I search for all values in this dictionary in my when
clause?
我可以使用 ossec_datacenter[ossec_dc]['hostname']
但我想搜索所有值以确保不存在匹配项.
But I want so search all values to make sure no matches are present.
换句话说,我不希望在该数据结构的任何地方找到 inventory_hostname
或 IP.
In other words I don't want the inventory_hostname
nor the IP to be found anywhere in that data structure.
推荐答案
如果你想使用json_query(需要ansible 2.2)你可以这样做来搜索ip和主机名:
If you want to use json_query (requires ansible 2.2) you can do this to search ip and hostname:
- name: find inventory_hostname
set_fact:
found: True
with_items:
- "{{ ossec_datacenter | json_query('*.ip') }}"
- "{{ ossec_datacenter | json_query('*.hostname') }}"
when: "inventory_hostname == item"
或者如果您想搜索数据中心中的任何键(ip、主机名或端口):
or if you want to search any of the keys in the datacenters (ip, hostname, or port):
- name: find inventory_hostname
set_fact:
found: True
with_items: "{{ ossec_datacenter | json_query('*.*') }}"
when: "inventory_hostname == item"
然后测试 found
变量.
这篇关于在 Ansible 中搜索字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文