大网数据广告活动
服务器运维问答

Is it possible to `include` a tasks file with a different set of hosts?

· 52CE 我爱测

Ideally, I'd like to be able to do something like: - include: deregister_from_loadbalancer.yml delegate_to: loadbalancer In such a way that I still have access to the vars for the host I'm running the overall play against, but ac…

问题原文

原帖链接 · Parthian Shot · CC BY-SA 3.0

Ideally, I'd like to be able to do something like:

- include: deregister_from_loadbalancer.yml
  delegate_to: loadbalancer

In such a way that I still have access to the vars for the host I'm running the overall play against, but actions are taking place on the loadbalancer host.

Why not separate plays?

I can't just break this into separate plays, because I'm taking machines out of loadbalancer rotation one-at-a-time. Separating into multiple plays would mean something like:

- 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'] }}"

社区高分解答

原帖链接 · udondan · CC BY-SA 3.0

If the example you posted...

- include: deregister_from_loadbalancer.yml
  delegate_to: loadbalancer

...does not work, you can still do this. Includes in general can take parameters, so you could write it as:

- include: deregister_from_loadbalancer.yml
  delegate_host: loadbalancer

And in your include deregister_from_loadbalancer.yml you use that var to delegate all your tasks then:

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

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。