Limiting hosts when including playbooks

2,090 views
Skip to first unread message

Lorin Hochstein

unread,
Jan 31, 2013, 11:26:22 PM1/31/13
to ansible...@googlegroups.com
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

Yeukhon Wong

unread,
Feb 1, 2013, 12:55:22 AM2/1/13
to ansible...@googlegroups.com
I think you can try this

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

Michael DeHaan

unread,
Feb 1, 2013, 8:34:56 AM2/1/13
to ansible...@googlegroups.com
hosts: group1:&mustBeInThisGroup works too now!
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lorin Hochstein

unread,
Feb 1, 2013, 10:52:05 AM2/1/13
to ansible...@googlegroups.com
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.

 Not sure what to call it (limit, limit-hosts, hosts)?


Lorin


On Fri, Feb 1, 2013 at 12:55 AM, Yeukhon Wong <gokop...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Lorin Hochstein
Lead Architect - Cloud Services
Nimbis Services, Inc.

Daniel Hokka Zakrisson

unread,
Feb 1, 2013, 11:02:09 AM2/1/13
to ansible...@googlegroups.com
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

Lorin Hochstein

unread,
Feb 1, 2013, 11:06:49 AM2/1/13
to ansible...@googlegroups.com
On Fri, Feb 1, 2013 at 11:02 AM, Daniel Hokka Zakrisson <dan...@hozac.com> 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. 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
  tasks:
   - name: ping the hosts
     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

Lorin Hochstein

unread,
Feb 1, 2013, 11:21:16 AM2/1/13
to ansible...@googlegroups.com
On Fri, Feb 1, 2013 at 11:06 AM, Lorin Hochstein <lo...@nimbisservices.com> wrote:

On Fri, Feb 1, 2013 at 11:02 AM, Daniel Hokka Zakrisson <dan...@hozac.com> 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? 



Lorin
 

Lorin Hochstein

unread,
Feb 1, 2013, 1:40:29 PM2/1/13
to ansible...@googlegroups.com


On Friday, February 1, 2013 11:21:16 AM UTC-5, Lorin Hochstein wrote:


On Fri, Feb 1, 2013 at 11:06 AM, Lorin Hochstein  wrote:


On Fri, Feb 1, 2013 at 11:02 AM, Daniel Hokka Zakrisson  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? 





(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

 

Michael DeHaan

unread,
Feb 1, 2013, 1:50:36 PM2/1/13
to ansible...@googlegroups.com
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.

Lorin Hochstein

unread,
Feb 1, 2013, 1:55:28 PM2/1/13
to ansible...@googlegroups.com
Yeah, I'm going to push up a proposed fix into https://github.com/ansible/ansible/pull/1956 shortly.


Lorin


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Daniel Hokka Zakrisson

unread,
Feb 1, 2013, 2:02:04 PM2/1/13
to ansible...@googlegroups.com
This works fine here. Are you on devel, or 0.9? This is new for 1.0, IIRC.

Daniel

Lorin Hochstein

unread,
Feb 1, 2013, 2:27:54 PM2/1/13
to ansible...@googlegroups.com
On Fri, Feb 1, 2013 at 2:02 PM, Daniel Hokka Zakrisson <dan...@hozac.com> wrote:
Lorin Hochstein wrote:
>
>
> On Friday, February 1, 2013 11:21:16 AM UTC-5, Lorin Hochstein wrote:
>>
>>
>>
>> On Fri, Feb 1, 2013 at 11:06 AM, Lorin Hochstein  wrote:
>>
>>>
>>> On Fri, Feb 1, 2013 at 11:02 AM, Daniel Hokka Zakrisson  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
>>
> 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

Daniel Hokka Zakrisson

unread,
Feb 1, 2013, 2:41:35 PM2/1/13
to ansible...@googlegroups.com
--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.

Daniel

Lorin Hochstein

unread,
Feb 1, 2013, 2:52:09 PM2/1/13
to ansible...@googlegroups.com
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. 


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Uffi Schnuffi

unread,
Mar 26, 2019, 8:01:48 AM3/26/19
to Ansible Project
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.

Sebastian Meyer

unread,
Mar 26, 2019, 8:41:48 AM3/26/19
to ansible...@googlegroups.com
On 26.03.19 13:01, 'Uffi Schnuffi' via Ansible Project wrote:
> 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.

---
- import_playbook: test.yaml
vars:
myhosts: plonk

---
- name: Test playbook
hosts: all:&{{ myhosts }}
tasks:
- name: test var
debug:
msg: "GOOOOOOO"


works for me.

See:
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.7.html#include-tasks-import-tasks-inline-variables

Sebastian
>>> <javascript:>>
>>>> wrote:
>>>>
>>>>> I think you can try this
>>>>>
>>>>>
>>> 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
>>>>>
>>>>>
>>>>> On Thursday, January 31, 2013 11:26:22 PM UTC-5, Lorin Hochstein wrote:
>>>>>>
>>>>>> 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
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups
>>>>> "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an
>>>>> email to ansible-proje...@googlegroups.com <javascript:>.
>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Lorin Hochstein
>>>> Lead Architect - Cloud Services
>>>> Nimbis Services, Inc.
>>>> www.nimbisservices.com
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>> Groups
>>>> "Ansible Project" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an
>>>> email to ansible-proje...@googlegroups.com <javascript:>.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to ansible-proje...@googlegroups.com <javascript:>.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>>
>> --
>> Lorin Hochstein
>> Lead Architect - Cloud Services
>> Nimbis Services, Inc.
>> www.nimbisservices.com
>>
>

--
Sebastian Meyer
Linux Consultant & Trainer
Tel.: +49-172-2057471
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

Uffi Schnuffi

unread,
Mar 27, 2019, 9:45:25 AM3/27/19
to Ansible Project
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

```
- 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 <playbook>`.

Brian Coca

unread,
Apr 9, 2019, 10:33:08 AM4/9/19
to Ansible Project
create the groups the imported playbook uses in a group_by in a preceding play


----------
Brian Coca

Uffi Schnuffi

unread,
Apr 17, 2019, 6:28:31 AM4/17/19
to Ansible Project
On Tuesday, April 9, 2019 at 4:33:08 PM UTC+2, Brian Coca wrote:
create the groups the imported playbook uses in a  group_by in a preceding play

This is amazing!!! Thank you! : )


Reply all
Reply to author
Forward
Message has been deleted
0 new messages