是否可以“包含”具有不同主机集的任务文件?

是否可以“包含”具有不同主机集的任务文件?

理想情况下,我希望能够做类似的事情:

- include: deregister_from_loadbalancer.yml
  delegate_to: loadbalancer

这样,我仍然可以访问我正在运行整体游戏的主机的变量,但操作是在负载均衡器主机上进行的。

为何不分开玩呢?

我无法将其分解为单独的剧集,因为我每次都会将机器从负载均衡器轮换中移除。分解为多个剧集意味着:

- hosts: loadbalancers
  tasks:
    - include: remove_from_loadbalancer.yml
      vars:
        machine: "{{ item }}"
      with_items: "{{ groups['webservers'] }}"

# at this point in the play I'm being fired for gross incompetence

- hosts: webservers
  tasks:
    # ... update them

- hosts: loadbalancers
  tasks:
    - include: add_to_loadbalancer.yml
      vars:
        machine: "{{ item }}"
      with_items: "{{ groups['webservers'] }}"

答案1

如果您发布的示例...

- include: deregister_from_loadbalancer.yml
  delegate_to: loadbalancer

...不起作用,你仍然可以这样做。Includes 通常可以接受参数,因此你可以将其写成:

- include: deregister_from_loadbalancer.yml
  delegate_host: loadbalancer

然后在您的包含中deregister_from_loadbalancer.yml使用该变量来委派您的所有任务:

- some: task
  delegate_to: "{{ delegate_host | default(omit) }}"

- another: task
  delegate_to: "{{ delegate_host | default(omit) }}"

相关内容