Ansible play 在一组主机上运行,​​但取决于其他主机的事实

Ansible play runs on one group of hosts, but depends on facts from others

这是基本用例:

我有一个我想设置的 NGINX 反向代理,所以我指定了 "nginx" 组中只有 运行 的游戏。

但是,为了知道反向代理的 ips,我需要从 "upstreams" 组收集事实。这不会发生,因为该剧没有 运行 setup 在 "upstreams".

包含我以前使用过的解决方案,但我希望能够将其全部包含在单个主机播放中,我可以 运行 独立于其他人。

使用Delegated Factspre_tasks,并将事实委托给他们所属的主机。

- hosts: nginx
  become: yes
  tags:
    - nginx
  vars:
    listen_address: "x.x.x.x"
  pre_tasks:
    - name: 'gather upstream facts.'
      setup:
      delegate_to: "{{item}}"
      delegate_facts: True
      with_items: "{{groups['upstreams']}}"
  roles:
    - role: nginx
      upstreams: "{{ groups['upstreams'] | map('extract', hostvars, ['ansible_all_ipv4_addresses']) | ipaddr('x.x.x.x') | first | list }}"