New feature "--limit" on ansible-playbook, confining patterns to part of your inventory

Sometimes you want to run a playbook on a cross section of your infrastructure.

There are multiple reasons for this.

Assume the following inventory for an example of overlapping groups:

[webservers]
wa
wb
wc
wd
we

[databaseservers]
da
db
dc
dd
de

[site-one]
wa
wb
wc
da
db
dc

[site-two]
wd
we
dd
de

[A]

assume I have a playbook that targets all of my webservers and database servers, however I want to do a rolling deployment.

hosts: webservers
tasks:
   - blarg...

hosts: dbservers
tasks:
    - blarg...

I can NOW do:

ansible-playbook example.yml --limit site-one
ansible-playbook example.yml --limit site-two

[B]

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.

[C]

The same could be done to limit a test of an initial production rollout to a small portion of the total hosts, with the intent to run them on the whole group if successful. In this case, just two servers:

ansible-playbook example.yml --limit wa,da

**** NEXT STEPS FOR EXPANDING THIS FEATURE ****

The next steps for this is to make patterns support the concept of range selection.

For instance, the following syntax does NOT work yet, but I want to make it work very soon:

ansible-playbook example.yml --limit all[0%-10%]

The above would run the playbook on the first 10% of all hosts in inventory.

AND

ansible-playbook example.yml --limit webservers[0-99];dbservers[0-5]

The above, hypotethically, would command the first 100 webservers, and the first six database servers.

(This should be pretty easy to do, and will also mean that patterns will be able to support this kind of magic, so you could use the same syntax in /usr/bin/ansible for the first argument.)

Comments and questions welcome!

--Michael

Dear Michael,

I am replying to an ancient message of yours:

  https://groups.google.com/d/msg/ansible-project/qfoeqytbRE4/SI58rlzeEwMJ

Do you think it would be useful to be able to use logical NOT in
--limit calls? I have a group 'down' into which I put hosts that are
currently unreachable, and it would be good to be able to say:

  ansible-playbook site.yml --limit '!down'

Rather than having to append ':!down' to the hosts: line of every
playbook.

Thoughts?

I’m fine with patches to support this.

I believe in the past previous attempts were incomplete and couldn’t be merged in.

I’ve used

ansible-playbook site.yml --limit ‘all:!down’

in the past. I assume that still works.

K

Ok, great!

also sprach Kahlil Hodgson <kahlil.hodgson@dealmax.com.au> [2013.06.19.2253 +0200]:

I've used

    ansible-playbook site.yml --limit 'all:!down'

in the past. I assume that still works.

Thanks for the hint!
https://github.com/ansible/ansible/pull/3275

Looks good to me.