# Playbook inclusion and Host Intersections

**URL:** <https://forum.ansible.com/t/playbook-inclusion-and-host-intersections/17803>\
**Category:** Ansible Project\
**Created:** [March 21, 2015, 12:07am UTC](https://forum.ansible.com/t/playbook-inclusion-and-host-intersections/17803 "2015-03-21T00:07:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Ray](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Alan\_Ray](https://forum.ansible.com/u/Alan_Ray)\
**Post date:** [March 21, 2015, 12:07am UTC](https://forum.ansible.com/t/playbook-inclusion-and-host-intersections/17803/1 "2015-03-21T00:07:51Z")

</div>

I’m trying to figure out how to make includable playbooks that

1. Work like normal playbooks when run via ansible, running an a group of hosts
2. Only run on the group of hosts from (1) that intersects with the hosts of the includer.

(Use case: I have a playbook for initializing our Jenkins slaves. Sometimes I need to run it directly to update all of the slaves to a new configuration. But sometimes I’m setting up a type of server that includes being a slave and I just need to include the role so that all servers of that type are setup correctly.)

This seems like a common use case, but I can’t find it in the documentation which makes me wonder if I have the wrong approach. Here’s what I have so far:

_JenkinsSlave.yml_

- name: JenkinsSlave creation  
hosts: jenkinslaves:&{{hosts}}  
…

_BuildServer.yml_

- include: JenkinsSlave.yml hosts=BuildServers  
…

The above works for inclusion, but doesn’t work when I run JenkinsSlaves.yml because hosts is empty and so the intersection of JenkinsSlave and hosts is none. I’d like something like :&{{hosts|default(all)}} but that doesn’t seem to work the way I expect.

Thanks for all feedback,  
Alan

---

<div class="post-metadata">

**Author:** ![Alan\_Ray](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Alan\_Ray](https://forum.ansible.com/u/Alan_Ray)\
**Post date:** [March 21, 2015, 1:49am UTC](https://forum.ansible.com/t/playbook-inclusion-and-host-intersections/17803/2 "2015-03-21T01:49:05Z")

</div>

The magic line seems to be

hosts: “jenkinsslaves:&{{ hosts|default(‘all’) }}”

I was missing that the ouput of the {{ }} expression needs to be a string (‘all’), not a variable (e.g. all).

This seems like a pretty easy solution, unless there is a better way?

Thanks,  
Alan
