Пример #1
0
    def parse(self, inventory, loader, path, cache=False):
        ''' parses the inventory file '''

        super(InventoryModule, self).parse(inventory, loader, path, cache=cache)

        self._read_config_data(path)

        strict = self.get_option('strict')
        fact_cache = FactCache()
        try:
            # Go over hosts (less var copies)
            for host in inventory.hosts:

                # get available variables to templar
                hostvars = combine_vars(get_group_vars(inventory.hosts[host].get_groups()), inventory.hosts[host].get_vars())
                if host in fact_cache:  # adds facts if cache is active
                    hostvars = combine_vars(hostvars, fact_cache[host])

                # create composite vars
                self._set_composite_vars(self.get_option('compose'), hostvars, host, strict=strict)

                # refetch host vars in case new ones have been created above
                hostvars = combine_vars(get_group_vars(inventory.hosts[host].get_groups()), inventory.hosts[host].get_vars())
                if host in self._cache:  # adds facts if cache is active
                    hostvars = combine_vars(hostvars, self._cache[host])

                # constructed groups based on conditionals
                self._add_host_to_composed_groups(self.get_option('groups'), hostvars, host, strict=strict)

                # constructed groups based variable values
                self._add_host_to_keyed_groups(self.get_option('keyed_groups'), hostvars, host, strict=strict)

        except Exception as e:
            raise AnsibleParserError("failed to parse %s: %s " % (to_native(path), to_native(e)))
Пример #2
0
def remove_inherited_and_overridden_vars(vars, group, inventory):
    if group not in _vars:
        _vars[group] = get_group_vars(group, inventory)
    gv = _vars[group]
    for (k, v) in vars.items():
        if k in gv:
            if gv[k] == v:
                vars.pop(k)
            else:
                gv.pop(k)
Пример #3
0
    def host_groupvars(self, host, loader, sources):
        ''' requires host object '''
        gvars = get_group_vars(host.get_groups())

        if self.get_option('use_vars_plugins'):
            gvars = combine_vars(
                gvars,
                get_vars_from_inventory_sources(loader, sources,
                                                host.get_groups(), 'all'))

        return gvars
Пример #4
0
def get_group_vars(group, inventory):
    try:
        from ansible.inventory.helpers import get_group_vars
        return get_group_vars(inventory.groups.values())
    except ImportError:
        pass
    # http://stackoverflow.com/a/197053
    vars = inspect.getargspec(inventory.get_group_vars)
    if 'return_results' in vars[0]:
        return inventory.get_group_vars(group, return_results=True)
    else:
        return inventory.get_group_vars(group)
Пример #5
0
    def parse(self, inventory, loader, path, cache=True):

        super(InventoryModule, self).parse(inventory, loader, path)
        self._read_config_data(path)
        self._connect_to_libvirt()
        self.inventory.add_group('libvirt')

        strict = self.get_option('strict')

        for dom in self._lv.listAllDomains():
            host = dom.name()
            LOG.info('inspecting %s', host)

            if not self.get_option('include_inactive') and \
                    dom.state() != libvirt.VIR_DOMAIN_RUNNING:
                LOG.info('skipping %s (not running)', host)
                continue

            self.inventory.add_host(host)
            self.inventory.add_child('libvirt', host)

            address = self._lookup_dom_address(host, dom)
            if address is None:
                LOG.warning('failed to find address for %s', host)
            else:
                self.inventory.set_variable(host, 'ansible_host', address)

            self._set_libvirt_vars(host, dom)

            hostvars = combine_vars(
                get_group_vars(inventory.hosts[host].get_groups()),
                inventory.hosts[host].get_vars())

            # create composite vars
            self._set_composite_vars(self.get_option('compose'),
                                     hostvars,
                                     host,
                                     strict=strict)
            # constructed groups based on conditionals
            self._add_host_to_composed_groups(self.get_option('groups'),
                                              hostvars,
                                              host,
                                              strict=strict)

            # constructed groups based variable values
            self._add_host_to_keyed_groups(self.get_option('keyed_groups'),
                                           hostvars,
                                           host,
                                           strict=strict)
Пример #6
0
    def parse(self, inventory, loader, path, cache=True):
        data = list(yaml.safe_load_all(open(path, encoding="UTF-8")))
        assert len(data) > 0

        nibs = {}
        for k, host in inventory.hosts.items():
            v = get_group_vars(host.groups)
            v.update(host.get_vars())

            if "nib" in v and isinstance(v["nib"], list):
                nibs[k] = v["nib"]

        rows = parse(*data[1:])
        for row in rows:
            if "host" in row:
                host = row["host"]["label"]
                if host in nibs:
                    nibs[host].append(row)
                else:
                    nibs[host] = [row]

        for name, nib in nibs.items():
            inventory.add_host(name)
            inventory.set_variable(name, "nib", nib)
Пример #7
0
 def groups_inventory():
     ''' gets group vars from inventory '''
     return get_group_vars(host_groups)
Пример #8
0
def remove_inherited_and_overridden_group_vars(group, inventory):
    if group not in _vars:
        _vars[group] = get_group_vars(group, inventory)
    for ancestor in group.get_ancestors():
        remove_inherited_and_overridden_vars(_vars[group], ancestor, inventory)
Пример #9
0
 def groups_inventory():
     ''' gets group vars from inventory '''
     return get_group_vars(host_groups)
Пример #10
0
def open_ssh(host, command):
    parameters = ['ssh']
    ansible_vars = host.get_vars()
    ansible_group_vars = get_group_vars(host.get_groups())

    # ansible_user
    #   The default ssh user name to use.
    username = ansible_vars.get('ansible_user') \
        or ansible_vars.get('ansible_ssh_user')

    if username is None:
        username = ansible_group_vars.get('ansible_user') \
            or ansible_group_vars.get('ansible_ssh_user')


    userpasswd = ansible_vars.get('ansible_ssh_pass') or ansible_group_vars.get('ansible_ssh_pass')

    sudopw = ansible_vars.get('ansible_become_pass') or ansible_group_vars.get('ansible_become_pass')

    if username is not None:
        parameters.append('-l')
        parameters.append(username)

    # ansible_ssh_private_key_file
    #   Private key file used by ssh.
    #   Useful if using multiple keys and you don't want to use SSH agent.
    identity_file = ansible_vars.get('ansible_ssh_private_key_file')

    if identity_file is not None:
        path = os.path.expanduser(identity_file)
        path = os.path.expandvars(path)
        path = os.path.abspath(path)

        parameters.append('-i')
        parameters.append(path)

    # ansible_port
    #   The ssh port number, if not 22
    port = ansible_vars.get('ansible_port') or 22

    if port is not None:
        parameters.append('-p')
        parameters.append(str(port))

    # ansible_host
    #   The name of the host to connect to, if different from the alias you
    #   wish to give to it.
    hostname = ansible_vars.get('ansible_host') \
        or ansible_vars.get('ansible_ssh_host') \
        or host.name

    # ansible_ssh_common_args
    #   This setting is always appended to the default command line for
    #   sftp, scp, and ssh. Useful to configure a ``ProxyCommand`` for a
    #   certain host (or group).
    ssh_args = ansible_vars.get('ansible_ssh_extra_args') \
        or ansible_group_vars.get('ansible_ssh_extra_args') or ""

    # ansible_sftp_extra_args
    #   This setting is always appended to the default sftp command line.

    # ansible_scp_extra_args
    #   This setting is always appended to the default scp command line.

    # ansible_ssh_extra_args
    #   This setting is always appended to the default ssh command line.

    parameters.append(hostname)
    parameters.extend(ssh_args.split())
    parameters.extend(command)

    if userpasswd is not None:
        parameters.insert(0, "sshpass -p \"" + userpasswd + "\"" )
        os.system(" ".join(parameters))
    else:
        print(" ".join(parameters))
        os.system(" ".join(parameters))
Пример #11
0

for host in inventory_manager.hosts.values():
    data["nodes"].append({
        "id": node_id(host),
        "label": host.name,
        "leaf": True,
        "meta": serialize(variable_manager.get_vars(host=host)),
    })

for group in inventory_manager.groups.values():
    data["nodes"].append({
        "id": node_id(group),
        "label": group.name,
        "leaf": False,
        "meta": serialize(get_group_vars([group])),
    })

for host in inventory_manager.hosts.values():
    for parent in select_direct_parents(host.groups):
        data["edges"].append({
            "from": node_id(parent),
            "to": node_id(host),
        })

for group in inventory_manager.groups.values():
    for parent in select_direct_parents(group.parent_groups):
        data["edges"].append({
            "from": node_id(parent),
            "to": node_id(group),
        })