Because the project needs to use to the Ansible API, according to modify the official documents provided by the use of examples, after many tests, will be able to use the code to share to everyone, you just need to modify the code according to their own actual environment.
Official Document: http://docs.ansible.com/ansible/latest/dev_guide/developing_api.html#python-api-2-0
#coding:utf-8import jsonfrom collections import namedtuplefrom 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 callbackbaseclass resultcallback ( Callbackbase): def v2_runner_on_ok (Self, result, **kwargs): host = result._host print (Json.dumps ({host.name: result._result}, indent=4)) # initializes the desired object options = Namedtuple ' Options ', [' connection ', ' module_path ', ' forks ', ' become ', ' become_ Method ', ' become_user ', ' Check ', ' diff ') # module_path parameter refers toThe path to the Ansible module package Loader = dataloader () options = options (connection= ' smart ', module_ Path= '/usr/lib/python2.7/dist-packages/ansible/modules ', forks=5, become=none, become_method= None, become_user= "root", check=false, diff=false) passwords = dict (vault_pass= ' Secret ') # instantiate resultcallback to process results results_callback = resultcallback () # Create inventory (inventory) and passed to Variablemanagerinventory = inventorymanager (loader=loader, sources=['. /conf/hosts ']) #. /conf/hosts is defined Hostsvariable_manager = variablemanager (loader=loader, inventory=inventory) # Create Task Play_source = dict ( name = "Ansible play", hosts = "Cephnode", gather_facts = ' No ', tasks = [ &Nbsp; dict (action=dict (module= ' shell ', args= ' Touch /tmp/7.txt '), register= ' shell_out '), #定义一条任务, if more than one task should be defined in such a way ] ) Play = play (). Load (play_source, Variable_manager=variable_manager, loader=loader) # Start execution tqm = nonetry: tqm = taskqueuemanager ( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, stdout_callback=results_callback, # use a custom callback instead of the "default" callback plug-in (if the Stdout_callback parameter is not required to output by default) ) result = tqm.run (play) finally: if tqm is not none: tqm.cleanup ()
It's mine.. The contents of the/conf/hosts file are as follows:
[Cephnode]
192.168.89.136
Attention:
If inventory is not explicitly specified (the following parameters), the hosts will be read from the/etc/ansible/hosts by default
Sources=['. /conf/hosts ']
To add, just say a way to define multiple tasks, for example:
tasks = [Dict (action=dict (module= ' shell ', args= ' Mkdir/tmp/toby '), register= ' Shell_out '), #首先创建目录 D ICT (action=dict (module= ' copy ', args= ' src=/tmp/abc123.txt dest=/tmp/toby '), register= ' Shell_out ') # The local abc123.txt is then sent through the copy module to the target host's/tmp/toby/directory]
Ansible Test for API 2.0