Python ansible.inventory.manager.InventoryManager() Examples

The following are 21 code examples for showing how to use ansible.inventory.manager.InventoryManager(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module ansible.inventory.manager , or try the search function .

Example 1
Project: im   Author: grycap   File: ansible_launcher.py    License: GNU General Public License v3.0 7 votes vote down vote up
def get_play_prereqs_2_4(self, options):
        loader = DataLoader()

        if self.vault_pass:
            loader.set_vault_secrets([('default', VaultSecret(_bytes=to_bytes(self.vault_pass)))])

        # create the inventory, and filter it based on the subset specified (if any)
        inventory = InventoryManager(loader=loader, sources=options.inventory)

        # create the variable manager, which will be shared throughout
        # the code, ensuring a consistent view of global variables
        try:
            # Ansible 2.8
            variable_manager = VariableManager(loader=loader, inventory=inventory,
                                               version_info=self.version_info(ansible_version))
            variable_manager._extra_vars = self.extra_vars
        except TypeError:
            variable_manager = VariableManager(loader=loader, inventory=inventory)
            variable_manager.extra_vars = self.extra_vars
            variable_manager.options_vars = {'ansible_version': self.version_info(ansible_version)}

        return loader, inventory, variable_manager 
Example 2
Project: Practical-Network-Automation-Second-Edition   Author: PacktPublishing   File: checkpython.py    License: MIT License 7 votes vote down vote up
def ansible_part():
    playbook_path = "checktemplate.yml"
    inventory_path = "hosts"

    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
    loader = DataLoader()
    options = Options(connection='local', module_path='', forks=100, become=None, become_method=None, become_user=None, check=False,
                    diff=False, listhosts=False, listtasks=False, listtags=False, syntax=False)
    passwords = dict(vault_pass='secret')

    inventory = InventoryManager(loader=loader, sources=['inventory'])
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    executor = PlaybookExecutor(  
                playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,  
                options=options, passwords=passwords)  
    results = executor.run()  
    print results 
Example 3
Project: tidb-ansible   Author: pingcap   File: inventory_check.py    License: Apache License 2.0 6 votes vote down vote up
def parse_inventory(inventory):
    loader = DataLoader()
    inv = InventoryManager(loader=loader, sources=[inventory])
    vars = VariableManager(loader=loader, inventory=inv)
    all_groups = inv.get_groups_dict()
    tidb_nodes = all_groups['tidb_servers']
    tikv_nodes = all_groups['tikv_servers']
    tidb_servers = {}
    tikv_servers = {}
    for tidb in tidb_nodes:
        var = vars.get_vars(host=inv.get_host(hostname=str(tidb)))
        ip = var['ansible_host'] if 'ansible_host' in var else var['inventory_hostname']
        tidb_port = var.get('tidb_port', 4000)
        tidb_status_port = var.get('tidb_status_port', 10080)
        deploy_dir = var['deploy_dir']

        if ip in tidb_servers:
            tidb_servers[ip].append([tidb_port, tidb_status_port, deploy_dir])
        else:
            tidb_servers[ip] = [[tidb_port, tidb_status_port, deploy_dir]]

    for tikv in tikv_nodes:
        var = vars.get_vars(host=inv.get_host(hostname=str(tikv)))
        ip = var['ansible_host'] if 'ansible_host' in var else var['inventory_hostname']
        tikv_port = var.get('tikv_port', 20160)
        tikv_status_port = var.get('tikv_status_port', 20180)
        deploy_dir = var['deploy_dir']

        if ip in tikv_servers:
            tikv_servers[ip].append([tikv_port, tikv_status_port, deploy_dir])
        else:
            tikv_servers[ip] = [[tikv_port, tikv_status_port, deploy_dir]]

    return [tidb_servers, tikv_servers] 
Example 4
Project: Learning-Python-Networking-Second-Edition   Author: PacktPublishing   File: execute_playbook.py    License: MIT License 6 votes vote down vote up
def execute_playbook():
    playbook_path = "playbook_template.yml"
    inventory_path = "hosts"

    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff', 'listhosts', 'listtasks', 'listtags', 'syntax'])
    loader = DataLoader()
    options = Options(connection='local', module_path='', forks=100, become=None, become_method=None, become_user=None, check=False,
                    diff=False, listhosts=False, listtasks=False, listtags=False, syntax=False)
    passwords = dict(vault_pass='secret')

    inventory = InventoryManager(loader=loader, sources=['inventory'])
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    executor = PlaybookExecutor(  
                playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader,  
                options=options, passwords=passwords)  
    results = executor.run()  
    print results 
Example 5
Project: ops-cli   Author: adobe   File: generator.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, inventory_generator):
        """
        :type inventory_generator: ops.inventory.generator.InventoryGenerator
        """

        self.inventory_generator = inventory_generator
        self.generated_path, self.ssh_config_path = inventory_generator.generate()

        # clean up variables cache for tests
        ansible_vars.VARIABLE_CACHE = dict()
        ansible_vars.HOSTVARS_CACHE = dict()
        ansible_inventory.HOSTS_PATTERNS_CACHE = dict()

        loader = DataLoader()
        loader.set_basedir(self.generated_path)
        self.inventory = InventoryManager(
            loader=loader, sources=[
                self.generated_path])
        self.variable_manager = VariableManager(
            loader=loader, inventory=self.inventory) 
Example 6
Project: infrared   Author: redhat-openstack   File: workspaces.py    License: Apache License 2.0 6 votes vote down vote up
def _get_inventory(self, workspace_name=None):
        """Returns Inventory object for the provided workspace.

           Uses active workspace as default.

           :param workspace_name: workspace name to list nodes from.
        """
        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        # need to have import here to avoid ansible patching
        from ansible.inventory.manager import InventoryManager
        from ansible.parsing.dataloader import DataLoader

        return InventoryManager(DataLoader(), sources=workspace.inventory) 
Example 7
Project: AnsibleUI   Author: lotus-dgas   File: AnsibleApi_v29.py    License: GNU General Public License v3.0 5 votes vote down vote up
def AnsibleExecApi29(task_id, tasks=[], inventory_data=None):
    options = get_default_options()
    context.CLIARGS = ImmutableDict(options)

    loader = DataLoader()
    passwords = dict(vault_pass='secret')
    results_callback = RedisCallBack(task_id)
    # inventory = InventoryManager(loader=loader, sources='localhost,')
    if inventory_data:
        inventory = BaseInventory(inventory_data)
    else:
        inventory = BaseInventory(InventoryInit().get_data())
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    play_source =  dict(
            name="Ansible Play",
            hosts='localhost',
            gather_facts='no',
            tasks=tasks,
        )
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
    tqm = None
    try:
        tqm = MyTaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                passwords=passwords,
                stdout_callback=results_callback,
            )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()

        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)


# Ansible 2.9 版本的 vars/manager.py: VariableManager 未有 extra_vars.setter 
Example 8
Project: commcare-cloud   Author: dimagi   File: main.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def inventory_manager(self):
        return InventoryManager(
            loader=self._ansible_inventory_data_loader,
            sources=self.paths.inventory_source
        ) 
Example 9
Project: DevOps   Author: YoLoveLife   File: playbook.py    License: GNU General Public License v2.0 5 votes vote down vote up
def make_inventory(self, group):
        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=DataLoader(),)
        self.inventory.add_group(str(group.uuid))
        for host in group.hosts.all():
            self.inventory.add_host(host.connect_ip, str(group.uuid), host.sshport) 
Example 10
Project: insights-core   Author: RedHatInsights   File: cluster.py    License: Apache License 2.0 5 votes vote down vote up
def parse_inventory(path):
    inventory = InventoryManager(loader=DataLoader(), sources=path)
    return inventory.get_groups_dict() 
Example 11
Project: swallow   Author: JiaJunZz   File: ansible_api.py    License: MIT License 5 votes vote down vote up
def __init__(self, sources='/etc/ansible/hosts'):
        self.sources = sources
        self.variable_manager = None
        self.loader = None
        self.options = None
        self.passwords = None
        self.callback = ResultCallback()
        self.results_raw = {}
        Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'timeout', 'remote_user',
                                         'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args',
                                         'sftp_extra_args',
                                         'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass',
                                         'verbosity',
                                         'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'diff'])
        self.options = Options(connection='smart', module_path=None, forks=100, timeout=10,
                               remote_user='root', ask_pass=False, private_key_file=None, ssh_common_args=None,
                               ssh_extra_args=None,
                               sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None,
                               become_user='root', ask_value_pass=False, verbosity=None, check=False, listhosts=False,
                               listtasks=False, listtags=False, syntax=False, diff=False)

        self.loader = DataLoader()
        self.passwords = dict(vault_pass='secret')
        self.inventory = InventoryManager(self.loader, self.sources)
        # 把inventory传递给variable_manager管理
        self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory) 
Example 12
Project: pytest-ansible   Author: ansible   File: v28.py    License: MIT License 5 votes vote down vote up
def initialize_inventory(self):
        log.debug("HostManagerV28.initialize_inventory()")
        self.options['loader'] = DataLoader()
        self.options['inventory_manager'] = InventoryManager(loader=self.options['loader'],
                                                             sources=self.options['inventory'])
        self.options['variable_manager'] = VariableManager(loader=self.options['loader'],
                                                           inventory=self.options['inventory_manager'])
        # self.options['inventory_manager'].clear_caches() 
Example 13
Project: pytest-ansible   Author: ansible   File: v25.py    License: MIT License 5 votes vote down vote up
def initialize_inventory(self):
        log.debug("HostManagerV25.initialize_inventory()")
        self.options['loader'] = DataLoader()
        self.options['inventory_manager'] = InventoryManager(loader=self.options['loader'],
                                                             sources=self.options['inventory'])
        self.options['variable_manager'] = VariableManager(loader=self.options['loader'],
                                                           inventory=self.options['inventory_manager'])
        # self.options['inventory_manager'].clear_caches() 
Example 14
Project: pytest-ansible   Author: ansible   File: v24.py    License: MIT License 5 votes vote down vote up
def initialize_inventory(self):
        log.debug("HostManagerV24.initialize_inventory()")
        self.options['loader'] = DataLoader()
        self.options['inventory_manager'] = InventoryManager(loader=self.options['loader'],
                                                             sources=self.options['inventory'])
        self.options['variable_manager'] = VariableManager(loader=self.options['loader'],
                                                           inventory=self.options['inventory_manager'])
        # self.options['inventory_manager'].clear_caches() 
Example 15
Project: imoocc   Author: iopsgroup   File: ansible_api.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self,resource,loader,variable_manager):
        self.resource = resource
        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader, sources=['%s/conf/hostslist'%BASE_DIR])
        # self.variable_manager.set_inventory(self.inventory)
        self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
        self.dynamic_inventory() 
Example 16
def dump_hosts():

    loader = DataLoader()
    inventory = InventoryManager(
        loader=loader,
        sources=yml_file)

    hosts = {}
    groups = inventory.groups
    for group in groups.keys():
        host_list = inventory.get_hosts(group)
        hosts[group] = [host.name for host in host_list]

    return json.dumps(hosts, indent=4) 
Example 17
Project: ansible-playbook-grapher   Author: haidaraM   File: conftest.py    License: MIT License 5 votes vote down vote up
def fixture_inventory_manager(data_loader):
    """
    Return an Ansible  InventoryManager
    :return:
    """
    from ansible.inventory.manager import InventoryManager
    return InventoryManager(loader=data_loader, sources=INVENTORY_FILE) 
Example 18
Project: ansible-review   Author: willthames   File: groupvars.py    License: MIT License 4 votes vote down vote up
def same_variable_defined_in_competing_groups(candidate, options):
    result = Result(candidate.path)
    # assume that group_vars file is under an inventory *directory*
    invfile = os.path.dirname(os.path.dirname(candidate.path))
    global _inv

    try:
        if ANSIBLE > 1:
            loader = ansible.parsing.dataloader.DataLoader()
            try:
                from ansible.inventory.manager import InventoryManager
                inv = _inv or InventoryManager(loader=loader, sources=invfile)
            except ImportError:
                var_manager = VariableManager()
                inv = _inv or ansible.inventory.Inventory(loader=loader,
                                                          variable_manager=var_manager,
                                                          host_list=invfile)
            _inv = inv
        else:
            inv = _inv or ansible.inventory.Inventory(invfile)
            _inv = inv
    except AnsibleError as e:
        result.errors = [Error(None, "Inventory is broken: %s" % e.message)]
        return result

    if hasattr(inv, 'groups'):
        group = inv.groups.get(os.path.basename(candidate.path))
    else:
        group = inv.get_group(os.path.basename(candidate.path))
    if not group:
        # group file exists in group_vars but no related group
        # in inventory directory
        return result
    remove_inherited_and_overridden_group_vars(group, inv)
    group_vars = set(_vars[group].keys())
    child_hosts = group.hosts
    child_groups = group.child_groups
    siblings = set()

    for child_host in child_hosts:
        siblings.update(child_host.groups)
    for child_group in child_groups:
        siblings.update(child_group.parent_groups)
    for sibling in siblings:
        if sibling != group:
            remove_inherited_and_overridden_group_vars(sibling, inv)
            sibling_vars = set(_vars[sibling].keys())
            common_vars = sibling_vars & group_vars
            common_hosts = [host.name for host in set(child_hosts) & set(sibling.hosts)]
            if common_vars and common_hosts:
                for var in common_vars:
                    error_msg_template = "Sibling groups {0} and {1} with common hosts {2} " + \
                                         "both define variable {3}"
                    error_msg = error_msg_template.format(group.name, sibling.name,
                                                          ", ".join(common_hosts), var)
                    result.errors.append(Error(None, error_msg))

    return result 
Example 19
Project: kalliope   Author: kalliope-project   File: ansible_playbook.py    License: GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, **kwargs):
        super(Ansible_playbook, self).__init__(**kwargs)

        self.task_file = kwargs.get('task_file', None)
        self.sudo = kwargs.get('sudo', False)
        self.sudo_user = kwargs.get('sudo_user', False)
        self.sudo_password = kwargs.get('sudo_password', False)

        # check if parameters have been provided
        if self._is_parameters_ok():
            # since the API is constructed for CLI it expects certain options to always be set in the context object
            context.CLIARGS = self._get_options()

            # initialize needed objects
            loader = DataLoader()

            passwords = {'become_pass': self.sudo_password}

            inventory = InventoryManager(loader=loader, sources="localhost,")

            # variable manager takes care of merging all the different sources to give you a unified
            # view of variables available in each context
            variable_manager = VariableManager(loader=loader, inventory=inventory)
            variable_manager.set_inventory(inventory)

            playbooks = None
            with open(self.task_file, 'r') as stream:
                try:
                    playbooks = yaml.full_load(stream)
                except yaml.YAMLError as exc:
                    logger.debug("Ansibe playbook error: {}".format(exc))

            if playbooks is not None:
                # force usage of python 3 interpreter
                playbooks[0].setdefault("vars", {})
                playbooks[0]["vars"]["ansible_python_interpreter"] = "/usr/bin/python3"

                play = Play().load(playbooks[0], variable_manager=variable_manager, loader=loader)

                # Run it - instantiate task queue manager, which takes care of forking and setting up all objects
                # to iterate over host list and tasks
                tqm = None
                try:
                    tqm = TaskQueueManager(
                        inventory=inventory,
                        variable_manager=variable_manager,
                        loader=loader,
                        passwords=passwords,
                        stdout_callback='default',
                        # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
                    )
                    tqm.run(play)  # most interesting data for a play is actually sent to the callback's methods
                finally:
                    # we always need to cleanup child procs and the structres we use to communicate with them
                    if tqm is not None:
                        tqm.cleanup() 
Example 20
Project: ceph-ansible-copilot   Author: pcuzner   File: playbook.py    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
def __init__(self, host_list, callback=None):

        Options = namedtuple('Options',
                             ['connection', 'module_path', 'forks', 'become',
                              'become_method', 'become_user', 'check', 'diff',
                              'listtags', 'listtasks', 'listhosts', 'syntax']
                             )

        self.logger = logging.getLogger('copilot')

        # initialize needed objects
        self.loader = DataLoader()

        self.options = Options(
                               syntax=False,
                               listtags=False,
                               listtasks=False,
                               listhosts=False,
                               connection='ssh',
                               module_path='',
                               forks=100,
                               become=True,
                               become_method='sudo',
                               become_user='root',
                               check=False,
                               diff=False
                       )

        # create inventory and pass to variable manager
        self.inventory = InventoryManager(loader=self.loader,
                                          sources=host_list)

        self.host_list = host_list

        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)

        # from ansible 2.4 the ansible_version is set in the cli module, and
        # since we're using the api we need to set it explicitly to make it
        # available to any playbooks we're asked to run
        self.variable_manager.extra_vars = {
            "ansible_version": cli.version_info(gitinfo=False)
        }

        self.callback = callback
        self.pb_file = None
        self.playbook = None
        self.rc = 0 
Example 21
Project: infrared   Author: redhat-openstack   File: interactive_ssh.py    License: Apache License 2.0 4 votes vote down vote up
def ssh_to_host(hostname, remote_command=None):
    """Compose cmd string of ssh and execute

    Uses Ansible to parse inventory file, gets ssh connection options
    :param hostname: str. Hostname from inventory
    """

    workspace_manager = CoreServices.workspace_manager()
    workspace = workspace_manager.get_active_workspace()
    if workspace is None:
        raise exceptions.IRNoActiveWorkspaceFound()
    inventory_file = workspace.inventory

    from ansible.inventory.manager import InventoryManager
    from ansible.parsing.dataloader import DataLoader
    invent = InventoryManager(DataLoader(), sources=inventory_file)

    host = invent.get_host(hostname)
    if host is None:
        raise exceptions.IRSshException(
            "Host {} is not in inventory {}".format(hostname, inventory_file))

    if _get_magic_var(host, "connection") == "local":
        raise exceptions.IRSshException("Only ssh transport acceptable.")

    cmd = " ".join(["ssh {priv_key} {comm_args}",
                    "{extra_args} -p {port} -t {user}@{host}"])

    cmd_fields = {}
    cmd_fields["user"] = _get_magic_var(host, "remote_user", default="root")
    cmd_fields["port"] = _get_magic_var(host, "port", default=22)
    cmd_fields["host"] = _get_magic_var(host, "remote_addr")

    priv_key = _get_magic_var(host, "private_key_file")
    # NOTE(yfried):
    # ssh client needs key to be in the directory you're running one from
    # ('ssh -i id_rsa ...') or to be provided by absolute path.
    # assume paths are relative to inventory file.
    priv_key = os.path.join(os.path.abspath(os.path.dirname(inventory_file)),
                            priv_key)
    if not os.access(priv_key, os.R_OK):
        raise exceptions.IRSshException("Private key file mentioned does not "
                                        "exist: {}".format(priv_key))

    if priv_key:
        cmd_fields["priv_key"] = "-i {}".format(priv_key)
    else:
        cmd_fields["priv_key"] = ""

    cmd_fields["comm_args"] = _get_magic_var(host, "ssh_common_args")
    cmd_fields["extra_args"] = _get_magic_var(host, "ssh_extra_args")

    compiled_cmd = cmd.format(**cmd_fields)
    LOG.debug("Establishing ssh connection to {} using: {}".format(cmd_fields["host"], compiled_cmd))
    if remote_command is not None:
        compiled_cmd = " ".join(
            [compiled_cmd, '"{}"'.format(remote_command)])

    result = os.WEXITSTATUS(os.system(compiled_cmd))
    LOG.debug("Connection to {} closed".format(cmd_fields["host"]))
    return result