随笔:ansiable python api
这个是官方提供的例子:https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html
中文解析可以参考:https://yangjinheng.github.io/2017/03/20/Linux/Ansible/
从例子主要关注:
1.host_list=['localhost','www.example.com','www.google.com'],这里相当于获取了IP然后去跑ansiable程序
2.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'))),]
这里相当于playbook里面写的程序
如果我们要写一个删除文件的例子,可以用find模块和file模块可以这样写:find去匹配tmp路径下超过5天的log文件,然后利用item模块和file模块配合删除匹配到的所有.log文件
dict(action=dict(module='find', args=dict(path='/tmp/',patterns='*.log',age='5d')), register='shell_out')
dict(action=dict(module='file', args=dict(path='{{ item.path }}',state='absent')),with_items='{{ shell_out.files }}', register='shell_ou')
通过这1,2点利用官方的例子可以自己写一个ansiable程序,就相当于ansiable的playbook
#!/usr/bin/env pythonfrom__future__import(absolute_import,division,print_function)__metaclass__=typeimportjsonimportshutilimportansible.constantsasCfromansible.executor.task_queue_managerimportTaskQueueManagerfromansible.module_utils.common.collectionsimportImmutableDictfromansible.inventory.managerimportInventoryManagerfromansible.parsing.dataloaderimportDataLoaderfromansible.playbook.playimportPlayfromansible.plugins.callbackimportCallbackBasefromansible.vars.managerimportVariableManagerfromansibleimportcontext# Create a callback plugin so we can capture the outputclassResultsCollectorJSONCallback(CallbackBase):"""A sample callback plugin used for performing an action as results come in. If you want to collect all results into a single object for processing at the end of the execution, look into utilizing the ``json`` callback plugin or writing your own custom callback plugin. """def__init__(self,*args,**kwargs):super(ResultsCollectorJSONCallback,self).__init__(*args,**kwargs)self.host_ok={}self.host_unreachable={}self.host_failed={}defv2_runner_on_unreachable(self,result):host=result._hostself.host_unreachable[host.get_name()]=resultdefv2_runner_on_ok(self,result,*args,**kwargs):"""Print a json representation of the result. Also, store the result in an instance attribute for retrieval later """host=result._hostself.host_ok[host.get_name()]=resultprint(json.dumps({host.name:result._result},indent=4))defv2_runner_on_failed(self,result,*args,**kwargs):host=result._hostself.host_failed[host.get_name()]=resultdefmain():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 objectcontext.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#L204sources=','.join(host_list)iflen(host_list)==1:sources+=','# initialize needed objectsloader=DataLoader()# Takes care of finding and reading yaml, json and ini filespasswords=dict(vault_pass='secret')# Instantiate our ResultsCollectorJSONCallback for handling results as they come in. Ansible expects this to be one of its main display outletsresults_callback=ResultsCollectorJSONCallback()# create inventory, use path to host config file as source or hosts in a comma separated stringinventory=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 contextvariable_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_sourceplay=Play().load(play_source,variable_manager=variable_manager,loader=loader)# Actually run ittry:result=tqm.run(play)# most interesting data for a play is actually sent to the callback's methodsfinally:# we always need to cleanup child procs and the structures we use to communicate with themtqm.cleanup()ifloader:loader.cleanup_all_tmp_files()# Remove ansible tmpdirshutil.rmtree(C.DEFAULT_LOCAL_TMP,True)print("UP ***********")forhost,resultinresults_callback.host_ok.items():print('{0} >>> {1}'.format(host,result._result['stdout']))print("FAILED *******")forhost,resultinresults_callback.host_failed.items():print('{0} >>> {1}'.format(host,result._result['msg']))print("DOWN *********")forhost,resultinresults_callback.host_unreachable.items():print('{0} >>> {1}'.format(host,result._result['msg']))if__name__=='__main__':main()
from __future__ import (absolute_import, division, print_function) __metaclass__ = type import json import shutil import ansible.constants as C from ansible.executor.task_queue_manager import TaskQueueManager from ansible.module_utils.common.collections import ImmutableDict from ansible.inventory.manager import InventoryManager from ansible.parsing.dataloader import DataLoader from ansible.playbook.play import Play from ansible.plugins.callback import CallbackBase from ansible.vars.manager import VariableManager from ansible import context # Create a callback plugin so we can capture the output class ResultsCollectorJSONCallback(CallbackBase): """A sample callback plugin used for performing an action as results come in. If you want to collect all results into a single object for processing at the end of the execution, look into utilizing the ``json`` callback plugin or writing your own custom callback plugin. """ def __init__(self, *args, **kwargs): super(ResultsCollectorJSONCallback, self).__init__(*args, **kwargs) self.host_ok = {} self.host_unreachable = {} self.host_failed = {} def v2_runner_on_unreachable(self, result): host = result._host self.host_unreachable[host.get_name()] = result def v2_runner_on_ok(self, result, *args, **kwargs): """Print a json representation of the result. Also, store the result in an instance attribute for retrieval later """ host = result._host self.host_ok[host.get_name()] = result print(json.dumps({host.name: result._result}, indent=4)) def v2_runner_on_failed(self, result, *args, **kwargs): host = result._host self.host_failed[host.get_name()] = result 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='secret') # 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'])) if __name__ == '__main__': main()