# Limiting hosts on included playbooks

**URL:** <https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895>\
**Category:** Ansible Project\
**Created:** [November 12, 2013, 10:59pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895 "2013-11-12T22:59:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [November 12, 2013, 10:59pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895/1 "2013-11-12T22:59:00Z")

</div>

Hi all,

I’m attempting to use a technique for limiting hosts in playbook includes described in an older thread ( [https://groups.google.com/forum/#!msg/ansible-project/7Yb4zmZPg8c/b646JLYpJccJ](https://groups.google.com/forum/#!msg/ansible-project/7Yb4zmZPg8c/b646JLYpJccJ) ), and it’s mostly working, except for one thing. I have a playbook that includes another like this:

- include: other\_playbook.yml limit\_hosts=a\_group

In that included playbook, my hosts lines are all like this:

- hosts: target\_group:&{{ limit\_hosts }}

which works great for limiting the included playbook tasks to the passed in group. However, I also need to be able to run that included playbook separately, at which point I need to set limit\_hosts to ‘all’ for normal operation. I can do this on the command line with -e, or in a vars section, but the best place to put it (which would make things ‘just work’) seems to me to be my group\_vars/all file, but that’s the one place that doesn’t work. Should it? Is there a better way to do this? I’m running a fairly recent ansible dev version ( 078aaa9 ), and have also tried with the latest as of this post ( f31cb7c ).

Nathan

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 12, 2013, 11:08pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895/2 "2013-11-12T23:08:30Z")

</div>

have you tried?:

- hosts: target\_group:&{{ limit\_hosts|default(‘all’)}}

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [November 12, 2013, 11:15pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895/3 "2013-11-12T23:15:59Z")

</div>

I have, and it works, but then I have to set that for every play, and can’t set it for all of them at once.

Nathan

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 12, 2013, 11:25pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895/4 "2013-11-12T23:25:40Z")

</div>

not sure what you mean by that.

in any case group\_vars/all won't get picked until AFTER hosts: is  
evaluated so you cannot use a variable from the inventory to define the  
inventory you are going to use (self referential!).

---

<div class="post-metadata">

**Author:** ![Nathan\_Howell](https://avatars.discourse-cdn.com/v4/letter/n/65b543/32.png) [@Nathan\_Howell](https://forum.ansible.com/u/Nathan_Howell)\
**Post date:** [November 12, 2013, 11:35pm UTC](https://forum.ansible.com/t/limiting-hosts-on-included-playbooks/13895/5 "2013-11-12T23:35:10Z")

</div>

By that I mean that I would much prefer to set this variable in one place for all plays, instead of having to define a default everywhere I need to use that variable.

Ah, I guess that makes sense about group\_vars. Is there anywhere else I can set this as a default value?

Nathan
