Ansible Test for API 2.0

Source: Internet
Author: User
Tags dataloader

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.