# Limiting hosts when including playbooks

**URL:** <https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278>\
**Category:** Ansible Project\
**Created:** [February 1, 2013, 4:26am UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278 "2013-02-01T04:26:22Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 4:26am UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/1 "2013-02-01T04:26:22Z")

</div>

When including a playbook in another playbook, is there any way to limit the hosts in the child playbook?

For example, if I had an existing playbook (e.g., “configure-widget.yaml”) that had its hosts set to “webservers”, and I wanted to write a playbook that only applied to the staging web server, is there any way to do something like:

- include: configure-widget.yaml limit-hosts=staging

Lorin

---

<div class="post-metadata">

**Author:** ![John\_Wong](https://avatars.discourse-cdn.com/v4/letter/j/6f9a4e/32.png) [@John\_Wong](https://forum.ansible.com/u/John_Wong)\
**Post date:** [February 1, 2013, 5:55am UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/2 "2013-02-01T05:55:22Z")

</div>

I think you can try this  
[https://groups.google.com/forum/#!msg/ansible-project/qfoeqytbRE4/SI58rlzeEwMJ](https://groups.google.com/forum/#!msg/ansible-project/qfoeqytbRE4/SI58rlzeEwMJ)

If you ever need some exception (all in this group except XX) you can use  
hosts: group1:!except\_this\_group

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 1, 2013, 1:34pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/3 "2013-02-01T13:34:56Z")

</div>

hosts: group1:&mustBeInThisGroup works too now!

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 3:52pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/4 "2013-02-01T15:52:05Z")

</div>

That approach works for limiting hosts when calling from the command-line, but I’d like to do it from inside of a playbook. One of the motivations is what Michael suggests in that original posting:

“The same as the above example could be used with --limit stage or --limit production to control inventory. I really don’t recommend this though, because leaving off limit would be pretty dangerous.”

If I want to limit execution of a playbook to staging servers, I worry about somebody forgetting the “–limit” flag. I’d prefer to do have a separate playbook, say “update-staging.yaml”, that looks like this:

- include: update.yaml hosts=staging

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 1, 2013, 4:02pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/5 "2013-02-01T16:02:09Z")

</div>

Lorin Hochstein wrote:

> That approach works for limiting hosts when calling from the command-line,  
> but I'd like to do it from inside of a playbook. One of the motivations is  
> what Michael suggests in that original posting:
> 
> "The same as the above example could be used with --limit stage or --limit  
> production to control inventory. I really don't recommend this though,  
> because leaving off limit would be pretty dangerous."
> 
> If I want to limit execution of a playbook to staging servers, I worry  
> about somebody forgetting the "--limit" flag. I'd prefer to do have a  
> separate playbook, say "update-staging.yaml", that looks like this:
> 
> - include: update.yaml hosts=staging
> 
> Unfortunately, that doesn't work. Right now I'm using a makefile to invoke  
> my ansible playbooks to achieve this functionality, but I find that  
> un-ansible-ish.
> 
> Michael, would you accept merges that implemented this sort of host  
> filtering on include statements? Would work the same as --limit on the  
> command-line except it would be an argument to include.

hosts: is already templateable with include arguments, so you could do  
hosts: webservers:&$hosts  
I think this will also lead to an error if you don't specify hosts, instead  
of running against all of it.

Daniel

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 4:06pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/6 "2013-02-01T16:06:49Z")

</div>

> Lorin Hochstein wrote:  
> \> That approach works for limiting hosts when calling from the  
> command-line,  
> \> but I'd like to do it from inside of a playbook. One of the motivations  
> is  
> \> what Michael suggests in that original posting:  
> \>  
> \> "The same as the above example could be used with --limit stage or  
> --limit  
> \> production to control inventory. I really don't recommend this though,  
> \> because leaving off limit would be pretty dangerous."  
> \>  
> \> If I want to limit execution of a playbook to staging servers, I worry  
> \> about somebody forgetting the "--limit" flag. I'd prefer to do have a  
> \> separate playbook, say "update-staging.yaml", that looks like this:  
> \>  
> \> - include: update.yaml hosts=staging  
> \>  
> \> Unfortunately, that doesn't work. Right now I'm using a makefile to  
> invoke  
> \> my ansible playbooks to achieve this functionality, but I find that  
> \> un-ansible-ish.  
> \>  
> \> Michael, would you accept merges that implemented this sort of host  
> \> filtering on include statements? Would work the same as --limit on the  
> \> command-line except it would be an argument to include.
> 
> hosts: is already templateable with include arguments, so you could do  
> hosts: webservers:&$hosts  
> I think this will also lead to an error if you don't specify hosts, instead  
> of running against all of it.

Cool, that works. For anybody who hits this via google, here's a complete  
example:

ping.yaml:

- hosts: webservers:&$hosts  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;- name: ping the hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: ping

pong.yaml:

# Just ping the staging server  
- include: ping.yaml hosts=staging

This is a useful pattern, where's a good place to document this?

Lorin

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 4:21pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/7 "2013-02-01T16:21:16Z")

</div>

Submitted a doc patch here: [https://github.com/ansible/ansible/pull/1956](https://github.com/ansible/ansible/pull/1956)

Lorin

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 6:40pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/8 "2013-02-01T18:40:29Z")

</div>

(multiple self replies today).

It seems that this doesn’t actually work. The templating on hosts only seems to work against extra\_vars that are passed on the command-line, so you can do something like:

ansible-playbook pong.yaml -e hosts=staging

And it will interpolate the “hosts” variable, properly, but if you do:

- include: ping.yaml hosts=staging

Then the “hosts=staging” part doesn’t get passed in.

I’m not sure if this is a bug, or by design.

Lorin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 1, 2013, 6:50pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/9 "2013-02-01T18:50:36Z")

</div>

Templating is a black art, it probably just requires we pass more into  
the dictionary for this, and "double template" the include line before  
we process it.

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 6:55pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/10 "2013-02-01T18:55:28Z")

</div>

Yeah, I’m going to push up a proposed fix into [https://github.com/ansible/ansible/pull/1956](https://github.com/ansible/ansible/pull/1956) shortly.

Lorin

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 1, 2013, 7:02pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/11 "2013-02-01T19:02:04Z")

</div>

Lorin Hochstein wrote:

> > > > Lorin Hochstein wrote:  
> > > > \> That approach works for limiting hosts when calling from the  
> > > > command-line,  
> > > > \> but I'd like to do it from inside of a playbook.
> > > 
> > > hosts: is already templateable with include arguments, so you could do
> > > 
> > > > hosts: webservers:&$hosts  
> > > > I think this will also lead to an error if you don't specify hosts,  
> > > > instead  
> > > > of running against all of it.
> > > 
> > > This is a useful pattern, where's a good place to document this?
> > 
> > Submitted a doc patch here: [https://github.com/ansible/ansible/pull/1956](https://github.com/ansible/ansible/pull/1956)
> 
> (multiple self replies today).
> 
> It seems that this doesn't actually work. The templating on hosts only  
> seems to work against extra\_vars that are passed on the command-line, so  
> you can do something like:
> 
> ansible-playbook pong.yaml -e hosts=staging
> 
> And it will interpolate the "hosts" variable, properly, but if you do:
> 
> - include: ping.yaml hosts=staging
> 
> Then the "hosts=staging" part doesn't get passed in.
> 
> I'm not sure if this is a bug, or by design.

This works fine here. Are you on devel, or 0.9? This is new for 1.0, IIRC.

Daniel

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 7:27pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/12 "2013-02-01T19:27:54Z")

</div>

I'm on devel: f7e286cf345a97a9ca6b8587cc16fbc901047dc6

I had to do this to get it to work for me:  
[https://github.com/ansible/ansible/compare/devel...d163c50](https://github.com/ansible/ansible/compare/devel...d163c50)

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 1, 2013, 7:41pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/13 "2013-02-01T19:41:35Z")

</div>

Lorin Hochstein wrote:

---

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein1](https://avatars.discourse-cdn.com/v4/letter/l/ce73a5/32.png) [@Lorin\_Hochstein1](https://forum.ansible.com/u/Lorin_Hochstein1)\
**Post date:** [February 1, 2013, 7:52pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/14 "2013-02-01T19:52:09Z")

</div>

> Lorin Hochstein wrote:  
> \>  
> \>\> Lorin Hochstein wrote:  
> \>\> \>  
> \>\> \>  
> \>\> \>\>  
> \>\> \>\>  
> \>\> \>\>  
> \>\> \>\>  
> \>\> \>\>\>  
> \>\> \>\>\>  
> \>\> \>\>\>\> Lorin Hochstein wrote:  
> \>\> \>\>\>\> \> That approach works for limiting hosts when calling from the  
> \>\> \>\>\>\> command-line,  
> \>\> \>\>\>\> \> but I'd like to do it from inside of a playbook.  
> \>\> \>\>\>\>  
> \>\> \>\>\>  
> \>\> \>\>  
> \>\> \>\>\> hosts: is already templateable with include arguments, so you could  
> \>\> do  
> \>\> \>\>\>\> hosts: webservers:&$hosts  
> \>\> \>\>\>\> I think this will also lead to an error if you don't specify hosts,  
> \>\> \>\>\>\> instead  
> \>\> \>\>\>\> of running against all of it.  
> \>\> \>\>\>\>  
> \>\> \>\>\> This is a useful pattern, where's a good place to document this?  
> \>\> \>\>\>  
> \>\> \>\>\>  
> \>\> \>\> Submitted a doc patch here:  
> \>\> [https://github.com/ansible/ansible/pull/1956](https://github.com/ansible/ansible/pull/1956)  
> \>\> \>\>  
> \>\> \> It seems that this doesn't actually work. The templating on hosts only  
> \>\> \> seems to work against extra\_vars that are passed on the command-line,  
> \>\> so  
> \>\> \> you can do something like:  
> \>\> \>  
> \>\> \> ansible-playbook pong.yaml -e hosts=staging  
> \>\> \>  
> \>\> \>  
> \>\> \> And it will interpolate the "hosts" variable, properly, but if you do:  
> \>\> \>  
> \>\> \>  
> \>\> \> - include: ping.yaml hosts=staging  
> \>\> \>  
> \>\> \> Then the "hosts=staging" part doesn't get passed in.  
> \>\> \>  
> \>\> \>  
> \>\> \> I'm not sure if this is a bug, or by design.  
> \>\>  
> \>\> This works fine here. Are you on devel, or 0.9? This is new for 1.0,  
> \>\> IIRC.  
> \>\>  
> \>\>  
> \> I'm on devel: f7e286cf345a97a9ca6b8587cc16fbc901047dc6  
> \>  
> \> I had to do this to get it to work for me:  
> \> [https://github.com/ansible/ansible/compare/devel...d163c50](https://github.com/ansible/ansible/compare/devel...d163c50)
> 
> --list-hosts is unfortunately separate and uses different logic to what  
> would actually be executed. The proper fix here is probably to create the  
> Play objects and use the same code everywhere.

Oh, it's a list-hosts bug! I just posted #1959, I updated its title to  
reflect that the problem is specific to --list-hosts. Definitely good idea  
to use the same code path, people will be mighty surprised if --list-hosts  
give different output from the actual hosts that are executed.

---

<div class="post-metadata">

**Author:** ![Uffi\_Schnuffi1](https://avatars.discourse-cdn.com/v4/letter/u/e47774/32.png) [@Uffi\_Schnuffi1](https://forum.ansible.com/u/Uffi_Schnuffi1)\
**Post date:** [March 26, 2019, 12:01pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/15 "2019-03-26T12:01:47Z")

</div>

This is no longer working with the new `import_playbook` module.

Is there a recommended way to achieve this (i.e. limiting hosts for an imported playbook?). I have not found a way as of yet.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [March 26, 2019, 12:41pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/16 "2019-03-26T12:41:39Z")

</div>



---

<div class="post-metadata">

**Author:** ![Uffi\_Schnuffi](https://avatars.discourse-cdn.com/v4/letter/u/8edcca/32.png) [@Uffi\_Schnuffi](https://forum.ansible.com/u/Uffi_Schnuffi)\
**Post date:** [March 27, 2019, 1:45pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/17 "2019-03-27T13:45:24Z")

</div>

This is not feasbile when the imported playbook is not in one’s own control.  
In my use case the imported playbook is a playbook controlled by a third party. The issue here is that the host group names used by that playbook do not necessarily reflect the host names in our inventory.  
It would be neat if it were possible to define something like

```auto
- import_playbook: playbook
vars:
host_groups:
group1: # this is the host group used in the imported playbook

- our_inv_groupA
- our_inv_groupB

```

Such functionality would make importing very powerful. I’m not sure if it’s doable with the static nature of importing playbooks, though… I guess it depends on when the inventory comes into play during (pre)processing.

My workaround now is to make sure that the host groups used in the imported playbook exist in our inventory. Further, it has to be ensured that the imported playbook does not use `all` (since the inventory file may contain other and shared hosts): Can be tested with ``ansible-playbook --list-hosts `.`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 9, 2019, 2:32pm UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/18 "2019-04-09T14:32:50Z")

</div>

create the groups the imported playbook uses in a group\_by in a preceding play

---

<div class="post-metadata">

**Author:** ![Uffi\_Schnuffi1](https://avatars.discourse-cdn.com/v4/letter/u/e47774/32.png) [@Uffi\_Schnuffi1](https://forum.ansible.com/u/Uffi_Schnuffi1)\
**Post date:** [April 17, 2019, 10:28am UTC](https://forum.ansible.com/t/limiting-hosts-when-including-playbooks/12278/19 "2019-04-17T10:28:31Z")

</div>

This is amazing!!! Thank you! : )
