Small change to previous patch, make ranges of hosts inclusive.

This commit is contained in:
Michael DeHaan 2012-07-24 20:10:05 -04:00
parent 416b8d59a9
commit 8fa4dc3920
7 changed files with 39 additions and 45 deletions

View file

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

View file

@ -2,12 +2,12 @@
.\" Title: ansible-playbook
.\" Author: [see the "AUTHOR" section]
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 07/04/2012
.\" Date: 07/24/2012
.\" Manual: System administration commands
.\" Source: Ansible 0.6
.\" Language: English
.\"
.TH "ANSIBLE\-PLAYBOOK" "1" "07/04/2012" "Ansible 0\&.6" "System administration commands"
.TH "ANSIBLE\-PLAYBOOK" "1" "07/24/2012" "Ansible 0\&.6" "System administration commands"
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------

View file

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

View file

@ -31,7 +31,6 @@ A name of a group in the inventory file, a shell-like glob selecting
hosts in inventory file, or any combination of the two separated by
semicolons.
OPTIONS
-------
@ -112,7 +111,11 @@ INVENTORY
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.
are included on their own line, enclosed in square brackets that
start the line.
Ranges of hosts are also supported. For more information and
additional options, see the documentation on http://ansible.github.com/.
FILES
-----

View file

@ -7,39 +7,38 @@
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
#
# Ex 1: Ungrouped hosts, specify before any group headers.
green.bikeshed.org
blue.bikeshed.org
red.bikeshed.org
bikeshed.org
bastion.secure.bikeshed.org
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
# An example for host expansion that uses the default 'beg' and an 'end'
mail[:5].example.com
# Ex 2: A collection of hosts belonging to the 'webservers' group
[webservers]
www01.bikeshed.org
www02.bikeshed.org
wheel.colors.com
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
# Your personal website also runs a webserver:
myserver.com
# An example for host expansion that uses both a 'beg' and an 'end', with
# the 'beg' acting as a formatting hint during host name expansion
# If you have multiple hosts following a pattern you can specify
# them like this:
www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
[dbservers]
db01.intranet.mydomain.net
10.25.1.56
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
# Perhaps you serve a db off your personal server too:
myserver.com
# An example for host expansion that uses a regular 'beg' and a regular
# 'end'
# Here's another example of host ranges, this time there are no
# leading 0s:
db-[99:101]-node.example.com

View file

@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
'''
This module is for enhancing ansible's inventory parsing capability such
that it can deal with hostnames specified using a simple pattern in the
@ -69,13 +70,13 @@ def expand_hostname_range(line = None):
(head, nrange, tail) = line.replace('[','|').replace(']','|').split('|')
bounds = nrange.split(":")
if len(bounds) != 2:
raise ValueError("host range incorrectly specified!")
raise ValueError("host range incorrectly specified")
beg = bounds[0]
end = bounds[1]
if not beg:
beg = "0"
if not end:
raise ValueError("host range end value missing!")
raise ValueError("host range end value missing")
if beg[0] == '0' and len(beg) > 1:
rlen = len(beg) # range length formatting hint
else:
@ -83,7 +84,7 @@ def expand_hostname_range(line = None):
if rlen > 1 and rlen != len(end):
raise ValueError("host range format incorrectly specified!")
for _ in range(int(beg), int(end)):
for _ in range(int(beg), int(end)+1):
if rlen:
rseq = str(_).zfill(rlen) # range sequence
else:

View file

@ -1,13 +1,13 @@
jupiter
saturn
thrudgelmir[:6]
thrudgelmir[:5]
[greek]
zeus
hera:3000
poseidon
cerberus[001:004]
cottus[99:101]
cerberus[001:003]
cottus[99:100]
[norse]
thor