Playbook inclusion and Host Intersections

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

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