How to use host ranges with `ansible-playbook --limit`?

7,030 views
Skip to first unread message

Frank Thommen

unread,
Jan 3, 2018, 12:04:23 PM1/3/18
to Ansible Project
Hi,

I assumed, that host ranges could be used with ansible-playbook's
--limit as they can be used in the inventory itself. However this
doesn't seem to be possible:


$ ansible-playbook site.yml --limit='host[010:027]'

[WARNING]: Could not match supplied host pattern, ignoring: host

ERROR! Specified --limit does not match any hosts
$

All hosts host010 - host027 are listed in the inventory.

The same happens with double quotes and witout quotes around the limit
pattern.

How can I use host ranges witn --limit?

Cheers
frank


Kai Stian Olstad

unread,
Jan 4, 2018, 1:12:38 AM1/4/18
to ansible...@googlegroups.com
On 03.01.2018 18:03, Frank Thommen wrote:
> I assumed, that host ranges could be used with ansible-playbook's
> --limit as they can be used in the inventory itself. However this
> doesn't seem to be possible:

It's called patterns
http://docs.ansible.com/ansible/latest/intro_patterns.html


> $ ansible-playbook site.yml --limit='host[010:027]'

According to the documentation this is the syntax to get index 10 to 27
from group named host.


> [WARNING]: Could not match supplied host pattern, ignoring: host
>
> ERROR! Specified --limit does not match any hosts
> $
>
> All hosts host010 - host027 are listed in the inventory.
>
> The same happens with double quotes and witout quotes around the limit
> pattern.
>
> How can I use host ranges witn --limit?

I think you best option is to switch to regex, and that can be done with
tilde

ansible-playbook site.yml --limit='~host0(1[0-9]|2[0-7])'


--
Kai Stian Olstad

Frank Thommen

unread,
Jan 4, 2018, 2:49:04 AM1/4/18
to ansible...@googlegroups.com
On 04/01/18 07:11, Kai Stian Olstad wrote:
> On 03.01.2018 18:03, Frank Thommen wrote:
>> I assumed, that host ranges could be used with ansible-playbook's
>> --limit as they can be used in the inventory itself.  However this
>> doesn't seem to be possible:
>
> It's called patterns
> http://docs.ansible.com/ansible/latest/intro_patterns.html
>
>
>> $ ansible-playbook site.yml --limit='host[010:027]'
>
> According to the documentation this is the syntax to get index 10 to 27
> from group named host.

I see. It seems a bad idea to implement the seemingly identical syntax
for two things with completely different meaning: A range in the
inventory and indexes elsewhere


>>  [WARNING]: Could not match supplied host pattern, ignoring: host
>>
>> ERROR! Specified --limit does not match any hosts
>> $
>>
>> All hosts host010 - host027 are listed in the inventory.
>>
>> The same happens with double quotes and witout quotes around the limit
>> pattern.
>>
>> How can I use host ranges witn --limit?
>
> I think you best option is to switch to regex, and that can be done with
> tilde
>
>  ansible-playbook site.yml --limit='~host0(1[0-9]|2[0-7])'

I hoped to avoid that. regexes tend to become quite complex and
unreadable very quickly. The usecase in the post is a very simplified
breakdown of the "real" term we need. But I'll give it a try anyway. :-)

Thanks
frank


Andrey Kvaps

unread,
May 28, 2018, 10:47:46 AM5/28/18
to Ansible Project
Hi, there is similar issue on github:
https://github.com/ansible/ansible/issues/4620

I already say about that, seems that ansible developers don't see any problem, and don't want to reopen issue.

So the only way how to do this use simple shell scripting, like for your command:

ansible-playbook site.yml --limit=`echo host{010..027} | tr \  ,`

- kvaps

Kai Stian Olstad

unread,
May 28, 2018, 11:56:30 AM5/28/18
to ansible...@googlegroups.com
On 28.05.2018 16:47, Andrey Kvaps wrote:
> So the only way how to do this use simple shell scripting, like for
> your
> command:
>
> ansible-playbook site.yml --limit=`echo host{010..027} | tr \ ,`

You might want to use seq instead so you don't need to pipe to tr.

ansible-playbook site.yml --limit=$(seq -s, -f 'host%03g' 10 27)


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages