在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屋!
查看全文