Ansible python API v2.9.4 , Ansible v2.9.4
API官方案例 import jsonimport shutilfrom ansible.module_utils.common.collections import ImmutableDictfrom ansible.parsing.dataloader import DataLoaderfrom ansible.vars .manager import VariableManagerfrom ansible.inventory.manager import InventoryManagerfrom ansible.playbook.play import Playfrom ansible.executor.task_queue_manager import TaskQueueManagerfrom ansible.plugins.callback import CallbackBasefrom ansible import contextimport ansible.constants as Cclass ResultCallback (CallbackBase ): def v2_runner_on_ok (self, result, **kwargs ): host = result._host print (json.dumps({host.name: result._result}, indent=4 )) context.CLIARGS = ImmutableDict(connection='local' , module_path=['/to/mymodules' ], forks=10 , become=None , become_method=None , become_user=None , check=False , diff=False ) loader = DataLoader() passwords = dict (vault_pass='secret' ) results_callback = ResultCallback() inventory = InventoryManager(loader=loader, sources='localhost,' ) variable_manager = VariableManager(loader=loader, inventory=inventory) 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}}' ))) ] ) 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() shutil.rmtree(C.DEFAULT_LOCAL_TMP, True )
API详解 CallbackBase CallbackBase
是需要重写的结果返回类。类的定义位于ansible.plugins.plugins.callback
的__init__.py
中。 因为ansible版本是>2的,所以可以重写的方法开头是带有v2
的。一些可重写的方法:
v2_runner_on_failed
:当执行失败后执行的方法
v2_runner_on_ok
:当执行成功后执行的方法
v2_runner_on_unreachable
:当出现无法连接的情况执行的方法
参考文献
Ansible 官方文档