Exemple #1
0
    def __init__(self, vars_manager, play, inventory, loader):
        self._lookup = {}
        self._loader = loader

        # temporarily remove the inventory filter restriction
        # so we can compile the variables for all of the hosts
        # in inventory
        restriction = inventory._restriction
        inventory.remove_restriction()
        hosts = inventory.get_hosts(ignore_limits_and_restrictions=True)
        inventory.restrict_to_hosts(restriction)

        # check to see if localhost is in the hosts list, as we
        # may have it referenced via hostvars but if created implicitly
        # it doesn't sow up in the hosts list
        has_localhost = False
        for host in hosts:
            if host.name in C.LOCALHOST:
                has_localhost = True
                break

        # we don't use the method in inventory to create the implicit host,
        # because it also adds it to the 'ungrouped' group, and we want to
        # avoid any side-effects
        if not has_localhost:
            new_host =  Host(name='localhost')
            new_host.set_variable("ansible_python_interpreter", sys.executable)
            new_host.set_variable("ansible_connection", "local")
            new_host.ipv4_address = '127.0.0.1'
            hosts.append(new_host)

        for host in hosts:
            self._lookup[host.name] = vars_manager.get_vars(loader=loader, play=play, host=host, include_hostvars=False)
Exemple #2
0
 def _create_implicit_localhost(self, pattern):
     new_host = Host(pattern)
     new_host.set_variable("ansible_python_interpreter", sys.executable)
     new_host.set_variable("ansible_connection", "local")
     new_host.ipv4_address = '127.0.0.1'
     self.get_group("ungrouped").add_host(new_host)
     return new_host
Exemple #3
0
    def _create_implicit_localhost(self, pattern):
        new_host = Host(pattern)
        new_host.set_variable("ansible_python_interpreter", sys.executable)
        new_host.set_variable("ansible_connection", "local")
        new_host.ipv4_address = '127.0.0.1'

        ungrouped = self.get_group("ungrouped")
        if ungrouped is None:
            self.add_group(Group('ungrouped'))
            ungrouped = self.get_group('ungrouped')
            self.get_group('all').add_child_group(ungrouped)
        ungrouped.add_host(new_host)
        return new_host
Exemple #4
0
    def _parse_base_groups(self):
        # FIXME: refactor

        ungrouped = Group(name='ungrouped')
        all = Group(name='all')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        for line in self.lines:
            line = self._before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[", "").replace("]", "")
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(
                            name=active_group_name)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(
                        name=active_group_name)
            elif line.startswith(";") or line == '':
                pass
            elif active_group_name:
                tokens = shlex.split(line)
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                # Three cases to check:
                # 0. A hostname that contains a range pesudo-code and a port
                # 1. A hostname that contains just a port
                if hostname.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and "]" in hostname and ":" in hostname
                      and (hostname.rindex("]") < hostname.rindex(":"))
                      or ("]" not in hostname and ":" in hostname)):
                    (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k, v) = t.split("=", 1)
                            except ValueError, e:
                                raise AnsibleError(
                                    "Invalid ini entry in %s: %s - %s" %
                                    (self.filename, t, str(e)))
                            v = self._parse_value(v)
                            if k == 'ansible_ssh_host':
                                host.ipv4_address = v
                            host.set_variable(k, v)
                    self.groups[active_group_name].add_host(host)
Exemple #5
0
    def _parse_base_groups(self):
        # FIXME: refactor

        ungrouped = Group(name='ungrouped')
        all = Group(name='all')
        all.add_child_group(ungrouped)

        self.groups = dict(all=all, ungrouped=ungrouped)
        active_group_name = 'ungrouped'

        for line in self.lines:
            line = self._before_comment(line).strip()
            if line.startswith("[") and line.endswith("]"):
                active_group_name = line.replace("[","").replace("]","")
                if ":vars" in line or ":children" in line:
                    active_group_name = active_group_name.rsplit(":", 1)[0]
                    if active_group_name not in self.groups:
                        new_group = self.groups[active_group_name] = Group(name=active_group_name)
                    active_group_name = None
                elif active_group_name not in self.groups:
                    new_group = self.groups[active_group_name] = Group(name=active_group_name)
            elif line.startswith(";") or line == '':
                pass
            elif active_group_name:
                tokens = shlex.split(line)
                if len(tokens) == 0:
                    continue
                hostname = tokens[0]
                port = C.DEFAULT_REMOTE_PORT
                # Three cases to check:
                # 0. A hostname that contains a range pesudo-code and a port
                # 1. A hostname that contains just a port
                if hostname.count(":") > 1:
                    # Possible an IPv6 address, or maybe a host line with multiple ranges
                    # IPv6 with Port  XXX:XXX::XXX.port
                    # FQDN            foo.example.com
                    if hostname.count(".") == 1:
                        (hostname, port) = hostname.rsplit(".", 1)
                elif ("[" in hostname and
                    "]" in hostname and
                    ":" in hostname and
                    (hostname.rindex("]") < hostname.rindex(":")) or
                    ("]" not in hostname and ":" in hostname)):
                        (hostname, port) = hostname.rsplit(":", 1)

                hostnames = []
                if detect_range(hostname):
                    hostnames = expand_hostname_range(hostname)
                else:
                    hostnames = [hostname]

                for hn in hostnames:
                    host = None
                    if hn in self.hosts:
                        host = self.hosts[hn]
                    else:
                        host = Host(name=hn, port=port)
                        self.hosts[hn] = host
                    if len(tokens) > 1:
                        for t in tokens[1:]:
                            if t.startswith('#'):
                                break
                            try:
                                (k,v) = t.split("=", 1)
                            except ValueError, e:
                                raise AnsibleError("Invalid ini entry in %s: %s - %s" % (self.filename, t, str(e)))
                            if k == 'ansible_ssh_host':
                                host.ipv4_address = self._parse_value(v)
                            else:
                                host.set_variable(k, self._parse_value(v))
                    self.groups[active_group_name].add_host(host)