Example #1
0
 def shell_run(self, cmd_str):
     print "ansible _cmd  ", cmd_str, type(cmd_str)
     # cmd_str = cmd_str[0]
     play_source = dict(
         name="Ansible Shell",
         hosts=self.host,
         gather_facts='no',
         tasks=[dict(action=dict(module='shell', args=cmd_str))])
     play = Play().load(play_source,
                        variable_manager=self.variable_manager,
                        loader=self.loader)
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=self.passwords,
             stdout_callback=self.results_callback,
         )
         tqm.run(play)
     except Exception, e:
         print "shell_run Error: "
Example #2
0
    def runadHoc(self, host_list, task_list):
        play_source = dict(
            # hosts为执行的主机或者群组
            name="Ansible Play ad-hoc",
            hosts=host_list,
            gather_facts='no',
            tasks=task_list)
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback=self.callback,
                run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                run_tree=False,
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        result_raw = {'success': {}, 'failed': {}, 'unreachable': {}}
        for host, result in self.callback.host_ok.items():
            result_raw['success'][host] = result._result
        for host, result in self.callback.host_failed.items():
            result_raw['failed'][host] = result._result

        print(json.dumps(result_raw, indent=4))
        return json.dumps(result_raw, indent=4)
Example #3
0
def run_adhoc(ip,order):
    variable_manager.extra_vars={"ansible_ssh_user":"******" , "ansible_ssh_pass":"******"}
    play_source = {"name":"Ansible Ad-Hoc","hosts":"%s"%ip,"gather_facts":"no","tasks":[{"action":{"module":"command","args":"%s"%order}}]}
#    play_source = {"name":"Ansible Ad-Hoc","hosts":"192.168.2.160","gather_facts":"no","tasks":[{"action":{"module":"command","args":"python ~/store.py del"}}]}   
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
    tqm = None
    callback = ResultsCollector()

    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=None,
            run_tree=False,
        )
        tqm._stdout_callback = callback
        result = tqm.run(play)
        return callback

    finally:
        if tqm is not None:
            tqm.cleanup()
Example #4
0
    def ansible(self, module, args=""):
        play_source = dict(name="Ansible Play",
                           hosts='all',
                           gather_facts='no',
                           tasks=[
                               dict(action=dict(module=module, args=args),
                                    register='shell_out')
                           ])

        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        tqm = None
        try:
            tqm = TaskQueueManager(inventory=self.inventory,
                                   variable_manager=self.variable_manager,
                                   loader=self.loader,
                                   options=self.options,
                                   passwords=self.passwords,
                                   stdout_callback=self.callback)
            tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()

        results_raw = {'success': {}, 'failed': {}, 'unreachable': {}}
        for host, result in self.callback.host_ok.items():
            results_raw['success'][host] = result._result
        for host, result in self.callback.host_failed.items():
            results_raw['failed'][host] = result._result['msg']
        for host, result in self.callback.host_unreachable.items():
            results_raw['unreachable'][host] = result._result['msg']

        print json.dumps(results_raw, indent=4)
        return results_raw
Example #5
0
    def ansiblePlay(self, action):
        # create play with tasks
        args = "ls /"
        play_source = dict(
            name="Ansible Play",
            hosts='all',
            gather_facts='no',
            tasks=[
                dict(action=dict(module='shell', args=args),
                     register='shell_out'),
                dict(action=dict(module='debug',
                                 args=dict(msg='{{shell_out.stdout}}')))
            ])
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        # run it
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback='default',
            )
            result = tqm.run(play)
        finally:
            # print result
            if tqm is not None:
                tqm.cleanup()
                os.remove(self.hostsFile.name)
                self.inventory.clear_pattern_cache()
            return result
Example #6
0
 def exec_shell(self, command):
     source = {'hosts': 'all', 'gather_facts': 'no', 'tasks': [
         {'action': {'module': 'shell', 'args': command}, 'register': 'shell_out'}]}
     play = Play().load(source, variable_manager=self.variable_manager, loader=self.loader)
     results_callback = AdHocResultCallback()
     # results_callback = CallbackModule()
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords={},
             stdout_callback=results_callback
         )
         tqm.run(play)
         # return results_callback.results
         return results_callback.results_raw
     except:
         raise
     finally:
         if tqm is not None:
             tqm.cleanup()
Example #7
0
 def run_model(self, host_list, module_name, module_args):
     """ 
     run module from andible ad-hoc. 
     module_name: ansible module_name 
     module_args: ansible module args 
     """
     play_source = dict(
         name="Ansible Play",
         hosts=host_list,
         gather_facts='no',
         tasks=[dict(action=dict(module=module_name, args=module_args))])
     play = Play().load(play_source,
                        variable_manager=self.variable_manager,
                        loader=self.loader)
     tqm = None
     if self.redisKey:
         self.callback = ModelResultsCollectorToSave(
             self.redisKey, self.logId)
     else:
         self.callback = ModelResultsCollector()
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=self.passwords,
         )
         tqm._stdout_callback = self.callback
         tqm.run(play)
     except Exception as err:
         DsRedis.OpsAnsibleModel.lpush(self.redisKey, data=err)
         if self.logId: AnsibleSaveResult.Model.insert(self.logId, err)
     finally:
         if tqm is not None:
             tqm.cleanup()
Example #8
0
 def play(self, hosts='localhost', module='setup'):
     play_source = dict(name="Ansible Play",
                        hosts='localhost',
                        gather_facts='no',
                        tasks=[dict(action=dict(module='setup'))])
     results_callback = ResultCallback()
     play = Play().load(play_source,
                        variable_manager=self.variable_manager,
                        loader=self.loader)
     qm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=self.passwords,
             stdout_callback=results_callback,
         )
         result = tqm.run(play)
         return results_callback
     finally:
         if tqm is not None:
             tqm.cleanup()
 def test_b_create_stream_2(self):
     play_source =  dict(
         name = "Create Kinesis Stream with specifying the retention period less than 24 hours",
         hosts = 'localhost',
         gather_facts = 'no',
         tasks = [
             dict(
                 action=dict(
                     module='kinesis_stream',
                     args=dict(
                         name='stream-test',
                         region=aws_region,
                         retention_period=23,
                         shards=10,
                         wait='yes'
                     )
                 ),
                 register='stream'
             )
         ]
     )
     play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
     tqm, results = run(play)
     self.failUnless(tqm._stats.failures['localhost'] == 1)
Example #10
0
        self.hosk_ok[result._host.get_name()] = result

    def v2_runner_on_failed(self, result, *args, **kwargs):
        self.host_failed[result._host.get_name()] = result


callback = ModelResultsCollector()
play_source = dict(name="ansible  脚本  命令 test",
                   hosts='k8snode',
                   gather_facts='no',
                   tasks=[
                       dict(action=dict(module='shell', args='ls /root'),
                            register='shell_out')
                   ])
play = Play().load(play_source,
                   variable_manager=variable_manager,
                   loader=loader)
#定义
passwords = dict()
tqm = TaskQueueManager(inventory=Inventory,
                       variable_manager=variable_manager,
                       loader=loader,
                       options=options,
                       passwords=passwords,
                       stdout_callback=callback)
result = tqm.run(play)
#print  callback.hosk_ok.items()
result_raw = {'success': {}, 'failed': {}, 'unreachable': {}}
for host, result in callback.hosk_ok.items():
    result_raw['success'][host] = result._result
"""for  host,result  in  callback.hosk_unreachable.items():
Example #11
0
    def play(self, target_ip, tasks):
        Options = namedtuple('Options', [
            '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'
        ])
        # initialize needed objects
        variable_manager = VariableManager()
        # TODO load vars
        loader = DataLoader()
        options = Options(connection='ssh',
                          module_path='/etc/ansible/modules',
                          forks=100,
                          remote_user="******",
                          private_key_file="",
                          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)
        passwords = dict(vault_pass='******')

        # create inventory and pass to var manager
        inventory = Inventory(loader=loader, variable_manager=variable_manager, \
                              host_list=[ip for ip in target_ip])
        variable_manager.set_inventory(inventory)

        # create play with tasks
        task_list = []
        for task in tasks:
            # task = "sysctl: name=net.ipv4.ip_forward value=1 state=present
            module, tasks_str = task.split(':', 1)
            # parse args
            kv_args = parse_kv(tasks_str)
            # create datastructure
            task_list.append(
                dict(action=dict(module=module, args=kv_args),
                     register='shell_out'), )
        print(task_list)

        play_source = dict(name="Ansible Play {}".format(target_ip),
                           hosts=target_ip,
                           gather_facts='no',
                           tasks=task_list)
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)

        # actually run it
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                # TODO callback must be an instance of CallbackBase or the name of a callback plugin
                stdout_callback='default',
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()

        return 0, ""
Example #12
0
    def ansibleRun(self, **kwargs ):
        _module = ''
        _host   = ''
        _qm     = ''
        _args   = ''
        _ansible_hosts_file = ''
        _ansible_password   = ''
        _json   = ''
        
        if 'module' in kwargs:
            _module = kwargs.get('module')
        if 'host' in kwargs:
            _host = kwargs.get('host')
        if 'qm' in kwargs:
            _qm = kwargs.get('qm')
        if 'args' in kwargs:
            _args = kwargs.get('args')
        if 'ansible_hosts_file' in kwargs:
            _ansible_hosts_file = kwargs.get('ansible_hosts_file')
        if 'ansible_password' in kwargs:
            _ansible_password = kwargs.get('ansible_password')
        if 'json' in kwargs:
            _json = kwargs.get('json')

        # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
        #Either use Become method or do not, depending on ansible_password
        if 'ansible_password' in kwargs:
            Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'private_key_file', 'become', 'become_method', 'become_user', 'check', 'diff'])
            options = Options(connection='ssh', module_path=['/to/mymodules'], forks=10, private_key_file=None, become=True, become_method='sudo', become_user='******', check=False, diff=False)
            loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
            passwords = dict(vault_pass='******')
            results_callback = ResultCallback()
            inventory = InventoryManager(loader=loader, sources=_ansible_hosts_file)
            variable_manager = VariableManager(loader=loader, inventory=inventory)
            variable_manager.extra_vars = {'ansible_become_password' : _ansible_password}

        else:
            Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'private_key_file', 'become', 'become_method', 'become_user', 'check', 'diff'])
            options = Options(connection='ssh', module_path=['/to/mymodules'], forks=10, private_key_file=None, become=False, become_method=None, become_user=None, check=False, diff=False)
            loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
            passwords = dict(vault_pass='******')
            results_callback = ResultCallback()
            inventory = InventoryManager(loader=loader, sources=_ansible_hosts_file)
            variable_manager = VariableManager(loader=loader, inventory=inventory)
        

        play_source =  dict(
            name = "Ansible Play",
            hosts = _host,
            gather_facts = 'no',
            tasks = [
                dict(action=dict(module=_module + _qm , args= _args) ),
                ]
            )

        play = Play().load(play_source, variable_manager=variable_manager, loader=loader)


        tqm = None
        try:
            if _json == True:
                tqm = TaskQueueManager(
                    inventory=inventory,
                    variable_manager=variable_manager,
                    loader=loader,
                    options=options,
                    passwords=passwords,
                    stdout_callback=results_callback,  # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
                )
            else: 
                tqm = TaskQueueManager(
                    inventory=inventory,
                    variable_manager=variable_manager,
                    loader=loader,
                    options=options,
                    passwords=passwords,
                    #stdout_callback=results_callback,  # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
                )
            
            result = 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()

            # Remove ansible tmpdir
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
        return results_callback
Example #13
0
def ansible_run(play_source, host_list, extra_vars={}):
    logger.info(json.dumps([play_source, host_list, extra_vars], indent=4))
    # ansible-playbookで指定できる引数と同じ
    context.CLIARGS = ImmutableDict(tags={},
                                    listtags=False,
                                    listtasks=False,
                                    listhosts=False,
                                    syntax=False,
                                    extra_vars=extra_vars,
                                    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=True,
                                    become_method='sudo',
                                    become_user='******',
                                    verbosity=True,
                                    check=False,
                                    start_at_task=None)

    # 鍵認証が優先され、パスワードを聞かれた場合のみ利用する。書きたくない場合は適当でOK
    passwords = dict(vault_pass='******')

    # コールバックのインスタンス化
    results_callback = ResultCallback()

    # インベントリを1ライナー用フォーマットに変換
    sources = ','.join(host_list)
    if len(host_list) == 1:
        sources += ','
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=sources)

    # 値をセット
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # 実行
    tqm = None
    try:
        tqm = TaskQueueManager(
            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()
        # Remove ansible tmpdir
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
        return results_callback.res
Example #14
0
    def run(self):
        ''' create and execute the single task playbook '''

        super(AdHocCLI, self).run()

        # only thing left should be host pattern
        pattern = to_text(self.args[0], errors='surrogate_or_strict')

        sshpass = None
        becomepass = None

        self.normalize_become_options()
        (sshpass, becomepass) = self.ask_passwords()
        passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        # dynamically load any plugins
        get_all_plugin_loaders()

        loader, inventory, variable_manager = self._play_prereqs(self.options)

        try:
            hosts = CLI.get_host_list(inventory, self.options.subset, pattern)
        except AnsibleError:
            if self.options.subset:
                raise
            else:
                hosts = []
                display.warning("No hosts matched, nothing to do")

        if self.options.listhosts:
            display.display('  hosts (%d):' % len(hosts))
            for host in hosts:
                display.display('    %s' % host)
            return 0

        if self.options.module_name in C.MODULE_REQUIRE_ARGS and not self.options.module_args:
            err = "No argument passed to %s module" % self.options.module_name
            if pattern.endswith(".yml"):
                err = err + ' (did you mean to run ansible-playbook?)'
            raise AnsibleOptionsError(err)

        # Avoid modules that don't work with ad-hoc
        if self.options.module_name.startswith(('include', 'import_')):
            raise AnsibleOptionsError(
                "'%s' is not a valid action for ad-hoc commands" %
                self.options.module_name)

        play_ds = self._play_ds(pattern, self.options.seconds,
                                self.options.poll_interval)
        play = Play().load(play_ds,
                           variable_manager=variable_manager,
                           loader=loader)

        if self.callback:
            cb = self.callback
        elif self.options.one_line:
            cb = 'oneline'
        # Respect custom 'stdout_callback' only with enabled 'bin_ansible_callbacks'
        elif C.DEFAULT_LOAD_CALLBACK_PLUGINS and C.DEFAULT_STDOUT_CALLBACK != 'default':
            cb = C.DEFAULT_STDOUT_CALLBACK
        else:
            cb = 'minimal'

        run_tree = False
        if self.options.tree:
            C.DEFAULT_CALLBACK_WHITELIST.append('tree')
            C.TREE_DIR = self.options.tree
            run_tree = True

        # now create a task queue manager to execute the play
        self._tqm = None
        try:
            self._tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=self.options,
                passwords=passwords,
                stdout_callback=cb,
                run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                run_tree=run_tree,
            )

            result = self._tqm.run(play)
        finally:
            if self._tqm:
                self._tqm.cleanup()
            if loader:
                loader.cleanup_all_tmp_files()

        return result
Example #15
0
    def execute(self, hosts, environment, module="shell", command="ls"):
        Options = namedtuple('Options', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'check'
        ])
        # initialize needed objects
        variable_manager = VariableManager()
        loader = DataLoader()
        options = Options(connection='ssh',
                          module_path=None,
                          forks=10,
                          become=None,
                          become_method=None,
                          become_user=None,
                          check=False)
        passwords = {}
        if self.vault_password_file:
            passwords = dict(
                vault_pass=open(os.path.expanduser(self.vault_password_file),
                                'r').read().strip())
            loader.set_vault_password(passwords['vault_pass'])

        # Instantiate our ResultCallback for handling results as they come in
        results_callback = ResultCallback()

        # create inventory and pass to var manager
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=environment)
        variable_manager.set_inventory(inventory)

        # create play with tasks
        play_source = dict(
            name=self.play_name,
            hosts=hosts,
            gather_facts='no',
            tasks=[
                dict(action=dict(module=module, args=command),
                     register='shell_out'),
                dict(action=dict(module='debug',
                                 args=dict(msg='{{shell_out.stdout}}')))
            ])
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)

        # actually run it
        tqm = None
        try:
            logger.info(
                "Executing: command [%s] in play %s on hosts %s in environment %s",
                command, self.play_name, hosts, environment)
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback=
                results_callback,  # Use our custom callback instead of the ``default`` callback plugin
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
Example #16
0
def run_ansible_command(command):
    # 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', 'module_path', 'forks',
            'become', 'become_method', 'become_user', 'check', 'diff'
        ])

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

    # initialize needed objects
    loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
    passwords = dict(vault_pass='******')

    # Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
    results_callback = ResultCallback()

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

    # 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)

    # create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
    play_source =  dict(
        name = "Ansible Play",
        hosts = 'localhost',
        gather_facts = 'no',
        tasks = [
            dict(action=dict(module='shell', args='ls'), register='shell_out'),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='pause', args=dict(minutes=1))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
            dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}'))),
         ]
    )

    # Create play object, playbook objects use .load instead of init or new methods,
    # this will also automatically create the task objects from the info provided in play_source
    play = Play().load(play_source, 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,
                  options=options,
                  passwords=passwords,
                  stdout_callback=results_callback,  # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
              )
        result = 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()

        # Remove ansible tmpdir
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Example #17
0
    def default(self, arg, forceshell=False):
        """ actually runs modules """
        if arg.startswith("#"):
            return False

        if not self.cwd:
            display.error("No host found")
            return False

        # defaults
        module = 'shell'
        module_args = arg

        if forceshell is not True:
            possible_module, *possible_args = arg.split()
            if module_loader.find_plugin(possible_module):
                # we found module!
                module = possible_module
                if possible_args:
                    module_args = ' '.join(possible_args)
                else:
                    module_args = ''

        if self.callback:
            cb = self.callback
        elif C.DEFAULT_LOAD_CALLBACK_PLUGINS and C.DEFAULT_STDOUT_CALLBACK != 'default':
            cb = C.DEFAULT_STDOUT_CALLBACK
        else:
            cb = 'minimal'

        result = None
        try:
            check_raw = module in C._ACTION_ALLOWS_RAW_ARGS
            task = dict(action=dict(module=module, args=parse_kv(module_args, check_raw=check_raw)), timeout=self.task_timeout)
            play_ds = dict(
                name="Ansible Shell",
                hosts=self.cwd,
                gather_facts='no',
                tasks=[task],
                remote_user=self.remote_user,
                become=self.become,
                become_user=self.become_user,
                become_method=self.become_method,
                check_mode=self.check_mode,
                diff=self.diff,
                collections=self.collections,
            )
            play = Play().load(play_ds, variable_manager=self.variable_manager, loader=self.loader)
        except Exception as e:
            display.error(u"Unable to build command: %s" % to_text(e))
            return False

        try:
            # now create a task queue manager to execute the play
            self._tqm = None
            try:
                self._tqm = TaskQueueManager(
                    inventory=self.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    passwords=self.passwords,
                    stdout_callback=cb,
                    run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                    run_tree=False,
                    forks=self.forks,
                )

                result = self._tqm.run(play)
                display.debug(result)
            finally:
                if self._tqm:
                    self._tqm.cleanup()
                if self.loader:
                    self.loader.cleanup_all_tmp_files()

            if result is None:
                display.error("No hosts found")
                return False
        except KeyboardInterrupt:
            display.error('User interrupted execution')
            return False
        except Exception as e:
            if self.verbosity >= 3:
                import traceback
                display.v(traceback.format_exc())
            display.error(to_text(e))
            return False
Example #18
0
def ansible_install_api(task_id, play_book_path, schema):
    context.CLIARGS = ImmutableDict(
        connection='smart',
        private_key_file=settings.ANSIBLE_PRIVATE_KEY,
        forks=10,
        become_method='sudo',
        become_user='******',
        check=False,
        diff=False,
        verbosity=0)

    host_list = [schema.host_ip]
    sources = ','.join(host_list)
    if len(host_list) == 1:
        sources += ','

    loader = DataLoader()
    passwords = dict(vault_pass='')

    results_callback = ResultsCollectorJSONCallback(task_id=task_id)

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

    variable_manager = VariableManager(loader=loader, inventory=inventory)
    tqm = TaskQueueManager(
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,
        passwords=passwords,
        stdout_callback=
        results_callback,  # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
    )

    play_sources = []

    import os
    os.chdir(os.path.dirname(play_book_path))
    with open(play_book_path) as f:
        data = yaml.load(f, yaml.SafeLoader)
        if isinstance(data, list):
            play_sources.extend(data)
        else:
            play_sources.append(data)

    logger.info("there are %d tasks to run", len(play_sources))
    for play_book in play_sources:
        play_book['hosts'] = host_list
        play_book['remote_user'] = '******'
        play_book['vars']['mysql_port'] = schema.port
        play_book['vars']['mysql_schema'] = schema.schema
        print("playbook", play_book)
        play = Play().load(play_book,
                           variable_manager=variable_manager,
                           loader=loader)
        # Actually run it
        try:
            result = 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 structures we use to communicate with them
            logger.info("tqm has finished")
            tqm.cleanup()
            if loader:
                loader.cleanup_all_tmp_files()

    # Create play object, playbook objects use .load instead of init or new methods,
    # this will also automatically create the task objects from the info provided in play_source

    # Remove ansible tmpdir
    shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
    return not results_callback.is_failed
Example #19
0
    def run(self, module_name, module_args):
        """
        run module from andible ad-hoc.
        module_name: ansible module_name
        module_args: ansible module args
        """
        self.results_raw = {'success': {}, 'failed': {}, 'unreachable': {}}

        # initialize needed objects
        variable_manager = VariableManager()
        loader = DataLoader()

        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=30,
                          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)

        passwords = dict(sshpass=None, becomepass=None)

        # create inventory and pass to var manager
        inventory = MyInventory(self.resource, loader,
                                variable_manager).inventory  #你要生成自己的inventory
        variable_manager.set_inventory(inventory)

        # create play with tasks
        play_source = dict(
            name="Ansible Play",
            hosts=self.host_list,  #主机ip地址,[]
            gather_facts='no',
            tasks=[dict(action=dict(module=module_name, args=module_args))
                   ]  #ansible要执行的命令
        )
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)

        # actually run it
        tqm = None
        # callback = Jsoncallback()        #ansible命令执行结果收集器
        callback = minicb()
        try:
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                # stdout_callback='json',
                # run_tree=False,
            )
            tqm._stdout_callback = callback
            callrun = tqm.run(play)
            # return callrun

            result = callback.results

        finally:
            if tqm is not None:
                tqm.cleanup()
        # return result
        return result[-1]['tasks'][-1]
Example #20
0
def main():
    host_list = ['localhost', 'www.example.com', 'www.google.com']
    # since the API is constructed for CLI it expects certain options to always be set in the context object
    context.CLIARGS = ImmutableDict(
        connection='smart',
        module_path=['/to/mymodules', '/usr/share/ansible'],
        forks=10,
        become=None,
        become_method=None,
        become_user=None,
        check=False,
        diff=False)
    # required for
    # https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L204
    sources = ','.join(host_list)
    if len(host_list) == 1:
        sources += ','

    # initialize needed objects
    loader = DataLoader(
    )  # Takes care of finding and reading yaml, json and ini files
    passwords = dict(vault_pass='******')

    # Instantiate our ResultsCollectorJSONCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
    results_callback = ResultsCollectorJSONCallback()

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

    # 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)

    # instantiate task queue manager, which takes care of forking and setting up all objects to iterate over host list and tasks
    # IMPORTANT: This also adds library dirs paths to the module loader
    # IMPORTANT: and so it must be initialized before calling `Play.load()`.
    tqm = TaskQueueManager(
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,
        passwords=passwords,
        stdout_callback=
        results_callback,  # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
    )

    # create data structure that represents our play, including tasks, this is basically what our YAML loader does internally.
    play_source = dict(
        name="Ansible Play",
        hosts=host_list,
        gather_facts='no',
        tasks=[
            dict(action=dict(module='shell', args='ls'), register='shell_out'),
            dict(action=dict(module='debug',
                             args=dict(msg='{{shell_out.stdout}}'))),
            dict(
                action=dict(module='command', args=dict(
                    cmd='/usr/bin/uptime'))),
        ])

    # Create play object, playbook objects use .load instead of init or new methods,
    # this will also automatically create the task objects from the info provided in play_source
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # Actually run it
    try:
        result = 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 structures we use to communicate with them
        tqm.cleanup()
        if loader:
            loader.cleanup_all_tmp_files()

    # Remove ansible tmpdir
    shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

    print("UP ***********")
    for host, result in results_callback.host_ok.items():
        print('{0} >>> {1}'.format(host, result._result['stdout']))

    print("FAILED *******")
    for host, result in results_callback.host_failed.items():
        print('{0} >>> {1}'.format(host, result._result['msg']))

    print("DOWN *********")
    for host, result in results_callback.host_unreachable.items():
        print('{0} >>> {1}'.format(host, result._result['msg']))
Example #21
0
def main():
    host_list = ['localhost', 'www.example.com', 'www.google.com']
    Options = namedtuple('Options', [
        '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'
    ])

    # initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='smart',
                      module_path='/usr/share/ansible',
                      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)

    passwords = dict()

    # create inventory and pass to var manager
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=host_list)
    variable_manager.set_inventory(inventory)

    # create play with tasks
    play_source = dict(
        name="Ansible Play",
        hosts=host_list,
        gather_facts='no',
        tasks=[
            dict(
                action=dict(module='command', args=dict(
                    cmd='/usr/bin/uptime')))
        ])
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # actually run it
    tqm = None
    callback = ResultsCollector()
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
        )
        tqm._stdout_callback = callback
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()

    print "UP ***********"
    for host, result in callback.host_ok.items():
        print '{} >>> {}'.format(host, result._result['stdout'])

    print "FAILED *******"
    for host, result in callback.host_failed.items():
        print '{} >>> {}'.format(host, result._result['msg'])

    print "DOWN *********"
    for host, result in callback.host_unreachable.items():
        print '{} >>> {}'.format(host, result._result['msg'])
Example #22
0
    def handle(self, *args, **options):
        AnsibleOptions = namedtuple('AnsibleOptions', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'check', 'diff'
        ])
        ansible_options = AnsibleOptions(connection='smart',
                                         module_path=[],
                                         forks=10,
                                         become=None,
                                         become_method=None,
                                         become_user=None,
                                         check=False,
                                         diff=False)
        loader = DataLoader()
        passwords = {}
        results_callback = ResultCallback()
        inventory = InventoryManager(loader=loader,
                                     sources=os.path.join(
                                         settings.BASE_DIR, 'etc', 'host'))
        variable_manager = VariableManager(loader=loader, inventory=inventory)
        path_resource = '/tmp/resource.py'
        play_source = {
            'name':
            "cmdb",
            'hosts':
            'all',
            'gather_facts':
            'no',
            'tasks': [{
                'name': 'collect_host',
                'setup': ''
            }, {
                'name':
                'copyfile',
                'copy':
                'src={0} dest={1}'.format(
                    os.path.join(settings.BASE_DIR, 'etc', 'resource.py'),
                    path_resource)
            }, {
                'name': 'collect_resource',
                'command': 'python {0}'.format(path_resource)
            }]
        }

        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=ansible_options,
                passwords=passwords,
                stdout_callback=results_callback,
            )
            result = tqm.run(play)
            #print(result)
        finally:
            if tqm is not None:
                tqm.cleanup()

            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Example #23
0
    def default(self, arg, forceshell=False):
        """ actually runs modules """
        if arg.startswith("#"):
            return False

        if not self.cwd:
            display.error("No host found")
            return False

        if arg.split()[0] in self.modules:
            module = arg.split()[0]
            module_args = ' '.join(arg.split()[1:])
        else:
            module = 'shell'
            module_args = arg

        if forceshell is True:
            module = 'shell'
            module_args = arg

        result = None
        try:
            check_raw = module in ('command', 'shell', 'script', 'raw')
            play_ds = dict(
                name="Ansible Shell",
                hosts=self.cwd,
                gather_facts='no',
                tasks=[
                    dict(action=dict(module=module,
                                     args=parse_kv(module_args,
                                                   check_raw=check_raw)))
                ],
                remote_user=self.remote_user,
                become=self.become,
                become_user=self.become_user,
                become_method=self.become_method,
                check_mode=self.check_mode,
                diff=self.diff,
            )
            play = Play().load(play_ds,
                               variable_manager=self.variable_manager,
                               loader=self.loader)
        except Exception as e:
            display.error(u"Unable to build command: %s" % to_text(e))
            return False

        try:
            cb = 'minimal'  # FIXME: make callbacks configurable
            # now create a task queue manager to execute the play
            self._tqm = None
            try:
                self._tqm = TaskQueueManager(
                    inventory=self.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    passwords=self.passwords,
                    stdout_callback=cb,
                    run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                    run_tree=False,
                    forks=self.forks,
                )

                result = self._tqm.run(play)
            finally:
                if self._tqm:
                    self._tqm.cleanup()
                if self.loader:
                    self.loader.cleanup_all_tmp_files()

            if result is None:
                display.error("No hosts found")
                return False
        except KeyboardInterrupt:
            display.error('User interrupted execution')
            return False
        except Exception as e:
            display.error(to_text(e))
            # FIXME: add traceback in very very verbose mode
            return False
Example #24
0
    def run(self):
        ''' use Runner lib to do SSH things '''

        super(AdHocCLI, self).run()


        # only thing left should be host pattern
        pattern = self.args[0]

        # ignore connection password cause we are local
        if self.options.connection == "local":
            self.options.ask_pass = False

        sshpass    = None
        becomepass    = None
        vault_pass = None

        self.normalize_become_options()
        (sshpass, becomepass) = self.ask_passwords()
        passwords = { 'conn_pass': sshpass, 'become_pass': becomepass }

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = CLI.read_vault_password_file(self.options.vault_password_file)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords(ask_vault_pass=True, ask_new_vault_pass=False, confirm_new=False)[0]

        loader = DataLoader(vault_password=vault_pass)
        variable_manager = VariableManager()
        variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options)

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

        hosts = inventory.list_hosts(pattern)
        if len(hosts) == 0:
            self.display.warning("provided hosts list is empty, only localhost is available")

        if self.options.listhosts:
            self.display.display('  hosts (%d):' % len(hosts))
            for host in hosts:
                self.display.display('    %s' % host)
            return 0

        if self.options.module_name in C.MODULE_REQUIRE_ARGS and not self.options.module_args:
            err = "No argument passed to %s module" % self.options.module_name
            if pattern.endswith(".yml"):
                err = err + ' (did you mean to run ansible-playbook?)'
            raise AnsibleOptionsError(err)

        #TODO: implement async support
        #if self.options.seconds:
        #    callbacks.display("background launch...\n\n", color='cyan')
        #    results, poller = runner.run_async(self.options.seconds)
        #    results = self.poll_while_needed(poller)
        #else:
        #    results = runner.run()

        # create a pseudo-play to execute the specified module via a single task
        play_ds = self._play_ds(pattern)
        play = Play().load(play_ds, variable_manager=variable_manager, loader=loader)

        if self.options.one_line:
            cb = 'oneline'
        else:
            cb = 'minimal'

        # now create a task queue manager to execute the play
        self._tqm = None
        try:
            self._tqm = TaskQueueManager(
                    inventory=inventory,
                    variable_manager=variable_manager,
                    loader=loader,
                    display=self.display,
                    options=self.options,
                    passwords=passwords,
                    stdout_callback=cb,
                )
            result = self._tqm.run(play)
        finally:
            if self._tqm:
                self._tqm.cleanup()

        return result
Example #25
0
    def __run(self, *module_args, **complex_args):
        '''
        The API provided by ansible is not intended as a public API.
        '''

        # Assemble module argument string
        if module_args:
            complex_args.update(dict(_raw_params=' '.join(module_args)))

        # Assert hosts matching the provided pattern exist
        hosts = self.inventory_manager.list_hosts(self.pattern)
        if len(hosts) == 0:
            raise AnsibleNoHostsMatch("No hosts match:'%s'" % self.pattern)

        # Log the module and parameters
        log.debug("[%s] %s: %s" %
                  (self.pattern, self.module_name, complex_args))

        parser = CLI.base_parser(
            runas_opts=True,
            async_opts=True,
            output_opts=True,
            connect_opts=True,
            check_opts=True,
            runtask_opts=True,
            vault_opts=True,
            fork_opts=True,
            module_opts=True,
        )
        (options, args) = parser.parse_args([])

        # Pass along cli options
        options.verbosity = 5
        options.connection = self.options.get('connection')
        options.remote_user = self.options.get('user')
        options.become = self.options.get('become')
        options.become_method = self.options.get('become_method')
        options.become_user = self.options.get('become_user')
        options.module_path = self.options.get('module_path')

        # Initialize callback to capture module JSON responses
        cb = ResultAccumulator()

        kwargs = dict(
            inventory=self.inventory_manager,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=options,
            stdout_callback=cb,
            passwords=dict(conn_pass=None, become_pass=None),
        )

        # create a pseudo-play to execute the specified module via a single task
        play_ds = dict(
            name="Ansible Ad-Hoc",
            hosts=self.pattern,
            gather_facts='no',
            tasks=[
                dict(action=dict(module=self.module_name, args=complex_args)),
            ])
        play = Play().load(play_ds,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        # now create a task queue manager to execute the play
        tqm = None
        try:
            tqm = TaskQueueManager(**kwargs)
            results = tqm.run(play)
        finally:
            if tqm:
                tqm.cleanup()

        # Log the results
        log.debug(cb.results)

        # FIXME - should command failures raise an exception, or return?
        # If we choose to raise, callers will need to adapt accordingly
        # Catch any failures in the response
        # for host in results['contacted'].values():
        #     if 'failed' in host or host.get('rc', 0) != 0:
        #         raise Exception("Command failed: %s" % self.module_name, results)

        # Raise exception if host(s) unreachable
        # FIXME - if multiple hosts were involved, should an exception be raised?
        if cb.unreachable:
            raise AnsibleHostUnreachable("Host unreachable",
                                         dark=cb.unreachable,
                                         contacted=cb.contacted)

        # No hosts contacted
        # if not cb.contacted:
        #     raise ansible.errors.AnsibleConnectionFailed("Provided hosts list is empty")

        # Success!
        # return results
        return cb.contacted
Example #26
0
def play_book(playbook, passwords={}, inventory=None, extra_var={}):
    source = inventory
    if not inventory:
        source = '/etc/ansible/hosts'
    loader = DataLoader()
    inventory_data = InventoryManager(loader=loader, sources=[source])
    variable_manager = VariableManager(loader=loader, inventory=inventory_data)
    variable_manager.extra_vars = extra_var
    results_callback = ResultCallback()
    Options = namedtuple('Options',
                    ['connection',
                    'remote_user',
                    'ask_sudo_pass',
                    'verbosity',
                    'ack_pass',
                    'module_path',
                    'forks',
                    'become',
                    'become_method',
                    'become_user',
                    'check',
                    'listhosts',
                    'listtasks',
                    'listtags',
                    'syntax',
                    'sudo_user',
                    'sudo',
                    'diff'])
    options = Options(connection='smart',
                    remote_user=None,
                    ack_pass=None,
                    sudo_user=None,
                    forks=5,
                    sudo=True,
                    ask_sudo_pass=False,
                    verbosity=5,
                    module_path=None,
                    become=None,
                    become_method=None,
                    become_user=None,
                    check=False,
                    diff=False,
                    listhosts=None,
                    listtasks=None,
                    listtags=None,
                    syntax=None)

    play = Play().load(playbook, variable_manager=variable_manager, loader=loader)
    tqm = None

    try:
        tqm = TaskQueueManager(
                inventory=inventory_data,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback=results_callback,
            )
        tqm.run(play)
    except Exception as e:
        print(e)
    finally:
        if tqm is not None:
            tqm.cleanup()
Example #27
0
    def _run(self, *module_args, **complex_args):
        """Execute an ansible adhoc command returning the result in a AdhocResult object."""
        # Assemble module argument string
        if module_args:
            complex_args.update(dict(_raw_params=' '.join(module_args)))

        # Assert hosts matching the provided pattern exist
        hosts = self.options['inventory_manager'].list_hosts()
        no_hosts = False
        if len(hosts) == 0:
            no_hosts = True
            warnings.warn(
                "provided hosts list is empty, only localhost is available")

        self.options['inventory_manager'].subset(self.options.get('subset'))
        hosts = self.options['inventory_manager'].list_hosts(
            self.options['host_pattern'])
        if len(hosts) == 0 and not no_hosts:
            raise ansible.errors.AnsibleError(
                "Specified hosts and/or --limit does not match any hosts")

        # Log the module and parameters
        log.debug("[%s] %s: %s" % (self.options['host_pattern'],
                                   self.options['module_name'], complex_args))

        # Pass along cli options
        args = ['pytest-ansible', '-vvvvv', self.options['host_pattern']]
        for argument in ('connection', 'user', 'become', 'become_method',
                         'become_user', 'module_path'):
            arg_value = self.options.get(argument)
            argument = argument.replace('_', '-')

            if arg_value in (None, False):
                continue

            if arg_value is True:
                args.append('--{0}'.format(argument))
            else:
                args.append('--{0}={1}'.format(argument, arg_value))

        # Use Ansible's own adhoc cli to parse the fake command line we created and then save it
        # into Ansible's global context
        adhoc = AdHocCLI(args)
        adhoc.parse()

        # And now we'll never speak of this again
        del adhoc

        # Initialize callback to capture module JSON responses
        cb = ResultAccumulator()

        kwargs = dict(
            inventory=self.options['inventory_manager'],
            variable_manager=self.options['variable_manager'],
            loader=self.options['loader'],
            stdout_callback=cb,
            passwords=dict(conn_pass=None, become_pass=None),
        )

        # create a pseudo-play to execute the specified module via a single task
        play_ds = dict(name="pytest-ansible",
                       hosts=self.options['host_pattern'],
                       become=self.options['become'],
                       become_user=self.options['become_user'],
                       gather_facts='no',
                       tasks=[
                           dict(action=dict(module=self.options['module_name'],
                                            args=complex_args), ),
                       ])
        log.debug("Play(%s)", play_ds)
        play = Play().load(play_ds,
                           variable_manager=self.options['variable_manager'],
                           loader=self.options['loader'])

        # now create a task queue manager to execute the play
        tqm = None
        try:
            log.debug("TaskQueueManager(%s)", kwargs)
            tqm = TaskQueueManager(**kwargs)
            tqm.run(play)
        finally:
            if tqm:
                tqm.cleanup()

        # Log the results
        log.debug(cb.results)

        # Raise exception if host(s) unreachable
        # FIXME - if multiple hosts were involved, should an exception be raised?
        if cb.unreachable:
            raise AnsibleConnectionFailure("Host unreachable",
                                           dark=cb.unreachable,
                                           contacted=cb.contacted)

        # Success!
        return AdHocResult(contacted=cb.contacted)
Example #28
0
    def myexec(self):
        # 设置需要初始化的ansible配置参数
        Options = namedtuple('Options', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'check'
        ])
        # 初始化需要的对象
        variable_manager = VariableManager()
        loader = DataLoader()
        # connection这里用ssh连接方式,本地可以用local; module_path指定正确的ansible module路径
        options = Options(connection='smart',
                          module_path=None,
                          forks=100,
                          become=None,
                          become_method=None,
                          become_user=None,
                          check=False)
        # passwords = dict(vault_pass='******')
        passwords = None

        # Instantiate our ResultCallback for handling results as they come in
        results_callback = self.ResultCallback()
        # ssh连接采用password认证
        variable_manager.extra_vars = {
            "ansible_user": self.username,
            "ansible_ssh_pass": self.password
        }
        # 初始化inventory, host_list后面可以是列表或inventory文件
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=self.host_list)
        variable_manager.set_inventory(inventory)
        C.HOST_KEY_CHECKING = False

        # create play with tasks
        play_source = dict(
            name="Ansible Play",
            hosts=self.host,  # 这里指定all
            gather_facts='no',
            tasks=[
                dict(action=self.action, register='copy_out'),
                # dict(action=dict(module='debug', args=dict(msg='')))
            ])
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)
        # print(play,"1111111111111111111")

        # actually run it
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback=
                results_callback,  # Use our custom   callback instead of the ``default`` callback plugin
            )
            result = tqm.run(play)
            print(result, "2222222222222222")
            # print(tqm,"3333333333333333")
            # tqm.run(play)
            # print("444444444444")
        except Exception as exc:
            raise TaskExecutionException(str(exc))
        finally:
            if tqm is not None:
                tqm.cleanup()
Example #29
0
    def run(self):
        # insert node
        for ip in self.hosts:
            self._node_map[ip] = Service.new_node(self.task_id, ip)

        variable_manager = VariableManager()

        Logger.debug("start write ssh_key for task: {} global_id : {}".format(
            self.task_id, self.global_id))

        key_files = []

        group = Group(self.task_id)

        for h in self.hosts:

            # get ssh_key content
            key_content = _get_ssh_key(h)

            Logger.debug("read ssh_key for host: {} global_id: {}".format(
                h, self.global_id))

            # write ssh private key
            key_path = _write_ssh_key(h, key_content)

            #key_path="./tmp/97"
            Logger.debug("write ssh_key for host: {} global_id: {}".format(
                h, self.global_id))

            host_vars = dict(ansible_port=22,
                             ansible_user=self.user,
                             ansible_ssh_private_key_file="./" + key_path)

            Logger.debug("key_path: {} global_id: {}".format(
                key_path, self.global_id))

            key_files.append(key_path)

            host = Host(h)

            host.vars = host_vars

            group.add_host(host)

        # add params to each host
        if self.params is not None and isinstance(self.params, dict):
            for h in group.hosts:
                for key in self.params.keys():
                    variable_manager.set_host_variable(h, key,
                                                       self.params[key])

        Logger.debug("success write ssh_key for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # other options
        ssh_args = '-oControlMaster=auto -oControlPersist=60s -oStrictHostKeyChecking=no'
        options = _Options(connection='ssh',
                           module_path='./ansible/library',
                           forks=self.forks,
                           timeout=10,
                           remote_user=None,
                           private_key_file=None,
                           ssh_common_args=ssh_args,
                           ssh_extra_args=None,
                           sftp_extra_args=None,
                           scp_extra_args=None,
                           become=None,
                           become_method=None,
                           become_user=None,
                           verbosity=None,
                           check=False)

        if self.tasktype == "ansible_task":
            Logger.debug(
                "ansible tasks set*******************  global_id: {}".format(
                    self.global_id))
            play_source = dict(name=self.task_id,
                               hosts=self.task_id,
                               gather_facts='yes',
                               tasks=self.tasks)
        else:

            Logger.debug(
                "ansible role set******************* global_id: {}".format(
                    self.global_id))
            play_source = dict(name=self.task_id,
                               hosts=self.task_id,
                               gather_facts='yes',
                               roles=self.tasks)

        Logger.debug("start load play for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # make playbook
        playbook = Play().load(play_source,
                               variable_manager=variable_manager,
                               loader=_Loader)

        inventory = Inventory(loader=_Loader,
                              variable_manager=variable_manager)

        inventory.add_group(group)

        call_back = SyncCallbackModule(debug=True,
                                       step_callback=self._step_callback,
                                       global_id=self.global_id,
                                       source=self.source,
                                       tag_hosts=self.hosts)

        Logger.debug("success load play for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # task queue
        tqm = TaskQueueManager(inventory=inventory,
                               variable_manager=variable_manager,
                               loader=_Loader,
                               options=options,
                               passwords=None,
                               stdout_callback=call_back)

        try:
            back = tqm.run(playbook)

            Logger.info("back: {} global_id : {}".format(
                str(back), self.global_id))

            if back != 0:
                raise Exception("playbook run failed")

            return back
        finally:
            if tqm is not None:
                tqm.cleanup()
                _rm_tmp_key(key_files)
Example #30
0
    def prepare_cluster_obj(self, cluster, res):
        cluster_args = cluster["args"] if "args" in cluster else tuple()
        cluster_kwargs = update_d(
            {
                "domain": self.dns_name,
                "node_name": self.node_name,
                "public_ipv4": self.node.public_ips[-1],
                "cache": {},
                "cluster_name": cluster.get("cluster_name"),
            },
            cluster["kwargs"] if "kwargs" in cluster else {},
        )
        if "source" in cluster:
            cluster_kwargs.update(play_source=cluster["source"])
        cluster_type = cluster["module"].replace("-", "_")
        cluster_path = "/".join(_f for _f in (cluster_type,
                                              cluster_kwargs["cluster_name"])
                                if _f)
        cluster_kwargs.update(cluster_path=cluster_path)
        if "cache" not in cluster_kwargs:
            cluster_kwargs["cache"] = {}

        if ":" in cluster_type:
            cluster_type, _, tag = cluster_type.rpartition(":")
            del _
        else:
            tag = None

        cluster_kwargs.update(tag=tag)

        # create play with tasks
        if "play_source" not in cluster_kwargs:
            raise NotImplementedError(
                "Ansible from module/playbook not implemented yet. Inline your source."
            )
        cluster_kwargs["play_source"].update(hosts=self.env_namedtuple.hosts)
        extra_vars = {
            "ansible_host": self.env_namedtuple.host,
            "ansible_ssh_host": self.env_namedtuple.host,
            "ansible_user": self.env_namedtuple.user,
            "ansible_ssh_user": self.env_namedtuple.user,
            "ansible_port": self.env_namedtuple.port,
            "ansible_ssh_port": self.env_namedtuple.port,
        }

        if self.env.password is not None:
            extra_vars.update(
                {"ansible_ssh_pass": self.env_namedtuple.password})
        if self.env.key_filename is None:
            extra_vars.update({
                "ansible_ssh_private_key_file":
                self.env_namedtuple.key_filename
            })
        if (self.env.ssh_config is not None
                and "StrictHostKeyChecking" in self.env.ssh_config):
            host_key_checking = (self.env_namedtuple.
                                 ssh_config["StrictHostKeyChecking"] == "yes")
            extra_vars.update({
                "ansible_ssh_extra_args":
                "-o " +
                " -o ".join('{k}="{v}"'.format(k=k, v=v)
                            for k, v in iteritems(self.env.ssh_config)
                            if k not in frozenset(("Port", "User", "Host"))),
                "ansible_host_key_checking":
                host_key_checking,
            })

        self.variable_manager.extra_vars = extra_vars
        cluster_kwargs["play"] = Play().load(
            cluster_kwargs["play_source"],
            variable_manager=self.variable_manager,
            loader=self.loader,
        )

        return PreparedClusterObj(
            cluster_path=cluster_path,
            cluster_type=cluster_type,
            cluster_args=cluster_args,
            cluster_kwargs=cluster_kwargs,
            res=res,
            tag=tag,
        )