Exemple #1
0
    def run_playbook(self, playbook, inventory=None, **kwargs):
        six.moves.reload_module(constants)
        if not os.path.isfile(playbook):
            raise exceptions.FileNotFound(name=playbook)

        if inventory is None:
            inventory = self.inventory

        LOGGER.debug('Running with inventory : %s', inventory)
        LOGGER.debug('Running with playbook: %s', playbook)

        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]

        options = self._build_opt_dict(inventory, **kwargs)
        loader = DataLoader()
        ansible_inventory = InventoryManager(loader=loader,
                                             sources=options.inventory)
        variable_manager = VariableManager(loader=loader, inventory=inventory)

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

        ansible_inventory.subset(options.subset)

        pbex = PlaybookExecutor(playbooks=playbooks,
                                inventory=ansible_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
Exemple #2
0
def AnsibleApi(tid, hosts, tasks, sources, add_vars):
    Options = namedtuple('Options', ['remote_user','connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
    options = Options(remote_user=ansible_remote_user, connection='paramiko', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False)
    loader = DataLoader()
    passwords = dict(vault_pass='******')
    inventory = InventoryManager(loader=loader, sources=sources)
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    variable_manager.extra_vars=add_vars
    play_source =  dict(name = "Ansible Play",hosts = hosts,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,
                  options=options,
                  passwords=passwords,
                  stdout_callback=ResultCallback(tid),
              )
        result = tqm.run(play)
        print(result)
    finally:
        if tqm is not None:
            tqm.cleanup()
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemple #3
0
    def build_inventory(self):
        self.clear_caches()

        loader = DataLoader()

        # Ansible 2.2 and 2.3 specific fixes
        if ansible_version.startswith('2.2.') or ansible_version.startswith(
                '2.3.'):
            variable_manager = ansible.vars.VariableManager()
            variable_manager.extra_vars = load_extra_vars(
                loader=loader, options=EmptyOptions())

            inventory = ansible.inventory.Inventory(
                loader=loader,
                variable_manager=variable_manager,
                host_list=self.inventory_path.strpath)
            variable_manager.set_inventory(inventory)
        # Ansible 2.4+
        else:
            inventory = InventoryManager(loader=loader,
                                         sources=self.inventory_path.strpath)
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)
            variable_manager.extra_vars = load_extra_vars(
                loader=loader, options=EmptyOptions())

        return inventory
Exemple #4
0
    def _play_prereqs(options):

        # all needs loader
        loader = DataLoader()

        basedir = getattr(options, 'basedir', False)
        if basedir:
            loader.set_basedir(basedir)

        vault_ids = options.vault_ids
        default_vault_ids = C.DEFAULT_VAULT_IDENTITY_LIST
        vault_ids = default_vault_ids + vault_ids

        vault_secrets = CLI.setup_vault_secrets(loader,
                                                vault_ids=vault_ids,
                                                vault_password_files=options.vault_password_files,
                                                ask_vault_pass=options.ask_vault_pass,
                                                auto_prompt=False)
        loader.set_vault_secrets(vault_secrets)

        # 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
        variable_manager = VariableManager(loader=loader, inventory=inventory)

        # load vars from cli options
        variable_manager.extra_vars = load_extra_vars(loader=loader, options=options)
        variable_manager.options_vars = load_options_vars(options, CLI.version_info(gitinfo=False))

        return loader, inventory, variable_manager
Exemple #5
0
    def get_play_prereqs_2(self, options):
        loader = DataLoader()

        if self.vault_pass:
            loader.set_vault_password(self.vault_pass)

        variable_manager = VariableManager()
        variable_manager.extra_vars = self.extra_vars
        variable_manager.options_vars = {
            'ansible_version': self.version_info(ansible_version)
        }

        # Add this to avoid the Ansible bug:  no host vars as host is not in inventory
        # In version 2.0.1 it must be fixed
        ansible.inventory.HOSTS_PATTERNS_CACHE = {}

        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=options.inventory)
        variable_manager.set_inventory(inventory)

        if self.host:
            inventory.subset(self.host)
        # let inventory know which playbooks are using so it can know the
        # basedirs
        inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

        return loader, inventory, variable_manager
Exemple #6
0
def run_ansible_playbook(playbook_name,
                         inventory_path,
                         extra_vars={},
                         options={}):
    """Running an ansible playbook.
    Args:
        :param `str` playbook_name: The name of the playbook. Only the name, Without dir and extension.
        :param `str` inventory_path: The path of the inventory file. Could be relative if in workspace.
        :param `dict` (optional) extra_vars: Extra variables (i.e. --extra_vars <var>)
        :param 'dict' (optional) options: options to override. see Options class.
    Returns:
        :return: Playbook excecution results.
    """
    # Resolving playbook absolute path
    playbook_path = (playbook_name if os.path.exists(playbook_name) else
                     os.path.join(PLAYBOOKS_DIR, playbook_name + '.yaml'))
    if not os.path.exists(playbook_path):
        raise IOError('No such file: {}'.format(playbook_path))
    # Preparing the playbook
    loader = DataLoader()
    options = Options(**options)
    inventory_manager = InventoryManager(loader, inventory_path)
    variable_manager = VariableManager(loader=loader,
                                       inventory=inventory_manager)
    variable_manager.extra_vars = extra_vars
    playbook_exec = PlaybookExecutor(playbooks=[playbook_path],
                                     inventory=inventory_manager,
                                     variable_manager=variable_manager,
                                     loader=loader,
                                     options=options,
                                     passwords={})
    # Running the playbook
    return playbook_exec.run()
def execute_playbook(playbook_path, key_path, extra_vars={}):
    loader = DataLoader()

    inventory = InventoryManager(loader=loader, sources=["/etc/ansible/hosts"])
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    if not os.path.exists(playbook_path):
        # logging.debug('[INFO] The playbook does not exist')
        sys.exit()

    Options = namedtuple('Options',
                         ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks',
                          'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args',
                          'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user',
                          'verbosity', 'check', 'diff'])

    options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='smart',
                      module_path=None,
                      forks=100, remote_user=None, private_key_file=key_path,
                      ssh_common_args="-o StrictHostKeyChecking=no",
                      ssh_extra_args=None,
                      sftp_extra_args=None, scp_extra_args=None, become=False, become_method='sudo', become_user='******',
                      verbosity=None, check=False, diff=False)

    variable_manager.extra_vars = extra_vars

    passwords = {}

    pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager,
                            loader=loader,
                            options=options, passwords=passwords)
    results = pbex.run()
    return results
Exemple #8
0
    def _play_prereqs(options):

        # all needs loader
        loader = DataLoader()

        vault_ids = options.vault_ids
        default_vault_ids = C.DEFAULT_VAULT_IDENTITY_LIST
        vault_ids = default_vault_ids + vault_ids

        vault_secrets = CLI.setup_vault_secrets(
            loader,
            vault_ids=vault_ids,
            vault_password_files=options.vault_password_files,
            ask_vault_pass=options.ask_vault_pass,
            auto_prompt=False)
        loader.set_vault_secrets(vault_secrets)

        # 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
        variable_manager = VariableManager(loader=loader, inventory=inventory)

        # load vars from cli options
        variable_manager.extra_vars = load_extra_vars(loader=loader,
                                                      options=options)
        variable_manager.options_vars = load_options_vars(
            options, CLI.version_info(gitinfo=False))

        return loader, inventory, variable_manager
Exemple #9
0
	def _prepare_for_run(self, host, extra_vars, playbook):
		module_path = f'{self._base_plays_path}/modules'

		options = InventoryOptions(
			remote_user=self._remote_user,
			private_key_file=self._private_key_file,
			module_path=module_path)

		loader = DataLoader()
		loader.set_vault_secrets(self._default_secret)
		inventory = InventoryManager(loader=loader, sources=f'{host},')
		variable_manager = VariableManager(loader=loader, inventory=inventory)
		variable_manager.extra_vars = extra_vars

		self.play_executor = PlaybookExecutor(
			playbooks=[playbook],
			options=options,
			loader=loader,
			inventory=inventory,
			variable_manager=variable_manager,
			passwords={}
		)
		self.logger_cb = LogCallBack(self._verbosity)
		# TODO: make a PR to ansible core to allowing access to task queue
		# manager property in order to get all execution logs
		self.play_executor._tqm._stdout_callback = self.logger_cb
Exemple #10
0
    def _play_prereqs(options):

        # all needs loader
        loader = DataLoader()

        # vault
        b_vault_pass = None
        if options.vault_password_file:
            # read vault_pass from a file
            b_vault_pass = CLI.read_vault_password_file(
                options.vault_password_file, loader=loader)
        elif options.ask_vault_pass:
            b_vault_pass = CLI.ask_vault_passwords()

        if b_vault_pass is not None:
            loader.set_vault_password(b_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
        variable_manager = VariableManager(loader=loader, inventory=inventory)

        # load vars from cli options
        variable_manager.extra_vars = load_extra_vars(loader=loader,
                                                      options=options)
        variable_manager.options_vars = load_options_vars(
            options, CLI.version_info(gitinfo=False))

        return loader, inventory, variable_manager
def main():
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources='./hosts')
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    playbook_path = './command.yml'

    if not os.path.exists(playbook_path):
        print('[INFO] The playbook does not exist')
        sys.exit()

    Options = namedtuple(
        'Options',
        ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection', 'module_path', 'forks',
         'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
         'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff']
    )
    options = Options(
        listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh',
        module_path=None, forks=100, remote_user='******', private_key_file=None,
        ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True,
        become_method='sudo', become_user='******', verbosity=None, check=False, diff=False
    )

    variable_manager.extra_vars = {'hoge': 'mywebserver'}
    passwords = {}

    pbex = PlaybookExecutor(
        playbooks=[playbook_path],
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,
        options=options,
        passwords=passwords
    )
    results = pbex.run()
Exemple #12
0
def get_ansible_variablemanager(self, **kwargs):
    from ansible.parsing.dataloader import DataLoader
    from ansible.utils.vars import load_extra_vars
    from ansible.vars.manager import VariableManager
    if 'options' in kwargs:
        options = kwargs['options']
    else:
        options = AnsibleOptions()
    if 'loader' in kwargs:
        loader = kwargs['loader']
    else:
        loader = DataLoader()
        vault_secret = get_vault_password_source(self.master.main_config)
        if not isinstance(vault_secret, NullSource):
            loader.set_vault_secrets([(vault_secret.id, vault_secret)])
        basedir = get_playbooks_directory(self.master.ctrl.config)
        loader.set_basedir(basedir)
    if 'inventory' in kwargs:
        inventory = kwargs['inventory']
    else:
        inventory = self.get_ansible_inventorymanager()
    if 'variable_manager' in kwargs:
        variable_manager = kwargs['variable_manager']
    else:
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        variable_manager.extra_vars = load_extra_vars(loader=loader, options=options)
    return (options, loader, inventory, variable_manager)
Exemple #13
0
    def get(self):
        if os.path.isdir(self.cluster_config_path):
            return {"cluster": None, "inventory": None}

        data_loader = DataLoader()
        # data_loader.set_vault_password('627VR8*;YU99B')
        variable_manager = VariableManager(loader=data_loader)

        extra_vars = self.console_args.extra_vars[:]

        configurations = [
            '@' + config for config in self.cluster_config_path.split(',')
        ]

        extra_vars.append('cluster=' +
                          get_cluster_name(self.cluster_config_path))
        extra_vars.extend(configurations)

        options = collections.namedtuple('options', 'extra_vars')
        variable_manager.extra_vars = load_extra_vars(
            loader=data_loader, options=options(extra_vars=extra_vars))

        read_variables = variable_manager.get_vars()

        templar = Templar(data_loader, variables=read_variables)
        templar._filter_loader = self.template.filter_plugin_loader

        return templar.template(read_variables, fail_on_undefined=True)
Exemple #14
0
    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
    def fw_configure(self, fw_vnfr):
        fw_cpinput_ip = '10.30.0.2'
        fw_cpinput_netmask = '255.255.255.252'
        fw_cpinput_network = '10.30.0.2/30'

        # configure vm using ansible playbook
        loader = DataLoader()
        with tempfile.NamedTemporaryFile() as fp:
            fp.write(b'[firewallserver]\n')
            if self.is_running_in_emulator:
                fp.write(b'mn.vnf_fw')
            else:
                fp.write(mgmt_ip.encode('utf-8'))
            fp.flush()
            inventory = InventoryManager(loader=loader, sources=[fp.name])
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        playbook_path = os.path.abspath('./ansible/site.yml')
        LOG.debug('Targeting the ansible playbook: %s', playbook_path)
        if not os.path.exists(playbook_path):
            LOG.error('The playbook does not exist')
            return False
        Options = namedtuple('Options', [
            'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
            'module_path', 'forks', 'remote_user', 'private_key_file',
            'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
            'scp_extra_args', 'become', 'become_method', 'become_user',
            'verbosity', 'check', 'diff'
        ])
        options = Options(listtags=False,
                          listtasks=False,
                          listhosts=False,
                          syntax=False,
                          connection='ssh',
                          module_path=None,
                          forks=100,
                          remote_user='******',
                          private_key_file=None,
                          ssh_common_args=None,
                          ssh_extra_args=None,
                          sftp_extra_args=None,
                          scp_extra_args=None,
                          become=True,
                          become_method=None,
                          become_user='******',
                          verbosity=None,
                          check=False,
                          diff=True)
        options = options._replace(connection='docker', become=False)
        variable_manager.extra_vars = {
            'FW_CPINPUT_NETWORK': fw_cpinput_network,
            'SON_EMULATOR': self.is_running_in_emulator
        }
        pbex = PlaybookExecutor(playbooks=[playbook_path],
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=options,
                                passwords={})
        results = pbex.run()
        return True
Exemple #16
0
    def runner(self, rediskey, hosts, tasks, extra_vars={}):
        passwords = dict(vault_pass='******')
        inventory = InventoryManager(loader=self.loader, sources=self.resource)
        variable_manager = VariableManager(loader=self.loader,
                                           inventory=inventory)
        variable_manager.extra_vars = extra_vars
        play_source = dict(name="Ansible Play",
                           hosts=hosts,
                           gather_facts='no',
                           tasks=tasks)
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=self.loader)
        tqm = None
        try:
            tqm = MyTaskQueueManager(inventory=inventory,
                                     variable_manager=variable_manager,
                                     loader=self.loader,
                                     options=self.options,
                                     passwords=passwords,
                                     stdout_callback=self.callback)
            tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        # 写入redis
        for task in tasks:
            if task['action']['module'] != 'setup':
                self._write_to_save(rediskey, self.get_result())
def playbook_action(playbook, vars):
    resource = '/etc/ansible/hosts'
    variable_manager = VariableManager()
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=resource)
    inventory.clear_pattern_cache()
    Options = namedtuple('Options', [
        'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
        'module_path', 'forks', 'remote_user', 'private_key_file',
        'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
        'scp_extra_args', 'become', 'become_method', 'become_user',
        'verbosity', 'check', 'diff'
    ])
    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='ssh',
                      module_path=None,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=True,
                      become_method='sudo',
                      become_user='******',
                      verbosity=None,
                      check=False,
                      diff=False)
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    variable_manager.extra_vars = vars
    passwords = {}
    callback = ResultsCollector()
    pbex = PlaybookExecutor(playbooks=[playbook],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    pbex._tqm._stdout_callback = callback
    start_time = time.time()
    result = pbex.run()
    end_time = time.time()
    logger.error('ansible执行时间为: %s' % (end_time - start_time))
    results_raw = {'success': {}, 'failed': {}, 'unreachable': {}}
    # for host, result in callback.host_ok.items():
    #     results_raw['success'][host] = result._result

    for host, result in callback.host_failed.items():
        results_raw['failed'][host] = result._result

    for host, result in callback.host_unreachable.items():
        results_raw['unreachable'][host] = result._result['msg']
    # logger.error(results_raw)

    return results_raw
Exemple #18
0
def ansible_runner_24x(playbook_path,
                       module_path,
                       extra_var,
                       inventory_src,
                       console=True):
    loader = DataLoader()
    extra_var["ansible_python_interpreter"] = sys.executable
    variable_manager = VariableManager(loader=loader)
    variable_manager.extra_vars = extra_var
    inventory = Inventory(loader=loader, sources=[inventory_src])
    variable_manager.set_inventory(inventory)
    passwords = {}
    Options = namedtuple('Options', [
        'connection',
        'module_path',
        'forks',
        'become',
        'become_method',
        'become_user',
        'check',
        'diff',
        'listhosts',
        'listtasks',
        'listtags',
        'syntax',
        'remote_user',
        'private_key_file',
        'ssh_common_args',
        'ssh_extra_args',
        'sftp_extra_args',
        'scp_extra_args',
        'verbosity',
    ])
    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='local',
                      module_path=module_path,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=False,
                      become_method='sudo',
                      become_user='******',
                      verbosity=4,
                      diff=False,
                      check=False)
    pbex = PlaybookExecutor(playbooks=[playbook_path],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    return pbex
Exemple #19
0
def ansible_playbook_api(tid, hosts, playbooks, sources, extra_vars={}):
    Options = namedtuple('Options', [
        'remote_user',
        'connection',
        'module_path',
        'forks',
        'become',
        'become_method',
        'become_user',
        'check',
        'diff',
        'listhosts',
        'listtasks',
        'listtags',
        'syntax',
        ])
    options = Options(
        remote_user=ansible_remote_user,
        connection='paramiko',
        module_path=['/to/mymodules'],
        forks=10,
        become=None,
        become_method=None,
        become_user=None,
        check=False,
        diff=False,
        listhosts=None,
        listtasks=None,
        listtags=None,
        syntax=None
    )
    loader = DataLoader()
    passwords = dict(vault_pass='******')
    inventory = InventoryManager(loader=loader, sources=sources)
    # 創建默认的主机组
    inventory.add_group("all")
    for host, port in hosts:
        print(host, port)
        inventory.add_host(host, group="all", port=port)

    variable_manager = VariableManager(loader=loader, inventory=inventory)
    variable_manager.extra_vars = extra_vars
    pb = MyPlaybookExecutor(tid=tid,
                            playbooks=playbooks,
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    # pb = PlaybookExecutor(
    #                         playbooks=playbooks,
    #                         inventory=inventory,
    #                         variable_manager=variable_manager,
    #                         loader=loader,
    #                         options=options,
    #                         passwords=passwords)
    # raise ValueError("1123")
    result = pb.run()
Exemple #20
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
Exemple #21
0
    def run(self, playbook,
            hosts,
            extra_vars={},
            log='',
            with_output=False,
            use_root=False):
        self.result['playbook'] = playbook
        if not os.path.exists(playbook):
            result = {
                'errno': -3,
                'msg': 'not exists playbook: ' + playbook
            }
        else:
            AT = Ansi_Template()
            hosts, host_file = AT.make_host_template(hosts, use_root)
            extra_vars['ansible_hosts'] = ':'.join(hosts)
            inventory = InventoryManager(loader=self.loader, sources=[host_file])
            variable_manager = VariableManager(loader=self.loader, inventory=inventory)
            variable_manager.extra_vars = extra_vars
            pbex = PlaybookExecutor(playbooks=[playbook],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=self.loader,
                                    options=self.options,
                                    passwords={})

            new_display = New_Display(log_file=log, debug=self.debug)
            results_callback = ResultCallback(new_display=new_display)

            pbex._tqm._stdout_callback = results_callback
            try:
                errno = pbex.run()
                result = results_callback.tasks
                result['errno'] = errno
            except AnsibleParserError as e:
                msg = 'syntax problems: {0}'.format(str(e))
                result = {
                    'errno': -2,
                    'msg': msg
                }
                self.write_log(log, msg)
                print('syntax problems: {0}'.format(str(e)))
            if with_output:
                result['output'] = '\n'.join(new_display.log_add)
            if result['errno'] != -2 and not result['summary']:
                msg = 'no host executed'
                result = {
                    'errno': -1,
                    'msg': msg
                }
                self.write_log(log, msg)
            os.unlink(host_file)
        self.result.update(result)
        if self.result['errno'] != 0:
            self.result['msg'] = '无法配置完成,请联系管理员!'
        return self.result
Exemple #22
0
def AnsibleApi(tid, hosts, tasks, sources, extra_vars={}):
    # 构建一个解析器,将相关参数传入
    Options = namedtuple('Options', [
        'remote_user', 'connection', 'module_path', 'forks', 'become',
        'become_method', 'become_user', 'check', 'diff'
    ])
    options = Options(remote_user=ansible_remote_user,
                      connection='paramiko',
                      module_path=['/to/mymodules'],
                      forks=10,
                      become=None,
                      become_method=None,
                      become_user=None,
                      check=False,
                      diff=False)

    # 初始化所需对象
    loader = DataLoader()  # 负责查找和读取yAML、json和ini文件
    passwords = dict(vault_pass='******')

    # 创建 inventory,定义需要执行的主机组
    inventory = InventoryManager(loader=loader, sources=sources)

    # 合并所有不同来源的参数

    variable_manager = VariableManager(loader=loader, inventory=inventory)
    variable_manager.extra_vars = extra_vars
    # 创建数据结构来加载任务
    play_source = dict(name="Ansible Play",
                       hosts=hosts,
                       gather_facts='no',
                       tasks=tasks)

    # 创建play对象,使用 .load 初始化
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # 实例化任务队列管理器,它负责设置所有对象,以便在主机列表和任务上迭代
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=ResultCallback(tid),  # 使用自定义回调函数
        )
        result = tqm.run(play)  # 执行任务
    finally:
        # 清理子进程等相关数据
        if tqm is not None:
            tqm.cleanup()
        # 删除临时目录
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemple #23
0
def _load_defaults(inventory_path, extra_vars=None, tags=None, basedir=False):
    """Load common defaults data structures.

    For factorization purpose."""

    extra_vars = extra_vars or {}
    tags = tags or []
    loader = DataLoader()
    if basedir:
        loader.set_basedir(basedir)

    inventory = Inventory(loader=loader, sources=inventory_path)

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

    # seems mandatory to load group_vars variable
    if basedir:
        variable_manager.safe_basedir = True

    if extra_vars:
        variable_manager.extra_vars = extra_vars

    # NOTE(msimonin): The ansible api is "low level" in the
    # sense that we are redefining here all the default values
    # that are usually enforce by ansible called from the cli
    Options = namedtuple("Options", [
        "listtags", "listtasks", "listhosts", "syntax", "connection",
        "module_path", "forks", "private_key_file", "ssh_common_args",
        "ssh_extra_args", "sftp_extra_args", "scp_extra_args", "become",
        "become_method", "become_user", "remote_user", "verbosity", "check",
        "tags", "diff", "basedir"
    ])

    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection="ssh",
                      module_path=None,
                      forks=100,
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=None,
                      become_method="sudo",
                      become_user="******",
                      remote_user=None,
                      verbosity=2,
                      check=False,
                      tags=tags,
                      diff=None,
                      basedir=basedir)

    return inventory, variable_manager, loader, options
Exemple #24
0
def execute_ansible(inventory_path, playbook_path, **kwargs):
    """Execute ansible playbook.
    
    Arguments:
        inventory_path {string} -- The path of the inventory file.
        playbook_path {string} -- The path of the playbook to be executed.
        kwagrs -- Extra varibles for the playbook.

    Return:

    """

    print "Env varible 'ANSIBLE_CONFIG' is '%s' for playbook '%s'" % (
        environ.get('ANSIBLE_CONFIG', 'Not Set'), 
        playbook_path
    )
    print "Running playbook: %s" % playbook_path
    # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' 
    # the args parsing options object
    Options = namedtuple(
        'Options', [
            'connection', 'listhosts', 'listtasks', 'listtags', 
            'syntax', 'module_path', 'forks', 'become', 'become_method', 
            'become_user', 'check', 'diff', 'remote_user', 'verbosity'
        ]
    )
    options = Options(
        connection='ssh', listhosts=False, listtasks=False, listtags=False, 
        syntax=False, module_path=None, forks=10, become=True, become_method='sudo', 
        become_user='******', check=False, diff=True, remote_user='******', verbosity=None
    )
    # initialize needed objects
    loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
    passwords = dict()

    # create inventory, use path to host config file as source or hosts in a comma separated string
    inventory = InventoryManager(loader=loader, sources=inventory_path)

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

    pbex = PlaybookExecutor(
        playbooks=[playbook_path], 
        inventory=inventory, 
        variable_manager=variable_manager, 
        loader=loader, 
        options=options, 
        passwords=passwords
    )
    result = pbex.run() # most interesting data for a play is actually sent to the callback's methods
    # Remove ansible tmpdir
    shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

    return result
Exemple #25
0
    def fetch_files(self):
        """
        Fetch files from all hosts via ansible playbook

        reference:
          https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
        """
        loader = DataLoader()
        inventory = InventoryManager(loader=loader, sources=[INVENTORY_PATH])

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

        variable_manager.set_inventory(inventory)

        variable_manager.extra_vars = {
            "data_id": self.new_id,
            "data_path": self.data_path,
            "remote_path": REMOTE_DATA_PATH
        }

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

        options = Options(connection="ssh",
                          forks=100,
                          become=True,
                          become_method="sudo",
                          become_user="******",
                          check=False,
                          listhosts=False,
                          listtasks=False,
                          listtags=False,
                          syntax=False,
                          module_path="",
                          diff=False)

        passwords = dict(vault_pass="******")

        playbook = PlaybookExecutor(playbooks=[PLAYBOOK_PATH],
                                    inventory=inventory,
                                    loader=loader,
                                    variable_manager=variable_manager,
                                    options=options,
                                    passwords=passwords)
        if not self.ansible_stdout:
            results_callback = ResultCallback()
            playbook._tqm._stdout_callback = results_callback
        _.info("fetch_files started...")
        playbook.run()
    def playbook_execution(self, playbook, host_ip):
        LOG.info("Executing playbook: %s", playbook)

        loader = DataLoader()

        inventory = None
        with tempfile.NamedTemporaryFile() as fp:
            fp.write(host_ip.encode('utf-8'))
            fp.flush()
            inventory = InventoryManager(loader=loader, sources=[fp.name])

        variable_manager = VariableManager(loader=loadder, inventory=inventory)

        if not os.path.exists(playbook):
            LOG.error('The playbook %s does not exist', playbook)
            return

        Options = namedtuple('Options', [
            'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
            'module_path', 'forks', 'remote_user', 'private_key_file',
            'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
            'scp_extra_args', 'become', 'become_method', 'become_user',
            'verbosity', 'check'
        ])
        options = Options(listtags=False,
                          listtasks=False,
                          listhosts=False,
                          syntax=False,
                          connection='ssh',
                          module_path=None,
                          forks=100,
                          remote_user='******',
                          private_key_file=None,
                          ssh_common_args=None,
                          ssh_extra_args=None,
                          sftp_extra_args=None,
                          scp_extra_args=None,
                          become=True,
                          become_method=None,
                          become_user='******',
                          verbosity=None,
                          check=False)

        variable_manager.extra_vars = {'hosts': host_ip}

        passwords = {}

        pbex = PlaybookExecutor(playbooks=[playbook],
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=options,
                                passwords=passwords)
        results = pbex.run()
        return
Exemple #27
0
    def run_playbook(self, host_list, playbooks):
        inventory = InventoryManager(loader=self.loader, sources=host_list)
        variable_manager = VariableManager(loader=self.loader, inventory=inventory)
 
        variable_manager.extra_vars = {'customer': 'test', 'disabled': 'yes'}
        play = PlaybookExecutor(playbooks=playbooks,
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=self.loader, options=self.ops, passwords=self.passwords)
        self.results_callback = ResultCallback()
        result = play.run()
        return result
Exemple #28
0
def exec_playbook():
    # Leveraging the Python 2.0 API for Executing ansible-playbook
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources="")
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    playbook_path = 'brewplay.yml'

    if not os.path.exists(playbook_path):
        print '[INFO] The playbook does not exist'
        sys.exit()

    Options = namedtuple('Options', [
        'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
        'module_path', 'forks', 'remote_user', 'private_key_file',
        'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
        'scp_extra_args', 'become', 'become_method', 'become_user',
        'verbosity', 'host_key_checking', 'check', 'diff'
    ])
    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='local',
                      module_path=None,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=False,
                      become_method='sudo',
                      become_user='******',
                      verbosity=0,
                      host_key_checking=False,
                      check=False,
                      diff=False)

    variable_manager.extra_vars = {
        'hosts': 'mywebserver'
    }  # This can accomodate various other command line arguments.`

    passwords = {}

    pbex = PlaybookExecutor(playbooks=[playbook_path],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)

    results = pbex.run()
Exemple #29
0
def execute_playbook(playbook_path):
    loader = DataLoader()

    inventory = InventoryManager(loader=loader, sources=["localhost"])
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    if not os.path.exists(playbook_path):
        logging.debug('[INFO] The playbook does not exist')
        sys.exit()

    Options = namedtuple('Options', [
        'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
        'module_path', 'forks', 'remote_user', 'private_key_file',
        'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
        'scp_extra_args', 'become', 'become_method', 'become_user',
        'verbosity', 'check', 'diff'
    ])

    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='local',
                      module_path=None,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=False,
                      become_method=None,
                      become_user='******',
                      verbosity=None,
                      check=False,
                      diff=False)

    variable_manager.extra_vars = {
        'hosts': 'localhost'
    }  # This can accomodate various other command line arguments.`

    passwords = {}

    pbex = PlaybookExecutor(playbooks=[playbook_path],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    results = pbex.run()
    return results
 def _create_pbex_args(playbooks, extra_vars):
     loader = DataLoader()
     inventory = InventoryManager(loader=loader)
     variable_manager = VariableManager(loader=loader, inventory=inventory)
     variable_manager.extra_vars = extra_vars
     return {
         'playbooks': playbooks,
         'inventory': inventory,
         'variable_manager': variable_manager,
         'loader': loader,
         'options': _PBEX_OPTIONS,
         'passwords': {},
     }
 def _create_pbex_args(playbooks, extra_vars):
     loader = DataLoader()
     inventory = InventoryManager(loader=loader)
     variable_manager = VariableManager(loader=loader, inventory=inventory)
     variable_manager.extra_vars = extra_vars
     return {
         'playbooks': playbooks,
         'inventory': inventory,
         'variable_manager': variable_manager,
         'loader': loader,
         'options': _PBEX_OPTIONS,
         'passwords': {},
     }
Exemple #32
0
    def execute_playbook(self, playbook_name, playbook_input):
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options', [
                'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
                'module_path', 'forks', 'remote_user', 'private_key_file',
                'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user',
                'verbosity', 'check', 'diff'
            ])
            options = Options(listtags=False,
                              listtasks=False,
                              listhosts=False,
                              syntax=False,
                              connection='ssh',
                              module_path=None,
                              forks=100,
                              remote_user=None,
                              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=None,
                              verbosity=None,
                              check=False,
                              diff=False)

            variable_manager.extra_vars = {"playbook_input": playbook_input}

            pbex = PlaybookExecutor(playbooks=[playbook_name],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options,
                                    passwords=None)
            ret_val = pbex.run()

            if ret_val != 0:
                msg = "playbook returned with error"
                self._logger.error(msg)
                raise Exception(msg)

        except Exception as exp:
            pass
Exemple #33
0
    def execute_playbook(self, playbook_info):
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options', [
                'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
                'module_path', 'forks', 'remote_user', 'private_key_file',
                'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user',
                'verbosity', 'check', 'diff'
            ])
            options = Options(listtags=False,
                              listtasks=False,
                              listhosts=False,
                              syntax=False,
                              connection='ssh',
                              module_path=None,
                              forks=100,
                              remote_user='******',
                              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=None,
                              verbosity=None,
                              check=False,
                              diff=False)

            variable_manager.extra_vars = playbook_info['extra_vars']

            pbex = PlaybookExecutor(playbooks=[playbook_info['uri']],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options,
                                    passwords=None)
            ret_val = pbex.run()
            output = self.get_plugin_output(pbex)

            if ret_val != 0:
                raise Exception("Playbook returned with error")

            return output
        except Exception as e:
            sys.exit("Exception in playbook process %s " % repr(e))
    def test_variable_manager_extra_vars(self):
        fake_loader = DictDataLoader({})

        extra_vars = dict(a=1, b=2, c=3)
        mock_inventory = MagicMock()
        v = VariableManager(loader=fake_loader, inventory=mock_inventory)
        v.extra_vars = extra_vars

        vars = v.get_vars(use_cache=False)

        for (key, val) in iteritems(extra_vars):
            self.assertEqual(vars.get(key), val)

        self.assertIsNot(v.extra_vars, extra_vars)
def ansible_runner_24x(playbook_path,
                       extra_vars,
                       options=None,
                       inventory_src='localhost',
                       console=True):

    loader = DataLoader()
    variable_manager = VariableManager(loader=loader)
    variable_manager.extra_vars = extra_vars
    inventory = Inventory(loader=loader, sources=[inventory_src])
    variable_manager.set_inventory(inventory)
    passwords = {}

    pbex = PlaybookExecutor([playbook_path],
                            inventory,
                            variable_manager,
                            loader,
                            options,
                            passwords)
    return pbex
def ansible_runner_2x(playbook_path,
                      extra_vars,
                      options=None,
                      inventory_src='localhost',
                      console=True):

    variable_manager = VariableManager()
    loader = DataLoader()
    variable_manager.extra_vars = extra_vars
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=inventory_src)
    passwords = {}
    pbex = PlaybookExecutor([playbook_path],
                            inventory,
                            variable_manager,
                            loader,
                            options,
                            passwords)

    return pbex
    def execute_playbook(self, playbook_name, playbook_input):
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options',
                                 ['listtags', 'listtasks', 'listhosts',
                                  'syntax', 'connection', 'module_path',
                                  'forks', 'remote_user', 'private_key_file',
                                  'ssh_common_args', 'ssh_extra_args',
                                  'sftp_extra_args', 'scp_extra_args',
                                  'become', 'become_method', 'become_user',
                                  'verbosity', 'check', 'diff'])
            options = Options(listtags=False, listtasks=False, listhosts=False,
                              syntax=False, connection='ssh', module_path=None,
                              forks=100, remote_user=None,
                              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=None,
                              verbosity=None, check=False, diff=False)

            variable_manager.extra_vars = {"playbook_input": playbook_input}

            pbex = PlaybookExecutor(playbooks=[playbook_name],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options, passwords=None)
            ret_val = pbex.run()

            if ret_val != 0:
                msg = "playbook returned with error"
                self._logger.error(msg)
                raise Exception(msg)

        except Exception as exp:
            pass
variable_manager = VariableManager()
loader = DataLoader()

#inventory = InventoryManager(loader=loader, variable_manager=variable_manager,  sources='./sample_hosts')
inventory = InventoryManager(loader=loader, sources='./sample_hosts')
playbook_path = 'sample.yml'

if not os.path.exists(playbook_path):
    print '[INFO] The playbook does not exist'
    sys.exit()

Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False)

variable_manager.extra_vars = {'hosts': 'mywebserver'} # This can accomodate various other command line arguments.`

passwords = {}

pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
print pbex._playbooks

taskData = []
conditionalData = []

print "\nParsed tasks....."
# Extract data from compiled playbook
for playbook_path in pbex._playbooks:
  pb = Playbook.load(playbook_path, variable_manager=pbex._variable_manager, loader=pbex._loader)
  plays = pb.get_plays()
  for play in plays:
Exemple #39
0
#!/usr/bin/env python
# coding=utf-8

from collections import namedtuple
from ansible.executor import playbook_executor
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager

loader = DataLoader()
inventory = InventoryManager(loader=loader, sources='localhost,')

extra_vars = {'name': 'create'}
variable_manager = VariableManager(loader=loader, inventory=inventory)
variable_manager.extra_vars = extra_vars

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

options = Options(connection='local', module_path=['/to/mymodules'],
                  forks=10, become=None, become_method=None,
                  become_user=None, check=False, diff=False, listhosts=False,
                  listtasks=False, listtags=False, syntax=False)

passwords = dict(vault_pass='******')

pb_executor = playbook_executor.PlaybookExecutor(
    playbooks=['test.yaml'],
    inventory=inventory,
    def execute_playbook(self, playbook_info):
        output = None
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options',
                                 ['listtags', 'listtasks', 'listhosts',
                                  'syntax', 'connection', 'module_path',
                                  'forks', 'remote_user', 'private_key_file',
                                  'ssh_common_args', 'ssh_extra_args',
                                  'sftp_extra_args', 'scp_extra_args',
                                  'become', 'become_method', 'become_user',
                                  'verbosity', 'check', 'diff'])
            options = Options(listtags=False, listtasks=False, listhosts=False,
                              syntax=False, connection='ssh', module_path=None,
                              forks=100, remote_user=None,
                              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=None,
                              verbosity=None, check=False, diff=False)

            variable_manager.extra_vars = playbook_info['extra_vars']

            pbex = PlaybookExecutor(playbooks=[playbook_info['uri']],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options, passwords=None)
            ret_val = pbex.run()

            output = self.get_plugin_output(pbex)

            if ret_val != 0:
                msg = MsgBundle.getMessage(MsgBundle.
                                           PLAYBOOK_RETURN_WITH_ERROR)
                raise Exception(msg)

            if output is None or output.get('status') is None:
                msg = MsgBundle.getMessage(MsgBundle.
                                           PLAYBOOK_OUTPUT_MISSING)
                raise Exception(msg)

            if output.get('status').lower() == "failure":
                msg = MsgBundle.getMessage(MsgBundle.
                                           PLAYBOOK_STATUS_FAILED)
                raise Exception(msg)

            return output
        except Exception as exp:
            msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR,
                                       playbook_uri=playbook_info['uri'],
                                       execution_id=playbook_info['extra_vars']
                                       ['playbook_input']['job_execution_id'],
                                       exc_msg=repr(exp))
            if exp.message:
                msg = msg + "\n" + exp.message

            JM_LOGGER.error(msg)

            # after handling exception, write an END
            # to stop listening to the file if created
            unique_pb_id = playbook_info['extra_vars'][
                'playbook_input']['unique_pb_id']
            exec_id = playbook_info['extra_vars']['playbook_input'][
                'job_execution_id']
            self._job_file_write.write_to_file(
                exec_id,
                unique_pb_id,
                JobFileWrite.PLAYBOOK_OUTPUT,
                json.dumps(output)
            )
            with open("/tmp/"+exec_id, "a") as f:
                f.write(unique_pb_id + 'END' + PLAYBOOK_EOL_PATTERN)
            sys.exit(msg)