diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 2eda3e6649d..657084c724d 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -33,6 +33,7 @@ from ansible.inventory.group import Group from ansible.inventory.host import Host from ansible.plugins import vars_loader from ansible.utils.vars import combine_vars +from ansible.parsing.utils.addresses import parse_address try: from __main__ import display @@ -83,27 +84,16 @@ class Inventory(object): host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] + self.parser = None + if host_list is None: - self.parser = None + pass elif isinstance(host_list, list): - self.parser = None all = Group('all') self.groups = [ all ] - ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?') - for x in host_list: - m = ipv6_re.match(x) - if m: - all.add_host(Host(m.groups()[0], m.groups()[1])) - else: - if ":" in x: - tokens = x.rsplit(":", 1) - # if there is ':' in the address, then this is an ipv6 - if ':' in tokens[0]: - all.add_host(Host(x)) - else: - all.add_host(Host(tokens[0], tokens[1])) - else: - all.add_host(Host(x)) + for h in host_list: + (host, port) = parse_address(h, allow_ranges=False) + all.add_host(Host(host, port)) elif self._loader.path_exists(host_list): #TODO: switch this to a plugin loader and a 'condition' per plugin on which it should be tried, restoring 'inventory pllugins' if self._loader.is_directory(host_list): diff --git a/lib/ansible/inventory/expand_hosts.py b/lib/ansible/inventory/expand_hosts.py index 0d63ba08bbc..7e1c127c27e 100644 --- a/lib/ansible/inventory/expand_hosts.py +++ b/lib/ansible/inventory/expand_hosts.py @@ -44,7 +44,7 @@ def detect_range(line = None): Returnes True if the given line contains a pattern, else False. ''' - if 0 <= line.find("[") < line.find(":") < line.find("]"): + if '[' in line: return True else: return False diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index 36115c57975..30408048136 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -29,6 +29,7 @@ from ansible.inventory.host import Host from ansible.inventory.group import Group from ansible.inventory.expand_hosts import detect_range from ansible.inventory.expand_hosts import expand_hostname_range +from ansible.parsing.utils.addresses import parse_address from ansible.utils.unicode import to_unicode, to_bytes class InventoryParser(object): @@ -265,30 +266,20 @@ class InventoryParser(object): optional port number that applies to all of them. ''' - # Is a port number specified? - # - # This may be a mandatory :NN suffix on any square-bracketed expression - # (IPv6 address, IPv4 address, host name, host pattern), or an optional - # :NN suffix on an IPv4 address, host name, or pattern. IPv6 addresses - # must be in square brackets if a port is specified. + # Can the given hostpattern be parsed as a host with an optional port + # specification? - port = None + (pattern, port) = parse_address(hostpattern, allow_ranges=True) + if not pattern: + self._raise_error("Can't parse '%s' as host[:port]" % hostpattern) - for type in ['bracketed_hostport', 'hostport']: - m = self.patterns[type].match(hostpattern) - if m: - (hostpattern, port) = m.groups() - continue + # Once we have separated the pattern, we expand it into list of one or + # more hostnames, depending on whether it contains any [x:y] ranges. - # Now we're left with just the pattern, which results in a list of one - # or more hostnames, depending on whether it contains any [x:y] ranges. - # - # FIXME: We could be more strict here about validation. - - if detect_range(hostpattern): - hostnames = expand_hostname_range(hostpattern) + if detect_range(pattern): + hostnames = expand_hostname_range(pattern) else: - hostnames = [hostpattern] + hostnames = [pattern] return (hostnames, port) @@ -374,29 +365,3 @@ class InventoryParser(object): $ # end of the line ''', re.X ) - - # The following patterns match the various ways in which a port number - # may be specified on an IPv6 address, IPv4 address, hostname, or host - # pattern. All of the above may be enclosed in square brackets with a - # mandatory :NN suffix; or all but the first may be given without any - # brackets but with an :NN suffix. - - self.patterns['bracketed_hostport'] = re.compile( - r'''^ - \[(.+)\] # [host identifier] - :([0-9]+) # :port number - $ - ''', re.X - ) - - self.patterns['hostport'] = re.compile( - r'''^ - ((?: # We want to match: - [^:\[\]] # (a non-range character - | # ...or... - \[[^\]]*\] # a complete bracketed expression) - )*) # repeated as many times as possible - :([0-9]+) # followed by a port number - $ - ''', re.X - ) diff --git a/lib/ansible/parsing/utils/addresses.py b/lib/ansible/parsing/utils/addresses.py new file mode 100644 index 00000000000..71675769d27 --- /dev/null +++ b/lib/ansible/parsing/utils/addresses.py @@ -0,0 +1,208 @@ +# Copyright 2015 Abhijit Menon-Sen +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import re + +# Components that match a numeric or alphanumeric begin:end or begin:end:step +# range expression inside square brackets. + +numeric_range = r''' + \[ + (?:[0-9]+:[0-9]+) # numeric begin:end + (?::[0-9]+)? # numeric :step (optional) + \] +''' + +alphanumeric_range = r''' + \[ + (?: + [a-z]:[a-z]| # one-char alphabetic range + [0-9]+:[0-9]+ # ...or a numeric one + ) + (?::[0-9]+)? # numeric :step (optional) + \] +''' + +# Components that match a 16-bit portion of an IPv6 address in hexadecimal +# notation (0..ffff) or an 8-bit portion of an IPv4 address in decimal notation +# (0..255) or an [x:y(:z)] numeric range. + +ipv6_component = r''' + (?: + [0-9a-f]{{1,4}}| # 0..ffff + {range} # or a numeric range + ) +'''.format(range=numeric_range) + +ipv4_component = r''' + (?: + [01]?[0-9]{{1,2}}| # 0..199 + 2[0-4][0-9]| # 200..249 + 25[0-5]| # 250..255 + {range} # or a numeric range + ) +'''.format(range=numeric_range) + +# A hostname label, e.g. 'foo' in 'foo.example.com'. Consists of alphanumeric +# characters plus dashes (and underscores) or valid ranges. The label may not +# start or end with a hyphen or an underscore. This is interpolated into the +# hostname pattern below. We don't try to enforce the 63-char length limit. + +label = r''' + (?:[\w]|{range}) # Starts with an alphanumeric or a range + (?:[\w_-]|{range})* # Then zero or more of the same or [_-] + (? 1: - match = re.match( - '\[(?P[^\]]+)\](:(?P[0-9]+))?', - hostname - ) - if match: - return match.group('ip'), match.group('port') - else: - return hostname, None - elif ':' in hostname: - return hostname.rsplit(':', 1) - return hostname, None + return dict(changed=True, add_host=dict(host_name=name, groups=new_groups, host_vars=host_vars)) diff --git a/test/integration/unicode.yml b/test/integration/unicode.yml index 1044c252705..6e8e073a79d 100644 --- a/test/integration/unicode.yml +++ b/test/integration/unicode.yml @@ -4,10 +4,9 @@ connection: local vars: test_var: 'Ī ī Ĭ ĭ Į į İ ı IJ ij Ĵ ĵ Ķ ķ ĸ Ĺ ĺ Ļ ļ Ľ ľ Ŀ ŀ Ł ł Ń ń Ņ ņ Ň ň ʼn Ŋ ŋ Ō ō Ŏ ŏ Ő ő Œ' - num_hosts: 5 hostnames: - - 'host-#ϬϭϮϯϰ' - - 'host-ͰͱͲͳʹ͵' + - 'host-ϬϭϮϯϰ' + - 'host-fóöbär' - 'host-ΙΚΛΜΝΞ' - 'host-στυφχψ' - 'host-ϬϭϮϯϰϱ' @@ -29,11 +28,11 @@ - 'ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď ď Đ đ Ē ē Ĕ ĕ Ė ė Ę ę Ě ě Ĝ ĝ Ğ ğ Ġ ġ Ģ ģ Ĥ ĥ Ħ ħ Ĩ ĩ' - add_host: - name: '{{hostnames}}.{{item}}' + name: '{{item}}' groups: 'ĪīĬĭ' + ansible_ssh_host: 127.0.0.1 ansible_connection: local - host_id: '{{item}}' - with_sequence: start=1 end={{num_hosts}} format=%d + with_items: hostnames - name: 'A task with unicode extra vars' debug: var=extra_var diff --git a/test/units/parsing/test_addresses.py b/test/units/parsing/test_addresses.py new file mode 100644 index 00000000000..8f4fe3696e4 --- /dev/null +++ b/test/units/parsing/test_addresses.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +import unittest + +from ansible.parsing.utils.addresses import parse_address + +class TestParseAddress(unittest.TestCase): + + tests = { + # IPv4 addresses + '192.0.2.3': ['192.0.2.3', None], + '192.0.2.3:23': ['192.0.2.3', 23], + + # IPv6 addresses + '::': ['::', None], + '::1': ['::1', None], + '[::1]:442': ['::1', 442], + 'abcd:ef98:7654:3210:abcd:ef98:7654:3210': ['abcd:ef98:7654:3210:abcd:ef98:7654:3210', None], + '[abcd:ef98:7654:3210:abcd:ef98:7654:3210]:42': ['abcd:ef98:7654:3210:abcd:ef98:7654:3210', 42], + + # Hostnames + 'some-host': ['some-host', None], + 'some-host:80': ['some-host', 80], + 'some.host.com:492': ['some.host.com', 492], + '[some.host.com]:493': ['some.host.com', 493], + 'a-b.3foo_bar.com:23': ['a-b.3foo_bar.com', 23], + u'fóöbär': [u'fóöbär', None], + u'fóöbär:32': [u'fóöbär', 32], + u'fóöbär.éxàmplê.com:632': [u'fóöbär.éxàmplê.com', 632], + + # Various errors + '': [None, None], + 'some..host': [None, None], + 'some.': [None, None], + '[example.com]': [None, None], + 'some-': [None, None], + 'some-.foo.com': [None, None], + 'some.-foo.com': [None, None], + } + + range_tests = { + '192.0.2.[3:10]': ['192.0.2.[3:10]', None], + '192.0.2.[3:10]:23': ['192.0.2.[3:10]', 23], + 'abcd:ef98::7654:[1:9]': ['abcd:ef98::7654:[1:9]', None], + '[abcd:ef98::7654:[6:32]]:2222': ['abcd:ef98::7654:[6:32]', 2222], + u'fóöb[a:c]r.éxàmplê.com:632': [u'fóöb[a:c]r.éxàmplê.com', 632], + '[a:b]foo.com': ['[a:b]foo.com', None], + 'foo[a:b].com': ['foo[a:b].com', None], + 'foo[a:b]:42': ['foo[a:b]', 42], + 'foo[a-b]-.com': [None, None], + 'foo[a-b]:32': [None, None], + 'foo[x-y]': [None, None], + } + + def test_without_ranges(self): + for t in self.tests: + test = self.tests[t] + + (host, port) = parse_address(t) + assert host == test[0] + assert port == test[1] + + def test_with_ranges(self): + for t in self.range_tests: + test = self.range_tests[t] + + (host, port) = parse_address(t, allow_ranges=True) + assert host == test[0] + assert port == test[1] diff --git a/test/units/plugins/action/test_add_host.py b/test/units/plugins/action/test_add_host.py deleted file mode 100644 index c694d387a3a..00000000000 --- a/test/units/plugins/action/test_add_host.py +++ /dev/null @@ -1,47 +0,0 @@ -import unittest - -from ansible.plugins.action import add_host - - -class TestAddHost(unittest.TestCase): - - def test_hostname(self): - host, port = add_host._parse_ip_host_and_port('some-remote-host') - assert host == 'some-remote-host' - assert port is None - - def test_hostname_with_port(self): - host, port = add_host._parse_ip_host_and_port('some-remote-host:80') - assert host == 'some-remote-host' - assert port == '80' - - def test_parse_ip_host_and_port_v4(self): - host, port = add_host._parse_ip_host_and_port('8.8.8.8') - assert host == '8.8.8.8' - assert port is None - - def test_parse_ip_host_and_port_v4_and_port(self): - host, port = add_host._parse_ip_host_and_port('8.8.8.8:80') - assert host == '8.8.8.8' - assert port == '80' - - def test_parse_ip_host_and_port_v6(self): - host, port = add_host._parse_ip_host_and_port( - 'dead:beef:dead:beef:dead:beef:dead:beef' - ) - assert host == 'dead:beef:dead:beef:dead:beef:dead:beef' - assert port is None - - def test_parse_ip_host_and_port_v6_with_brackets(self): - host, port = add_host._parse_ip_host_and_port( - '[dead:beef:dead:beef:dead:beef:dead:beef]' - ) - assert host == 'dead:beef:dead:beef:dead:beef:dead:beef' - assert port is None - - def test_parse_ip_host_and_port_v6_with_brackets_and_port(self): - host, port = add_host._parse_ip_host_and_port( - '[dead:beef:dead:beef:dead:beef:dead:beef]:80' - ) - assert host == 'dead:beef:dead:beef:dead:beef:dead:beef' - assert port == '80'