Exemple #1
0
    def _expand_hostpattern(self, hostpattern):
        '''
        Takes a single host pattern and returns a list of hostnames and an
        optional port number that applies to all of them.
        '''

        # Can the given hostpattern be parsed as a host with an optional port
        # specification?

        try:
            (pattern, port) = parse_address(hostpattern, allow_ranges=True)
        except Exception:
            # not a recognizable host pattern
            pattern = hostpattern
            port = None

        # 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.

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

        return (hostnames, port)
Exemple #2
0
    def _expand_hostpattern(self, hostpattern):
        '''
        Takes a single host pattern and returns a list of hostnames and an
        optional port number that applies to all of them.
        '''

        # Can the given hostpattern be parsed as a host with an optional port
        # specification?

        try:
            (pattern, port) = parse_address(hostpattern, allow_ranges=True)
        except:
            # not a recognizable host pattern
            pattern = hostpattern
            port = None

        # 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.

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

        return (hostnames, port)
Exemple #3
0
def _expand_hostpattern(hostpattern):
    """Expands pattern into list of hosts.

    Takes a single host pattern and returns a list of hostnames.
    :param hostpattern: a single host pattern
    :returns: list of hostnames
    """
    # Can the given hostpattern be parsed as a host with an optional port
    # specification?

    try:
        # pylint: disable=unused-variable
        (pattern, port) = parse_address(hostpattern, allow_ranges=True)
    except:  # noqa pylint: disable=bare-except
        # not a recognizable host pattern
        pattern = hostpattern

    # 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.

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

    return hostnames
Exemple #4
0
def my_expand(host_line):
    hosts = []
    if detect_range(host_line):
        hosts += expand_hostname_range(host_line)
    else:
        hosts += [host_line]
    return hosts
Exemple #5
0
    def parse(self, inventory, loader, path, cache=True):
        super(InventoryModule, self).parse(inventory, loader, path, cache)

        config = self._read_config_data(path)
        strict = self.get_option("strict")

        input_file = csv.DictReader(open(config["source"]))

        for entry in input_file:
            hostvars = {}
            groups = []

            if detect_range(entry["host"]):
                hosts = expand_hostname_range(entry["host"])
            else:
                hosts = [entry["host"]]

            for host in hosts:
                self.inventory.add_host(host)
                for k, v in entry.items():
                    if k not in ["host", "tags"]:
                        if v.startswith("vars:"):
                            varval = v.split("vars:")[1]
                            v = config["vars"].get(varval)
                        if k in config.get("column_replace", {}):
                            add_key = config["column_replace"][k]
                        else:
                            add_key = k
                        hostvars[add_key] = v
                    if k == "groups":
                        groups = v.split(" ")

                for k, v in config.get("defaults", {}).items():
                    if k not in hostvars:
                        hostvars[k] = v

                for k, v in hostvars.items():
                    self.inventory.set_variable(host, k, v)

                self._set_composite_vars(self.get_option("compose"),
                                         hostvars,
                                         host,
                                         strict=strict)
                self._add_host_to_composed_groups(self.get_option("groups"),
                                                  hostvars,
                                                  host,
                                                  strict=strict)
                self._add_host_to_keyed_groups(
                    self.get_option("keyed_groups"),
                    hostvars,
                    host,
                    strict=strict,
                )

                for group in groups:
                    self.inventory.add_group(group=group)
                    self.inventory.add_host(group=group, host=host)
Exemple #6
0
    def add_to_inventory(self, list_of_dicts, config):
        strict = self.get_option("strict")
        for entry in list_of_dicts:
            hostvars = {}
            groups = []

            if detect_range(entry["host"]):
                hosts = expand_hostname_range(entry["host"])
            else:
                hosts = [entry["host"]]

            for host in hosts:
                self.inventory.add_host(host)
                for k, v in entry.items():
                    if k not in ["host", "tags"]:
                        if v.startswith("vars:"):
                            varval = v.split("vars:")[1]
                            v = config["vars"].get(varval)
                        if k in config.get("column_replace", {}):
                            add_key = config["column_replace"][k]
                        else:
                            add_key = k
                        hostvars[add_key] = v
                    if k == "groups":
                        groups = v.split(" ")

                for k, v in config.get("defaults", {}).items():
                    if k not in hostvars:
                        hostvars[k] = v

                for k, v in hostvars.items():
                    self.inventory.set_variable(host, k, v)

                self._set_composite_vars(self.get_option("compose"),
                                         hostvars,
                                         host,
                                         strict=strict)
                self._add_host_to_composed_groups(self.get_option("groups"),
                                                  hostvars,
                                                  host,
                                                  strict=strict)
                self._add_host_to_keyed_groups(
                    self.get_option("keyed_groups"),
                    hostvars,
                    host,
                    strict=strict,
                )

                for group in groups:
                    self.inventory.add_group(group=group)
                    self.inventory.add_host(group=group, host=host)
Exemple #7
0
    def _ini_inventaire(self, filename):

        hosts = []

        f = open(filename)

        for line in f:

            if detect_range(line):
                hosts += expand_hostname_range(line)
            else:
                hosts += [line]

        return hosts