Пример #1
0
    def run_playbook(self, playbook_file, inventory_file=None, **kwargs):
        reload(constants)

        if not os.path.isfile(playbook_file):
            raise exceptions.FileNotFound(name=playbook_file)

        if inventory_file is None:
            inventory_file = self.inventory_file

        LOG.debug('Running with inventory file: %s', inventory_file)
        LOG.debug('Running with playbook file: %s', playbook_file)

        conn_pass = None
        if 'conn_pass' in kwargs:
            conn_pass = kwargs['conn_pass']

        become_pass = None
        if 'become_pass' in kwargs:
            become_pass = kwargs['become_pass']

        passwords = {'conn_pass': conn_pass, 'become_pass': become_pass}

        playbooks = [playbook_file]

        options = self._build_opt_dict(inventory_file, **kwargs)

        variable_manager = vars.VariableManager()
        loader = dataloader.DataLoader()
        options.extra_vars = {six.u(key): six.u(value)
                              for key, value in options.extra_vars.items()}
        variable_manager.extra_vars = options.extra_vars

        ansible_inv = inventory.Inventory(loader=loader,
                                          variable_manager=variable_manager,
                                          host_list=options.inventory)
        ansible_inv.set_playbook_basedir(os.path.dirname(playbook_file))
        variable_manager.set_inventory(ansible_inv)
        ansible_inv.subset(options.subset)

        pbex = playbook_executor.PlaybookExecutor(
            playbooks=playbooks,
            inventory=ansible_inv,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords)
        self.tqm = pbex._tqm
        errors_callback = ErrorsCallback()
        self.add_callback(errors_callback)
        # There is no public API for adding callbacks, hence we use a private
        # property to add callbacks
        pbex._tqm._callback_plugins.extend(self._callbacks)

        status = pbex.run()
        stats = pbex._tqm._stats
        failed_results = errors_callback.failed_results
        result = self._process_stats(stats, failed_results)
        return result
Пример #2
0
    def run_playbook(self, playbook_file, inventory_file=None, **kwargs):
        reload(constants)

        if not os.path.isfile(playbook_file):
            raise exceptions.FileNotFound(name=playbook_file)

        if inventory_file is None:
            inventory_file = self.inventory_file

        LOG.debug('Running with inventory file: %s', inventory_file)
        LOG.debug('Running with playbook file: %s', playbook_file)

        conn_pass = None
        if 'conn_pass' in kwargs:
            conn_pass = kwargs['conn_pass']

        become_pass = None
        if 'become_pass' in kwargs:
            become_pass = kwargs['become_pass']

        passwords = {'conn_pass': conn_pass, 'become_pass': become_pass}

        playbooks = [playbook_file]

        options = self._build_opt_dict(inventory_file, **kwargs)

        if six.PY2:
            options.extra_vars = json.loads(json.dumps(options.extra_vars))

        loader = dataloader.DataLoader()
        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
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        variable_manager.extra_vars = options.extra_vars
        inventory.subset(options.subset)
        pbex = playbook_executor.PlaybookExecutor(
            playbooks=playbooks,
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords)
        self.tqm = pbex._tqm
        errors_callback = ErrorsCallback()
        self.add_callback(errors_callback)
        # There is no public API for adding callbacks, hence we use a private
        # property to add callbacks
        pbex._tqm._callback_plugins.extend(self._callbacks)
        try:
            pbex.run()
        except errors.AnsibleParserError as e:
            raise exceptions.ParsePlaybookError(msg=str(e))
        stats = pbex._tqm._stats
        failed_results = errors_callback.failed_results
        result = self._process_stats(stats, failed_results)
        return result