コード例 #1
0
ファイル: test_templar.py プロジェクト: nj3110/ansible-1
    def _context(self, variables=None):
        variables = variables or {}

        env = AnsibleEnvironment()
        context = AnsibleContext(env,
                                 parent={},
                                 name='some_context',
                                 blocks={})

        for key, value in variables.items():
            context.vars[key] = value

        return context
コード例 #2
0
    def run(self):
        super(PlaybookDocutizer, self).run()

        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" % playbook)
            if not (os.path.isfile(playbook)):
                raise AnsibleError("the playbook: %s does not appear to be a file" % playbook)

        self._shared_loader_obj = SharedPluginLoaderObj()

        self._loader, self._inventory, self._variable_manager = self._play_prereqs(self.options)

        pbex = PlaybookExecutor(playbooks=self.args, inventory=self._inventory, variable_manager=self._variable_manager, loader=self._loader, options=self.options, passwords={})
        results = pbex.run()

        if isinstance(results, list):
            for p in results:
                plays = []
                for idx, play in enumerate(p['plays']):
                    display.display('Processing play %d: %s' % (idx+1, play.name))

                    if play._included_path is not None:
                        self._loader.set_basedir(play._included_path)
                    else:
                        pb_dir = os.path.realpath(os.path.dirname(p['playbook']))
                        self._loader.set_basedir(pb_dir)

                    hosts = CLI.get_host_list(self._inventory, self.options.subset)
                    # TODO(iwalker): do we really need a host? can this just run with localhost?
                    if len(hosts) == 0:
                        raise AnsibleError('No hosts were specified')

                    # NOTE(iwalker): can loop through all hosts and evaluate the tasks, but since we are not doing any
                    # conditional evaluation, we'll just pick the first host and use that.
                    host = hosts[0]
                    display.v('Processing against host: %s' % (host.get_name()))

                    self._all_vars = self._variable_manager.get_vars(play=play, host=host)
                    play_context = PlayContext(play=play, options=self.options)

                    processed_blocks = []
                    for block in play.compile():
                        block = block.filter_tagged_tasks(play_context, self._all_vars)
                        if not block.has_tasks():
                            continue
                        processed_blocks.append(self._process_block(block))

                    tasks = []
                    for block in processed_blocks:
                        if len(block) == 0:
                            continue
                        for task in block:
                            tasks.append(task)

                    processed_handlers = []
                    for block in play.compile_roles_handlers():
                        processed_handlers.extend(self._process_block(block))

                    play_info = {
                        'filename': p['playbook'],
                        'hosts': hosts,
                        'name': play.name,
                        'roles': play.roles,
                        'tasks': tasks,
                        'handlers': processed_handlers,
                        'become': play.become,
                        'remote_user': play.remote_user,
                    }

                    plays.append(play_info)

                env = AnsibleEnvironment(trim_blocks=True,
                                         extensions=['jinja2.ext.loopcontrols'],
                                         loader=FileSystemLoader(self.options.template_path))

                display.display('Rendering template containing %d plays' % (len(plays)))
                template = env.get_template(self.options.template_master)
                output = template.render(plays=plays,
                                         options=self.options)

                display.display('Saving output to %s' % (self.options.output))
                with codecs.open(self.options.output, mode='w', encoding='utf-8') as f:
                    f.write(output)