Ansible 用特殊性质过滤hostvar

Ansible 用特殊性质过滤hostvar,ansible,Ansible,我喜欢这样 --- all: hosts: server-a: server_dc: "Hetzner" ansible_host: 192.168.1.1 server-b: server_dc: "OVH" ansible_host: 192.168.1.2 而剧本调试是: - name: sample debug: var: hostvars 所有HostVar调试成功

我喜欢这样

---
  all:
    hosts:

      server-a:
        server_dc: "Hetzner"
        ansible_host: 192.168.1.1

      server-b:
        server_dc: "OVH"
        ansible_host: 192.168.1.2
而剧本调试是:

- name: sample
  debug:
    var: hostvars
所有HostVar调试成功

如何获得相同的hostvars变量但经过筛选。该服务器的任何一个dc都是相等的
OVH

我不想迭代模板,我只需要一个新的过滤变量,它包含所有其他属性

我需要调试另一个变量以查看此输出:

['server-b']

我相信这符合您的要求(删除“no_log:true”将导致在您的playbook输出中打印完整的词典):


能否请你在文章中给出一个清晰的例子,说明你所期望的结果?
- set_fact:
    filtered_hosts: "{{ filtered_hosts | default({}) | combine({item.key: item.value}) }}"
  when: "item.value.server_dc == 'OVH'"
  with_dict: "{{ hostvars }}"
  no_log: true
- debug:
    var: filtered_hosts