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

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

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])'

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. :slight_smile:

Thanks
frank

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

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)