From charlesreid1

Automatically Labeling Hosts

As the number of hosts grows, we may want to deal with numbered hosts instead of individually-named hosts.

Numbering Hosts

Vagrant allows the use of bracketed range notation to automatically label a large number of hosts with integers:

[web]
web[1:20].example.com

To zero-pad the integers,

[web]
web[01:20].example.com

Lettering Hosts

Likewise, letters can also be used:

[web]
web-[a-q].example.com

(last letter is however many letters you need.)

Example

As an example, we can slim down the following inventory (hosts) file:

[production]
delaware.example.com
georgia.example.com
maryland.example.com
newhampshire.example.com
newjersey.example.com
newyork.example.com
northcarolina.example.com
pennsylvania.example.com
rhodeisland.example.com
virginia.example.com

[staging]
ontario.example.com
quebec.example.com

[vagrant]
vagrant1 ansible_host=127.0.0.1 ansible_port=2222
vagrant2 ansible_host=127.0.0.1 ansible_port=2200
vagrant3 ansible_host=127.0.0.1 ansible_port=2201

[lb]
delaware.example.com

[web]
georgia.example.com
newhampshire.example.com
newjersey.example.com
ontario.example.com
vagrant1

[task]
newyork.example.com
northcarolina.example.com
maryland.example.com
ontario.example.com
vagrant2

[rabbitmq]
pennsylvania.example.com
quebec.example.com
vagrant3

[db]
rhodeisland.example.com
virginia.example.com
quebec.example.com
vagrant3

Now, using numbered ranges:

[production]
prod[1:10].example.com

[staging]
stage[1:2].example.com

[vagrant]
vagrant1 ansible_host=127.0.0.1 ansible_port=2222
vagrant2 ansible_host=127.0.0.1 ansible_port=2200
vagrant3 ansible_host=127.0.0.1 ansible_port=2201

[lb]
prod1.example.com

[web]
prod[2:4].example.com
stage1.example.com
vagrant1

[task]
prod[5:7].example.com
stage1.example.com
vagrant2

[rabbitmq]
prod8.example.com
stage2.example.com
vagrant3

[db]
prod[9:10].example.com
stage2.example.com
vagrant3

Flags