Skip to content

Commit 8fa4dc3

Browse files
committed
Small change to previous patch, make ranges of hosts inclusive.
1 parent 416b8d5 commit 8fa4dc3

File tree

7 files changed

+39
-45
lines changed

7 files changed

+39
-45
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Ansible Changes By Release
4545
* minor caching logic added to inventory to reduce hammering of inventory scripts.
4646
* ./hacking/test-module now supports options like ansible takes and has a debugger mode
4747
* playbook is now colorized, set ANSIBLE_NOCOLOR=1 if you do not like this, does not colorize if not a TTY
48+
* support ranges of hosts in the host file
4849

4950
0.5 "Amsterdam" ------- July 04, 2012
5051

docs/man/man1/ansible-playbook.1

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
.\" Title: ansible-playbook
33
.\" Author: [see the "AUTHOR" section]
44
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
5-
.\" Date: 07/04/2012
5+
.\" Date: 07/24/2012
66
.\" Manual: System administration commands
77
.\" Source: Ansible 0.6
88
.\" Language: English
99
.\"
10-
.TH "ANSIBLE\-PLAYBOOK" "1" "07/04/2012" "Ansible 0\&.6" "System administration commands"
10+
.TH "ANSIBLE\-PLAYBOOK" "1" "07/24/2012" "Ansible 0\&.6" "System administration commands"
1111
.\" -----------------------------------------------------------------
1212
.\" * set default formatting
1313
.\" -----------------------------------------------------------------

docs/man/man1/ansible.1

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
.\" Title: ansible
33
.\" Author: [see the "AUTHOR" section]
44
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
5-
.\" Date: 07/04/2012
5+
.\" Date: 07/24/2012
66
.\" Manual: System administration commands
77
.\" Source: Ansible 0.6
88
.\" Language: English
99
.\"
10-
.TH "ANSIBLE" "1" "07/04/2012" "Ansible 0\&.6" "System administration commands"
10+
.TH "ANSIBLE" "1" "07/24/2012" "Ansible 0\&.6" "System administration commands"
1111
.\" -----------------------------------------------------------------
1212
.\" * set default formatting
1313
.\" -----------------------------------------------------------------
@@ -140,19 +140,9 @@ Connection type to use\&. Possible options are
140140
.RE
141141
.SH "INVENTORY"
142142
.sp
143-
Ansible stores the hosts it can potentially operate on in an inventory
144-
file\&. The syntax is one host per line\&. Optionally, ansible can use a
145-
line of the form base[beg:end]tail to define a set of hosts, where
146-
[beg:end] defines a numerical range. If 'beg' is left out, it
147-
defaults to 0\&. An example: mail[1:6].example.com, where 'head'
148-
is 'mail', 'beg' is 1, 'end' is 6, and 'tail' is '.example.com'\&. In
149-
addition, 'beg' can be a a string padded with zero(s) to the left. If so
150-
provided, it acts as a formatting hint during hostname expansion. The usage
151-
must be confirmed by having an 'end' that has the same length as 'beg',
152-
else an exception is raised. An example: mail[001:003].example.com is to be
153-
expanded to mail001.example.com, mail002.example.com, and
154-
mail003.example.com\&. Groups headers are allowed and are included on their
155-
own line, enclosed in square brackets\&.
143+
Ansible stores the hosts it can potentially operate on in an inventory file\&. The syntax is one host per line\&. Groups headers are allowed and are included on their own line, enclosed in square brackets that start the line\&.
144+
.sp
145+
Ranges of hosts are also supported\&. For more information and additional options, see the documentation on http://ansible\&.github\&.com/\&.
156146
.SH "FILES"
157147
.sp
158148
/etc/ansible/hosts \(em Default inventory file

docs/man/man1/ansible.1.asciidoc.in

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ A name of a group in the inventory file, a shell-like glob selecting
3131
hosts in inventory file, or any combination of the two separated by
3232
semicolons.
3333

34-
3534
OPTIONS
3635
-------
3736

@@ -112,7 +111,11 @@ INVENTORY
112111

113112
Ansible stores the hosts it can potentially operate on in an inventory
114113
file. The syntax is one host per line. Groups headers are allowed and
115-
are included on their own line, enclosed in square brackets.
114+
are included on their own line, enclosed in square brackets that
115+
start the line.
116+
117+
Ranges of hosts are also supported. For more information and
118+
additional options, see the documentation on http://ansible.github.com/.
116119

117120
FILES
118121
-----

examples/hosts

+19-20
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,38 @@
77
# - Groups of hosts are delimited by [header] elements
88
# - You can enter hostnames or ip addresses
99
# - A hostname/ip can be a member of multiple groups
10-
#
10+
1111
# Ex 1: Ungrouped hosts, specify before any group headers.
12-
green.bikeshed.org
13-
blue.bikeshed.org
14-
red.bikeshed.org
15-
bikeshed.org
16-
bastion.secure.bikeshed.org
12+
13+
green.example.com
14+
blue.example.com
1715
192.168.100.1
1816
192.168.100.10
19-
# An example for host expansion that uses the default 'beg' and an 'end'
20-
mail[:5].example.com
2117

2218
# Ex 2: A collection of hosts belonging to the 'webservers' group
19+
2320
[webservers]
24-
www01.bikeshed.org
25-
www02.bikeshed.org
26-
wheel.colors.com
21+
alpha.example.org
22+
beta.example.org
2723
192.168.1.100
2824
192.168.1.110
29-
# Your personal website also runs a webserver:
30-
myserver.com
31-
# An example for host expansion that uses both a 'beg' and an 'end', with
32-
# the 'beg' acting as a formatting hint during host name expansion
25+
26+
# If you have multiple hosts following a pattern you can specify
27+
# them like this:
28+
3329
www[001:006].example.com
3430

3531
# Ex 3: A collection of database servers in the 'dbservers' group
32+
3633
[dbservers]
34+
3735
db01.intranet.mydomain.net
38-
10.25.1.56
3936
db02.intranet.mydomain.net
37+
10.25.1.56
4038
10.25.1.57
41-
# Perhaps you serve a db off your personal server too:
42-
myserver.com
43-
# An example for host expansion that uses a regular 'beg' and a regular
44-
# 'end'
39+
40+
# Here's another example of host ranges, this time there are no
41+
# leading 0s:
42+
4543
db-[99:101]-node.example.com
44+

lib/ansible/inventory/expand_hosts.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this software. If not, see <http://www.gnu.org/licenses/>.
1818
#
19+
1920
'''
2021
This module is for enhancing ansible's inventory parsing capability such
2122
that it can deal with hostnames specified using a simple pattern in the
@@ -69,21 +70,21 @@ def expand_hostname_range(line = None):
6970
(head, nrange, tail) = line.replace('[','|').replace(']','|').split('|')
7071
bounds = nrange.split(":")
7172
if len(bounds) != 2:
72-
raise ValueError("host range incorrectly specified!")
73+
raise ValueError("host range incorrectly specified")
7374
beg = bounds[0]
7475
end = bounds[1]
7576
if not beg:
7677
beg = "0"
7778
if not end:
78-
raise ValueError("host range end value missing!")
79+
raise ValueError("host range end value missing")
7980
if beg[0] == '0' and len(beg) > 1:
8081
rlen = len(beg) # range length formatting hint
8182
else:
8283
rlen = None
8384
if rlen > 1 and rlen != len(end):
8485
raise ValueError("host range format incorrectly specified!")
8586

86-
for _ in range(int(beg), int(end)):
87+
for _ in range(int(beg), int(end)+1):
8788
if rlen:
8889
rseq = str(_).zfill(rlen) # range sequence
8990
else:

test/simple_hosts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
jupiter
22
saturn
3-
thrudgelmir[:6]
3+
thrudgelmir[:5]
44

55
[greek]
66
zeus
77
hera:3000
88
poseidon
9-
cerberus[001:004]
10-
cottus[99:101]
9+
cerberus[001:003]
10+
cottus[99:100]
1111

1212
[norse]
1313
thor

0 commit comments

Comments
 (0)