Host names and patterns in Ansible 2
By Abhijit Menon-Sen <firstname.lastname@example.org>
Nearly lost among the many significant changes in Ansible 2 (not yet
released) are a number of related changes to how hostnames and host
patterns are handled.
to target managed nodes; one could match multiple patterns by separating
them with colons, semicolons, or commas, e.g.,
use of colons is now discouraged (and will eventually be deprecated)
because of the conflict with IPv6 addresses, and the (undocumented) use
of semicolons attracts a deprecation warning. Ansible 2 recommends only
This usage applies to the list of target
hosts: for a play, the
host pattern argument to the
ansible command, and the argument
groupname[x-y] syntax is no longer supported. Use
groupname[0:2] to match the first three hosts in a group. The
first host is
g, the last is
g[1:] matches all hosts except
Ansible 2 requires inventory hostnames to be valid IPv4/IPv6 addresses
or hostnames (i.e.,
x, but not
x--). As an extension, it accepts
Unicode word characters in hostname labels. Any mistakes result in
specific parsing errors, not mysterious failures during execution.
Inventory hostnames may also use alphabetic or numeric ranges to define
more than one host. For example,
foo[x:z:2] expands to
foz. Addresses may use numeric ranges:
A number of problems with the parsing of IPv6 addresses have also been
fixed, and their behaviour has been made consistent across the inventory
.ini files) and in playbooks (e.g., in
All of the recommended IPv6 address notations (from spelling out all 128
bits to the various compressed forms) are supported. Addresses with port
numbers must be written as
[addr]:port. One can also use
hexadecimal ranges to define multiple hosts in inventory files, e.g.
A couple of small but necessary bugfixes go hand-in-hand with the
parsing changes, and fix problems with passing IPv6 addresses
Taken together, these changes make it possible to use IPv6 in practice
Bigger on the inside
The changes described above merit only a couple of lines in the
but the implementation involved a complete rewrite of
the inventory file parser and
the address parser.
A variety of incidental bugs were fixed along the way.
The upshot is that the code—for the first time—now imposes syntactic
requirements on host names, addresses, and patterns in a systematic,
documented, testable way.