Exemplo n.º 1
0
def ansible_adhoc(ips):
    logger.info("ansible需要采集%d个IP" % (len(ips)))
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=ips)
    # 根据 inventory 加载对应变量
    variable_manager.set_inventory(inventory)
    # 增加外部变量
    variable_manager.extra_vars = {"ansible_ssh_user": ansible_username, "ansible_ssh_pass": ansible_password}
    play_source = {"name": "Ansible Ad-Hoc", "hosts": ips, "gather_facts": "no",
                   "tasks": [{"action": {"module": "setup", "args": ""}}]}
    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=None,
            stdout_callback=resultcallback,
            run_tree=False,
        )
        tqm.run(play)
        return ansible_facts_info
    finally:
        if tqm is not None:
            tqm.cleanup()
Exemplo n.º 2
0
    def run(self, host_list, module_name, module_args,):
        """
        run module from andible ad-hoc.
        module_name: ansible module_name
        module_args: ansible module args
        """
        # create play with tasks
        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)

        # actually run it
        tqm = None
        self.callback = ResultsCollector()
        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
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 3
0
    def run(self, play_data):
        """
        paly_data = dict(
            name="Ansible Ad-Hoc",
            hosts=pattern,
            gather_facts=True,
            tasks=[dict(action=dict(module='service', args={'name': 'vsftpd', 'state': 'restarted'}), async=async, poll=poll)]
        )
        """
        self._prepare_run()

        play = Play().load(play_data, 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.cb,
                run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                run_tree=False,
            )

            result = tqm.run(play)
            return result
        finally:
            if tqm:
                tqm.cleanup()
Exemplo n.º 4
0
    def run(self):

        # create play with tasks
        play_source =  dict(
                name = "Ansible Play",
                hosts = self.host_list,
                #hosts = 'localhost',
                gather_facts = 'no',
                tasks = [
                    dict(action=dict(module='shell', args='ls')),
                    #dict(action=dict(module='shell', args='ls'), 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)

        # actually 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=self.results_callback,  # Use our custom callback instead of the ``default`` callback plugin
                  )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 5
0
def my_runner(host_list,module_name,module_args):
 variable_manager.extra_vars={} # 增加外部变量
 # 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样
 # :param name: 任务名,类似playbook中tasks中的name
 # :param hosts: playbook中的hosts
 # :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task
 play_source = {"name":"Ansible Ad-Hoc","hosts":host_list,"gather_facts":"no","tasks":[{"action":{"module":module_name,"args":module_args}}]}
 play = Play().load(play_source,variable_manager=variable_manager,loader=loader)
 tqm = None
# results_callback = call_json.CallbackModule()
 try:
   tqm = TaskQueueManager(
	inventory=inventory,
 	variable_manager=variable_manager,
 	loader=loader,
 	options=options,
 	passwords=None,
 	stdout_callback='minimal',
#	stdout_callback=results_callback,  # Use our custom callback instead of the ``default`` callback plugin
   )
   savedStdout = sys.stdout
   with open(File_PATH,'w+') as file:
     sys.stdout = file #标准输出重定向至文件
     result = tqm.run(play)
   sys.stdout = savedStdout  
   return result
 
 finally:
   if tqm is not None:
     tqm.cleanup()
Exemplo n.º 6
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 or self.logId: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  
         constants.HOST_KEY_CHECKING = False #关闭第一次使用ansible连接客户端是输入命令
         tqm.run(play)  
     except Exception as err: 
         if self.redisKey:DsRedis.OpsAnsibleModel.lpush(self.redisKey,data=err)
         if self.logId:AnsibleSaveResult.Model.insert(self.logId, err)              
     finally:  
         if tqm is not None:  
             tqm.cleanup()  
Exemplo n.º 7
0
    def run_module(self, hosts, module_name, module_args):
        # create play with tasks
        play_source = dict(
            name="Ansible Play",
            hosts=hosts,
            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)

        self.results_callback = ModuleResultCallback()
        # actually 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=self.
                results_callback,  # Use our custom callback instead of the ``default`` callback plugin
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 8
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)  
     finally:  
         if tqm is not None:  
             tqm.cleanup()  
Exemplo n.º 9
0
def run_adhoc():
    variable_manager.extra_vars = {
        "ansible_ssh_user": "******", "ansible_ssh_pass": "******"
    }  # 增加外部变量
    # 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样
    # :param name: 任务名,类似playbook中tasks中的name
    # :param hosts: playbook中的hosts
    # :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task
    play_source = {
        "name": "Ansible Ad-Hoc",
        "hosts": "localhost",
        "gather_facts": "no",
        "tasks": [
            {"action": {"module": "shell", "args": "w"}}
        ]
    }
    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=None,
            stdout_callback='minimal',
            run_tree=False,
        )
        result = tqm.run(play)
        print result
    finally:
        if tqm is not None:
            tqm.cleanup()
Exemplo n.º 10
0
 def run_module(self, hosts='localhost', rules=[{'module': 'setup'}]):
     '''
     rules=[
           {'module': 'shell', 'args': 'echo "ok"', 'register': 'echo_ok'},
           {'module': 'debug', 'args': {'msg': '{{echo_ok.stdout}}'}}
     ]
     '''
     tasks = []
     for rule in rules:
         if 'register' in rule:
             register = rule.pop('register')
         tasks.append(dict(dict(action=rule), register=register))
     play_source =  dict(
         name = "Ansible Play",
         hosts = hosts,
         gather_facts = 'no',
         tasks = tasks
     )
     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,
          )
         tqm.run(play)
         return results_callback
     finally:
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 11
0
    def test_strategy_base_run_handlers(self, mock_worker):
        def fake_run(*args):
            return
        mock_worker.side_effect = fake_run
        mock_play_context = MagicMock()

        mock_handler_task = MagicMock(Handler)
        mock_handler_task.action = 'foo'
        mock_handler_task.get_name.return_value = "test handler"
        mock_handler_task.has_triggered.return_value = False

        mock_handler = MagicMock()
        mock_handler.block = [mock_handler_task]
        mock_handler.flag_for_host.return_value = False

        mock_play = MagicMock()
        mock_play.handlers = [mock_handler]

        mock_host = MagicMock(Host)
        mock_host.name = "test01"
        mock_host.has_hostkey = True

        mock_inventory = MagicMock()
        mock_inventory.get_hosts.return_value = [mock_host]

        mock_var_mgr = MagicMock()
        mock_var_mgr.get_vars.return_value = dict()

        mock_iterator = MagicMock
        mock_iterator._play = mock_play

        fake_loader = DictDataLoader()
        mock_options = MagicMock()
        mock_options.module_path = None

        tqm = TaskQueueManager(
            inventory=mock_inventory,
            variable_manager=mock_var_mgr,
            loader=fake_loader,
            options=mock_options,
            passwords=None,
        )
        tqm._initialize_processes(3)
        tqm._initialize_notified_handlers(mock_play)
        tqm.hostvars = dict()

        try:
            strategy_base = StrategyBase(tqm=tqm)

            strategy_base._inventory = mock_inventory
            strategy_base._notified_handlers = {mock_handler_task: [mock_host]}

            task_result = TaskResult(Host('host01'), Handler(), dict(changed=False))
            tqm._final_q.put(('host_task_ok', task_result))

            result = strategy_base.run_handlers(iterator=mock_iterator, play_context=mock_play_context)
        finally:
            tqm.cleanup()
Exemplo n.º 12
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']))
Exemplo n.º 13
0
    def run(self, host_list, 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':{}}
        Options = namedtuple('Options', ['connection','module_path', 'forks', 'timeout',  'remote_user',
                'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass', 'verbosity', 'check'])


        options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, timeout=10,
                remote_user='******', ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
                sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None,
                become_user='******',ask_value_pass=False, verbosity=None, check=False)

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

        # create play with tasks
        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)

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

        for host, result in callback.host_ok.items():
            self.results_raw['success'][host] = result._result.get('stdout') + result._result.get('stderr')

        for host, result in callback.host_failed.items():
            self.results_raw['failed'][host] = result._result.get('stdout') + result._result.get('stderr')

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

        logger.info(self.results_raw)
        return self.results_raw
Exemplo n.º 14
0
def main(args):
    # Options definition
    # Custom tuple to store playbook options
    Options = namedtuple('Options', ['connection', 'module_path', 'forks',
                                     'become', 'become_method', 'become_user',
                                     'check'])

    # Object initialization
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='ssh',module_path='library',
                      forks=100, become=None, become_method=None,
                      become_user=None, check=False)
    passwords = {}

    # Dinamyc inventory
    inventory = Inventory(loader=loader, variable_manager=variable_manager,
                          host_list=args)

    # Inventory assignation
    variable_manager.set_inventory(inventory)

    # Play creation with tasks
    play_source = dict(
         name="Ansible Play",
         hosts=args,
         gather_facts='no',
         tasks=[
             dict(action=dict(module='shell', args='hostname -f'),
                  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)

    # Running it
    tqm = None
    try:
        tqm = TaskQueueManager(
                      inventory=inventory,
                      variable_manager=variable_manager,
                      loader=loader,
                      options=options,
                      passwords=passwords,
                      stdout_callback='default'
        )
        result = tqm.run(play)
    finally:
        if tqm is not None:
                    tqm.cleanup()
Exemplo n.º 15
0
def main(argv=sys.argv[1:]):
    Options = namedtuple('Options', ['connection', 'module_path', 'forks',
                                     'become', 'become_method', 'become_user',
                                     'check'])
    # initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='local', module_path='/path/to/mymodules',
                      forks=100, become=None, become_method=None,
                      become_user=None,
                      check=False)

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

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

    variable_manager.set_inventory(inventory)

    # create play with tasks
    play_source = dict(name="Ansible Play",
                       hosts='localhost',
                       gather_facts='no',
                       tasks=[
                           dict(action=dict(module='shell',
                                args='uname -a'), 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:
        tqm = TaskQueueManager(inventory=inventory,
                               variable_manager=variable_manager,
                               loader=loader,
                               options=options,
                               passwords=passwords,
                               stdout_callback='default',
                               )

        result = tqm.run(play)
        print result
    finally:
        if tqm is not None:
            tqm.cleanup()
Exemplo n.º 16
0
    def test_base(self):
        test_inv_dir = 'test/inventory'
        for inv in os.listdir(test_inv_dir):
            print "Processing ", inv
            res = dynlxc.main(os.path.join(test_inv_dir, inv), '')

            variable_manager = VariableManager()
            loader = DataLoader()
            self.mock_rv.communicate.return_value = [
                json.dumps(res), 'mocked_err']
            try:
                inventory = Inventory(
                    loader=loader,
                    variable_manager=variable_manager,
                    host_list='inventory/dynlxc.py'
                )
            except Exception as err:
                raise Exception("Inventory file {0} processing result '{1}' "
                                "failed with {2}".format(inv, res, err))
            variable_manager.set_inventory(inventory)

            play_source = dict(name="Ansible Play", hosts='localhost',
                               gather_facts='no')

            playbook = os.path.abspath(os.path.join(test_inv_dir,
                                       '../playbooks', inv))
            if os.path.isfile(playbook):
                with open(playbook) as fh:
                    real_playbook = yaml.load(fh)[0]
                    play_source.update(real_playbook)

            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=self.options,
                    passwords=None,
                    stdout_callback='default',
                )
                result = tqm.run(play)
                assert result == 0, ("Ansible playbook exitcode "
                                     "different from 0")
            finally:
                if tqm is not None:
                    tqm.cleanup()
Exemplo n.º 17
0
 def run(self):
     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,
         )
         for name, play in self.plays.items():
             result = tqm.run(play)
     finally:
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 18
0
    def run(
            self,
            tasks,
            pattern,
            play_name='Ansible Ad-hoc',
            gather_facts='no',):
        """
        :param gather_facts:
        :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ]
        :param pattern: all, *, or others
        :param play_name: The play name
        :return:
        """
        self.check_pattern(pattern)
        results_callback = self.results_callback_class()
        cleaned_tasks = self.clean_tasks(tasks)

        play_source = dict(
            name=play_name,
            hosts=pattern,
            gather_facts=gather_facts,
            tasks=cleaned_tasks
        )

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

        tqm = TaskQueueManager(
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=self.options,
            stdout_callback=results_callback,
            passwords=self.options.passwords,
        )

        try:
            tqm.run(play)
            return results_callback
        except Exception as e:
            raise AnsibleError(e)
        finally:
            tqm.cleanup()
            self.loader.cleanup_all_tmp_files()
def main(host_list,module,args):
    Options = namedtuple('Options', ['connection','module_path', 'forks', 'remote_user',
            '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='xxx', forks=50,
            remote_user='******', 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(sshpass=None, becomepass=None)

    # 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=module, args=args)) ]
        )
    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
        tqm.run(play)
        return callback.results[0]['tasks'][0]['hosts']

    finally:
        if tqm is not None:
            tqm.cleanup()
def run(play):
    tqm = None
    results = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback='default',
        )
        results = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
    return tqm, results
Exemplo n.º 21
0
def execute_tasks(play_name, tasks, hosts, host_list=os.path.join(DIRNAME, 'ansible_hosts.py'), callback="default"):
    # NOTICE: Here is a trick. The host we acquired must in the host list
    # everytime. However I can't get the host list in advance. So I add the
    # hosts into the host list eveytime if it doesn't exist.
    if hosts not in ansible_hosts.hosts["all"]["hosts"]:
        ansible_hosts.hosts["all"]["hosts"].append(hosts)

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

    # 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=play_name,
        hosts=hosts,
        gather_facts='no',
        tasks=tasks)

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

    Options = namedtuple('Options', ['connection', 'module_path', 'forks',
        'become', 'become_method', 'become_user', 'check'])
    options = Options(connection=None, module_path=None, forks=10, become=None,
        become_method=None, become_user=None, check=False)
    passwords = dict()

    # actually run it
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=callback)
        return tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
        pass
 def run(self):
     result = {}
     results_callback = ResultCallback()
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=self.passwords,
             stdout_callback=results_callback
         )
         tqm.run(self.play)
     finally:
         result = results_callback.result
         if tqm is not None:
             tqm.cleanup()
     return result
Exemplo n.º 23
0
    def run(self):
        tqm = None
        try:
            tqm = TaskQueueManager(inventory=self.inventory,
                                   variable_manager=self.variable_manager,
                                   loader=self.loader,
                                   options=self.options,
                                   passwords={},
                                   stdout_callback=self.stdout_callback)
            self.consumer.send("Start => \r\n")
            for p in self.play:
                result = tqm.run(p)
            # self.delete_key()

            self.consumer.send('执行完毕\r\n')
        finally:
            self.consumer.close()
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 24
0
    def Arunner(self, ansible_host_path, module, args):

        inventory = InventoryManager(loader=self.loader,
                                     sources=[ansible_host_path])
        # 加载之前的变量
        variable_manager = VariableManager(loader=self.loader,
                                           inventory=inventory)
        play_source = dict(
            name="Ansible Play",
            hosts="all",  # all表示匹配清单所有机器,看源码发现的
            gather_facts="no",
            tasks=[
                dict(action=dict(module=module, args=args),
                     register='shell_out'),
            ])
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=self.loader)

        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=self.loader,
                options=self.ops,
                passwords=self.passwords,
                stdout_callback=self.callback,
            )
            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
        for host, result in self.callback.host_unreachable.items():
            result_raw['failed'][host] = result
        for host, result in self.callback.host_failed.items():
            result_raw['unreachable'][host] = result
        return json.dumps(result_raw, indent=4)
Exemplo n.º 25
0
def play_vars(ctx):
    # create inventory, use path to host config file as source or hosts in a comma
    # separated string
    inventory = InventoryManager(loader=loader, sources="inventory")
    # 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)
    # 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="all",
                       gather_facts="no",
                       tasks=[])
    loader.set_vault_secrets([("default", VaultSecret(_bytes=to_bytes("TBD")))
                              ])
    # 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)
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            passwords=dict(),
            # Use our custom callback instead of the ``default`` callback
            # plugin, which prints to stdout
            stdout_callback=results_callback,
        )
        # most interesting data for a play is actually
        # sent to the callback's methods
        tqm.run(play)
    finally:
        # we always need to cleanup child procs and the structures we use to
        # communicate with them
        if tqm is not None:
            tqm.cleanup()
        # Remove ansible tmpdir
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
    ctx.hostvars = variable_manager.get_vars()["hostvars"]
Exemplo n.º 26
0
 def run(self):
     # actually run it
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=dict(),
         )
         tqm._stdout_callback = self.call_back
         result = tqm.run(self.play)
         self._summary = self.call_back.summary
         self._results = self.call_back.results
         return result
     finally:
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 27
0
    def run_playbook(self, playbook):
        '''Run an ansible playbook using the ansible python api.'''
        play = self._get_play_(playbook)
        tqm = None
        self.results_callback = ResultCallback()

        try:
            tqm = TaskQueueManager(inventory=self._inventory,
                                   variable_manager=self._variable_manager,
                                   loader=self._loader,
                                   passwords=dict(),
                                   stdout_callback=self.results_callback)
            tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        return self.results_callback.result_playbook_run
Exemplo n.º 28
0
 def exec_shell(self,hostlist=[],command=None):
     source = {'hosts': hostlist, 'gather_facts': 'no', 'tasks': [
         {'action': {'module': 'shell', 'args': command}, 'register': 'shell_out'}]}
     play = Play().load(source, variable_manager=self.variable_manager, loader=self.loader)
     try:
         taskqm = TaskQueueManager(
             inventory = self.inventory,
             variable_manager = self.variable_manager,
             loader = self.loader,
             options = self.options,
             passwords = self.passwords,
             # stdout_callback = self.result_callback,
         )
         taskqm.run(play)
     except:
         raise
     finally:
         if taskqm is not None:
             taskqm.cleanup()
Exemplo n.º 29
0
def adhoc_cmd(sources=None, hosts=None, module=None, args=None):
    Options = namedtuple('Options', [
        'connection', 'module_path', 'forks', 'become', 'become_method',
        'become_user', 'check', 'diff'
    ])
    options = Options(connection='smart',
                      module_path=['/to/mymodules'],
                      forks=10,
                      become=None,
                      become_method=None,
                      become_user=None,
                      check=False,
                      diff=False)
    loader = DataLoader()
    passwords = dict()
    inventory = InventoryManager(loader=loader, sources=sources)
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    play_source = dict(
        name="My Ansible Task Test",
        hosts=hosts,
        gather_facts='no',
        tasks=[
            dict(action=dict(module=module, args=args), 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)

    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
        )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 30
0
def ansible_get_info(host_path):

    Options = namedtuple('Options',
                         ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check'])
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='ssh', module_path='/Library/Python/2.7/site-packages/ansible', forks=100, become=None,
                      become_method=None, become_user=None, check=False)
    passwords = dict(vault_pass='******')

    results_callback = ResultCallback()

    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_path)
    # inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=['172.16.0.50', '172.16.0.190'])
    # variable_manager.extra_vars={"ansible_ssh_user":"******" , "ansible_ssh_pass":"******"}

    variable_manager.set_inventory(inventory)

    play_source = dict(
        name="Ansible Play",
        hosts='all',
        gather_facts='no',
        tasks=[
            dict(action=dict(module='setup'), register='shell_out')
        ]
    )

    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=results_callback,
        )
        tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
Exemplo n.º 31
0
 def run(self):
     # create play with tasks
     # 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}}')))
     #          ]
     #     )
     from ansible.playbook import Playbook
     pb = Playbook.load(self.playbook_path,
                        variable_manager=self.variable_manager,
                        loader=self.loader)
     # pbex = PlaybookExecutor(playbooks=[self.playbook_path],
     #                         inventory=self.inventory,
     #                         variable_manager=self.variable_manager,
     #                         loader=self.loader,
     #                         options=self.options,
     #                         passwords=self.passwords)
     # play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
     plays = pb.get_plays()
     print("plays_type", type(plays))
     for play in plays:
         print("play type:", type(play))
         print("play :", dir(play))
         # actually 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=self.
                 results_callback,  # Use our custom callback instead of the ``default`` callback plugin
             )
             result = tqm.run(play)
         finally:
             if tqm is not None:
                 tqm.cleanup()
Exemplo n.º 32
0
 def _run_play(self, play_src):
     self.options = options = self.Options(**self.options_args)
     vm = self.variable_manager
     vm.extra_vars = load_extra_vars(loader=self.loader, options=options)
     play = Play().load(play_src, loader=self.loader, variable_manager=vm)
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=vm,
             loader=self.loader,
             options=options,
             passwords=dict(),
             stdout_callback='default')
         tqm.run(play)
         return vm.get_vars(self.loader)
     finally:
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 33
0
    def run(self, play):
        tqm = None
        rc = ResultCallback()
        try:
            tqm = TaskQueueManager(inventory=self._inventory,
                                   variable_manager=self._variable_manager,
                                   loader=self._loader,
                                   passwords=dict(),
                                   stdout_callback=rc)
            tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        if rc.failed_playbook_run:
            raise AnsibleRunError('Unable to run playbook.',
                                  rc.result_playbook_run[-1])
        return rc.result_playbook_run
Exemplo n.º 34
0
    def ansible_run(self, host_list, task_list):
        context._init_global_context(self.ops)
        play_source = dict(name="Ansible Play",
                           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.ops,
                passwords=self.passwords,
                stdout_callback=self.results_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)

        results_raw = {}
        results_raw['success'] = {}
        results_raw['failed'] = {}
        results_raw['unreachable'] = {}

        for host, result in self.results_callback.host_ok.items():
            results_raw['success'][host] = json.dumps(result._result)

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

        for host, result in self.results_callback.host_unreachable.items():
            results_raw['unreachable'][host] = result._result['msg']

        return results_raw
Exemplo n.º 35
0
    def run(self, host_list, module_name, module_args=None):
        """
        从 andible ad-hoc 运行模块.
        module_name: ansible 模块名称 (-m)
        module_args: ansible 模块参数 (-a)
        """

        # 创建任务
        play_source = dict(
            name="Ansible Play",
            hosts=host_list,
            gather_facts='no',
            tasks=[
                dict(action=dict(module=module_name, args=module_args))
                # dict(action=dict(module='shell', args="id"), register='shell_out'),
                # dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')), async=0, poll=15)
            ])
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        # 实际运行
        # TaskQueueManager 是创建进程池, 负责输出结果和多进程间数据结构或者队列的共享协作
        tqm = None
        # 结果回调类实例化
        self.callback = ResultsCollector()
        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()
            if self.loader:
                self.loader.cleanup_all_tmp_files()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 36
0
    def run_module(self,
                   host_list,
                   module_name,
                   module_args,
                   deploy=False,
                   send_msg=True):
        """
        run module from ansible ad-hoc.
        """
        self.callback = DeployResultsCollector(
            self.sock,
            send_msg=send_msg) if deploy else ModuleResultsCollector(self.sock)

        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)

        # actually 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=self.callback,
            )

            C.HOST_KEY_CHECKING = False  # 关闭第一次使用ansible连接客户端是输入命令
            tqm.run(play)
        except Exception as e:
            ansible_logger.error('执行{}失败,原因: {}'.format(module_name, e))
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 37
0
    def run(self, host_list, task_list):
        play_source = dict(name="Ansible Task",
                           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.ops,
                passwords=self.passwords,
                stdout_callback=self.results_callback,
                run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                run_tree=False,
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
                os.remove(self.hostsFile.name)
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        results_raw = {}
        results_raw['success'] = {}
        results_raw['failed'] = {}
        results_raw['unreachable'] = {}

        for host, result in self.results_callback.host_ok.items():
            results_raw['success'][host] = result._result["stdout_lines"]

        for host, result in self.results_callback.host_failed.items():
            results_raw['failed'][host] = result._result["stdout_lines"]

        for host, result in self.results_callback.host_unreachable.items():
            results_raw['unreachable'][host] = "Failed to connect to the host"

        print json.dumps(results_raw)
Exemplo n.º 38
0
    def run(self,
            tasks,
            pattern,
            play_name='Ansible Ad-hoc',
            gather_facts='no'):
        """
        :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ]
        :param pattern: all, *, or others
        :param play_name: The play name
        :return:
        """
        self.check_pattern(pattern)
        results_callback = self.results_callback_class()
        cleaned_tasks = self.clean_tasks(tasks)

        play_source = dict(name=play_name,
                           hosts=pattern,
                           gather_facts=gather_facts,
                           tasks=cleaned_tasks)

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

        tqm = TaskQueueManager(
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=self.options,
            stdout_callback=results_callback,
            passwords=self.options.passwords,
        )

        try:
            tqm.run(play)
            return results_callback
        except Exception as e:
            raise AnsibleError(e)
        finally:
            tqm.cleanup()
            self.loader.cleanup_all_tmp_files()
Exemplo n.º 39
0
    def run(self, playbook):
        pb = Playbook.load(playbook, variable_manager=self.variable_manager, loader=self.loader)
        # only support one playbook.yml
        play = pb.get_plays()[0]

        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords={'conn_pass': None, 'become_pass': None},
                stdout_callback=self.callback,
            )
            return tqm.run(play), self.callback.result
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 40
0
    def run(self, module_name, module_args, extra_vars={}):
        """ 
        run module from andible ad-hoc. 
        :arg string module_name: ansible module_name 
        :arg string module_args: ansible module args 
        """
        run_host_list = [x.name for x in self.inventory.get_hosts()]
        # create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
        play_source = dict(
            name="Ansible Play",
            hosts=run_host_list,
            gather_facts='no',
            tasks=[
                dict(action=dict(module=module_name, args=module_args)),
            ])
        self.variable_manager.extra_vars = extra_vars
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.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
        #self.callback = ResultCallback()
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback=self.
                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)
Exemplo n.º 41
0
    def run(self,
            tasks,
            pattern,
            play_name='Ansible Ad-hoc',
            gather_facts='no'):
        """
        :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ]
        :param pattern: all, *, or others
        :param play_name: The play name
        :param gather_facts:
        :return:
        """
        self.check_pattern(pattern)
        self.results_callback = self.get_result_callback()
        cleaned_tasks = self.clean_tasks(tasks)
        context.CLIARGS = ImmutableDict(self.options)

        play_source = dict(name=play_name,
                           hosts=pattern,
                           gather_facts=gather_facts,
                           tasks=cleaned_tasks)

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

        tqm = TaskQueueManager(
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            stdout_callback=self.results_callback,
            passwords={"conn_pass": self.options.get("password", "")})
        try:
            tqm.run(play)
            return self.results_callback
        except Exception as e:
            raise AnsibleError(e)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 42
0
    def run_model(self, host_list, module_name, module_args):
        """
        ansible group1 -m shell -a 'ls /tmp'

        """
        print(" in  run model")
        self.callback = ADhocCallback()
        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 = TaskQueueManager(
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=self.options,
            passwords=self.passwords,
            stdout_callback="minimal",
        )
        print(tqm)
        print(dir(play))
        print(play.get_tasks())
        print(play.get_name())
        print(play.get_vars())
        print(play.get_roles())

        try:
            tqm._stdout_callback = self.callback
            constants.HOST_KEY_CHECKING = False  # 关闭第一次使用ansible连接客户端是输入命令
            tqm.run(play)
            self.results_raw = self.callback.result_row
            print("[2] %s" % self.results_raw)  # debug 2
        except Exception as err:
            print(err)
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 43
0
    def test_strategy_base_queue_task(self, mock_worker):
        def fake_run(self):
            return

        mock_worker.run.side_effect = fake_run

        fake_loader = DictDataLoader()
        mock_var_manager = MagicMock()
        mock_host = MagicMock()
        mock_host.get_vars.return_value = dict()
        mock_host.has_hostkey = True
        mock_inventory = MagicMock()
        mock_inventory.get.return_value = mock_host
        mock_options = MagicMock()
        mock_options.module_path = None

        tqm = TaskQueueManager(
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=None,
        )
        tqm._initialize_processes(3)
        tqm.hostvars = dict()

        mock_task = MagicMock()
        mock_task._uuid = 'abcd'

        try:
            strategy_base = StrategyBase(tqm=tqm)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 1)
            self.assertEqual(strategy_base._pending_results, 1)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 2)
            self.assertEqual(strategy_base._pending_results, 2)
            strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
            self.assertEqual(strategy_base._cur_worker, 0)
            self.assertEqual(strategy_base._pending_results, 3)
        finally:
            tqm.cleanup()
Exemplo n.º 44
0
 def run_model(self, host_list, module_name, module_args):
     """
     run module from ansible ad-hoc.
     :param host_list:
     :param module_name: ansible module name
     :param module_args: ansible module args
     :return:
     """
     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 or self.logId:
         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
         constants.HOST_KEY_CHECKING = False
         tqm.run(play)
     except Exception as err:
         logger.error(msg="run model failed: {err}".format(err=str(err)))
         if self.redisKey:
             DsRedis.OpsAnsibleModel.lpush(self.redisKey, data=err)
         if self.logId:
             AnsibleSaveResult.Model.insert(self.logId, err)
     finally:
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 45
0
    def module_run(self, task_lst):
        """
        task_lst is a list for dict, Just like :
        [
            {
                'module': 'your_self_module',
                'args': 'args=sssss'
            },
            {
                'module': 'shell',
                'args': 'ifconfig'
            }
        ]
        :param task_lst:
        :return None:
        """
        tasks = _parse_task(task_lst)
        play_source = dict(name="Ansible Play",
                           hosts=self.hosts,
                           gather_facts='no',
                           tasks=tasks)

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

        # actually 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=self.result_callback,
                # Use our custom callback instead of the ``default`` callback plugin
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 46
0
def order_run(host, action):
    """
    run an ansible order
    :param host: string
    :param action: dict
    :return: return an object ResultCallback
    """
    variable_manager = VariableManager()
    loader = DataLoader()
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list='/etc/ansible/hosts')
    passwords = {}

    play_source = dict(name="Ansible Play",
                       hosts=host,
                       gather_facts='no',
                       tasks=[dict(action=action)])
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    tqm = None
    callback = ResultCallback()
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=callback,
            run_additional_callbacks=constants.DEFAULT_LOAD_CALLBACK_PLUGINS,
            run_tree=False,
        )

        tqm.run(play)

    finally:
        if tqm is not None:
            tqm.cleanup()
    return callback
Exemplo n.º 47
0
    def run(self, tasks, pattern, play_name='Ansible Ad-hoc', gather_facts='no'):
        """
        :param tasks: [{'action': {'module': 'shell', 'args': 'ls'}, ...}, ]
        :param pattern: all, *, or others
        :param play_name: The play name
        :param gather_facts:
        :return:
        """
        self.check_pattern(pattern)
        self.results_callback = self.get_result_callback()
        cleaned_tasks = self.clean_tasks(tasks)
        context.CLIARGS = ImmutableDict(self.options)

        play_source = dict(
            name=play_name,
            hosts=pattern,
            gather_facts=gather_facts,
            tasks=cleaned_tasks
        )

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

        tqm = TaskQueueManager(
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            stdout_callback=self.results_callback,
            passwords={"conn_pass": self.options.get("password", "")}
        )
        try:
            tqm.run(play)
            return self.results_callback
        except Exception as e:
            raise AnsibleError(e)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 48
0
    def run(self, args, gether_facts="no", module="shell", task_time=0):
        """
        参数说明:
        task_time -- 执行异步任务时等待的秒数,这个需要大于 0 ,等于 0 的时候不支持异步(默认值)。这个值应该等于执行任务实际耗时时间为好
        """
        if self.iswin:
            module = 'raw'

        play_source = dict(
            name="Ad-hoc",
            hosts=self.host_lists,
            gather_facts=gether_facts,
            tasks=[
                # 这里每个 task 就是这个列表中的一个元素,格式是嵌套的字典
                # 也可以作为参数传递过来,这里就简单化了。
                {
                    "action": {
                        "module": module,
                        "args": args
                    },
                    "async": task_time,
                    "poll": 0
                }
            ])

        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,
                                   passwords=self.passwords,
                                   stdout_callback=self.results_callback)

            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 49
0
 def run_module(self, module_name, module_args, hosts='all'):
     """
     运行 module
     """
     play_source = dict(
         name='Ansible Run Module',
         hosts=hosts,
         gather_facts='no',
         tasks=[
             {'action': {'module': module_name, 'args': module_args}},
         ]
     )
     play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.dynamic_inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             passwords=self.passwords,
             stdout_callback=self.results_callback,
         )
         tqm.run(play)
         # self.result_row = self.results_callback.result_row
     except Exception:
         print(traceback.format_exc())
     finally:
         # try:
         #     message = dict()
         #     message['status'] = 0
         #     message['message'] = '执行关闭'
         #     message = json.dumps(message)
         #     async_to_sync(channel_layer.group_send)(self.group, {
         #         "type": "send.message",
         #         "text": message,
         #     })
         # except:
         #     pass
         if tqm is not None:
             tqm.cleanup()
         # 这个临时目录会在 ~/.ansible/tmp/ 目录下
         shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Exemplo n.º 50
0
def run_adhoc(host_list, module_name, module_args):
    loader = DataLoader()  # 用来加载解析yaml文件或JSON内容,并且支持vault的解密
    variable_manager = VariableManager(
    )  # 管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory 中的
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=host_list)
    variable_manager.set_inventory(inventory)  # 根据 inventory 加载对应变量
    # 构建pb, 这里很有意思, 新版本运行ad-hoc或playbook都需要构建这样的pb, 只是最后调用play的类不一样
    # :param name: 任务名,类似playbook中tasks中的name
    # :param hosts: playbook中的hosts
    # :param tasks: playbook中的tasks, 其实这就是playbook的语法, 因为tasks的值是个列表,因此可以写入多个task
    play_source = {
        "name": "Ansible Ad-Hoc",
        "hosts": "pool-ip",
        "gather_facts": "no",
        "tasks": [{
            "action": {
                "module": module_name,
                "args": module_args
            }
        }]
    }
    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=None,
            stdout_callback='minimal',
            run_tree=False,
        )
        result = tqm.run(play)
        return result
    finally:
        if tqm is not None:
            tqm.cleanup()
Exemplo n.º 51
0
    def shell_run(self):
        play = Play().load(self.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=results_callback,
            )
            tqm.run(play)

        finally:
            if tqm is not None:
                tqm.cleanup()
            return results_callback.host_ok
Exemplo n.º 52
0
    def execute_play(self, play_source):
        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={},
                stdout_callback=self.default_results_callback)
            result = tqm.run(play)

            print('executed playbook, terminated with code %d.' % result)

        finally:
            if tqm is not None:
                tqm.cleanup()
Exemplo n.º 53
0
 def run(self, callback=None):
     self.variable_manager.set_inventory(self.inventory)
     play = Play().load(self.create_play_tasks(),
                        variable_manager=self.variable_manager,
                        loader=self.loader)
     tqm = None
     if not callback:
         callback = ResultsCollector()
     try:
         tqm = TaskQueueManager(inventory=self.inventory,
                                variable_manager=self.variable_manager,
                                loader=self.loader,
                                options=self.options,
                                passwords=self.passwords,
                                stdout_callback=callback)
         result = tqm.run(play)
     finally:
         if tqm is not None:
             tqm.cleanup()
     return result, callback
Exemplo n.º 54
0
 def run(self):
     tqm = None
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords={},
             stdout_callback=self.stdout_callback
         )
         for p in self.play:
             result = tqm.run(p)
         self.delete_key()
         self.push_mission.status = settings.OPS_PUSH_MISSION_SUCCESS
         self.consumer.send("OK")
     finally:
         self.consumer.close()
         if tqm is not None:
             tqm.cleanup()
Exemplo n.º 55
0
def exec_ansible(module,args,host):
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
    # initialize needed objects
    loader = DataLoader()
    options = Options(connection='ssh', module_path='/usr/local/python3/lib/python3.6/site-packages/', forks=100, become=None, become_method=None, become_user=None, check=False,diff=False)
    passwords = dict(vault_pass='******')

    results_callback = ResultCallback()
    # create inventory and pass to var manager
    inventory = InventoryManager(loader=loader, sources=['/tmp/hosts'])
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # create play with tasks
    play_source =  dict(
            name = "Ansible Play",
            hosts = host,
            gather_facts = 'no',
            tasks = [
                dict(action=dict(module=module, args=args), register='shell_out'),
             ]
        )
    play = Play().load(play_source, variable_manager=variable_manager, loader=loader)

    stdout_callback = results_callback
    tqm = None
    global exec_result
    try:
        tqm = TaskQueueManager(
                  inventory=inventory,
                  variable_manager=variable_manager,
                  loader=loader,
                  options=options,
                  passwords=passwords,
                 stdout_callback=results_callback,
              )
        result = tqm.run(play)

    finally:
        if tqm is not None:
            tqm.cleanup()
        return    exec_result
Exemplo n.º 56
0
    def run(self, hosts, module, args):
        play_source = dict(
            name="ansible run",
            hosts=hosts,
            gather_facts='no',
            tasks=[
                dict(
                    action=dict(
                        module=module,
                        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)

        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,
            )
            result = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()
        try:
            return(info)
        except:
            pass
Exemplo n.º 57
0
 def _execute_play(play_source, inventory, var_mgr, loader, options, callback):  # pylint: disable=too-many-arguments
   """
   Execute the playbook
   """
   play = Play().load(play_source, variable_manager=var_mgr, loader=loader)
   tqm = None
   try:
     tqm = TaskQueueManager(
         inventory=inventory,
         variable_manager=var_mgr,
         loader=loader,
         options=options,
         passwords=None,
         stdout_callback=callback,
         )
     _ = tqm.run(play)
   except Exception as exc:
     raise TaskExecutionException(str(exc))
   finally:
     if tqm is not None:
       tqm.cleanup()
Exemplo n.º 58
0
 def run(self, callback=None):
     self.variable_manager.set_inventory(self.inventory)
     play = Play().load(self.create_play_tasks(),
                        variable_manager=self.variable_manager, loader=self.loader)
     tqm = None
     if not callback:
         callback = ResultsCollector()
     try:
         tqm = TaskQueueManager(
             inventory=self.inventory,
             variable_manager=self.variable_manager,
             loader=self.loader,
             options=self.options,
             passwords=self.passwords,
             stdout_callback=callback
         )
         result = tqm.run(play)
     finally:
         if tqm is not None:
             tqm.cleanup()
     return result, callback
Exemplo n.º 59
0
 def ansible_play(self):
     # 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=self.results_callback,
             # stdout_callback='default',
         )
         result = tqm.run(self.play)
     finally:
         if tqm is not None:
             tqm.cleanup()
             #self.inventory.clear_pattern_cache()
         if result != 0:
             raise ValueError('SSH Command Failed, resturn: %s' % result)
         return self.return_results
Exemplo n.º 60
0
    def run_play(self, host_list, module_name, module_args):
        """
        run play
        :param host_list is list,
        :param module_name is string
        :param module_args is string
        """
        play = None
        # create play with tasks
        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)
        # actually run it
        tqm = None
        display = LogDisplay(logname=self.job_id)
        callback = CALLBACKMODULE[CALLBACK](display=display)
        if module_name == 'backup':
            # 对于备份模块,特殊处理,必须使用minimal,才能获取到文件名属性
            callback = minimal_callback(display=display)

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

        return display.get_log_json()