Limiting hosts when including a playbook
You can define a playbook that calls another and limits the host. Here's a child playbook,that's meant to be invoked by the parent. Note how hosts
is defined:
# ping.yaml
- name: ping
hosts: webservers:&$hosts
tasks:
- name: ping
action: ping
Here's how you would define another playbook that invokes ping.yaml but only for the staging server:
# pong.yaml
- include: ping.yaml hosts=staging
Written by Lorin Hochstein
Related protips
5 Responses
I haven't seen that syntax when defining hosts before. Have any pointers? Also, does this mean that staging is defined in webservers:children or can it be a top level group itself?
The :& syntax isn't currently documented, I just submitted a pull request to document that: https://github.com/ansible/ansible/pull/2033
In the example above, staging can be an individual host or any kind of group, including a top-level group.
Very cool. Now I can build generic utility playbook bundles
with 'hosts: all:&hosts' and use them anywhere.
is there a way to make "staging" into a variable?
- include: ping.yaml hosts=staging
Tried the usaul {{var}} synatx; does not seem to work
ie:
- include: tasks/zoo1.yml zhost={{zhost}}
Try quoting the {{zhost}}, like so:
- include: tasks/zoo1.yml zhost="{{zhost}}"