Last Updated: May 17, 2022
·
13.07K
· Lorin Hochstein

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

5 Responses
Add your response

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?

over 1 year ago ·

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.

over 1 year ago ·

Very cool. Now I can build generic utility playbook bundles
with 'hosts: all:&hosts' and use them anywhere.

over 1 year ago ·

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}}

over 1 year ago ·

Try quoting the {{zhost}}, like so:
- include: tasks/zoo1.yml zhost="{{zhost}}"

over 1 year ago ·