[英]Search Dictionary Values in Ansible
Having a dictionary like this: 有这样的字典:
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'
How would I search for all values in this dictionary in my when
clause? 我如何在
when
子句中搜索此字典中的所有值?
I can access variables using ossec_datacenter[ossec_dc]['hostname']
我可以使用
ossec_datacenter[ossec_dc]['hostname']
访问变量
But I want so search all values to make sure no matches are present. 但我想搜索所有值以确保不存在匹配。
In other words I don't want the inventory_hostname
nor the IP to be found anywhere in that data structure. 换句话说,我不希望在该数据结构中的任何地方找到
inventory_hostname
和IP。
If you want to use json_query (requires ansible 2.2) you can do this to search ip and hostname: 如果你想使用json_query(需要ansible 2.2),你可以这样做来搜索ip和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"
or if you want to search any of the keys in the datacenters (ip, hostname, or port): 或者如果要搜索数据中心(ip,hostname或port)中的任何键:
- name: find inventory_hostname
set_fact:
found: True
with_items: "{{ ossec_datacenter | json_query('*.*') }}"
when: "inventory_hostname == item"
and then test the found
var. 然后测试
found
var。
Here's a condition for hostname: 这是主机名的条件:
when: inventory_hostname not in (ossec_datacenter.values() | map(attribute='hostname') | list)
Use ansible_default_ipv4.address
or some other fact about IP address and reduce your dict with map(attribute='ip')
to search for IP addresses. 使用
ansible_default_ipv4.address
或其他一些关于IP地址的事实,并使用map(attribute='ip')
减少你的dict来搜索IP地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.