Is it possible to `include` a tasks file with a different set of hosts?
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'] }}"
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) }}"