Ansible Playbook API Development Call test

Source: Internet
Author: User
Tags ansible playbook

Ansible is a lightweight batch configuration management tool for agentless, because of the relatively late (13) development based on ansible less relevant documents, so here are some small experiments, combined with the existing data and source code, explore the Ansible two times development.

The contents of the essay are divided into three parts.

    1. Playbook Edit Execution
    2. Python invokes API execution playbook
    3. Java calls the Python program for playbook execution

The experimental environment is centos6,ansible version is 1.9.4,python version is 2.6.6,JDK version is 1.7u79

First, playbook Edit execution

To edit playbook, the function here is to copy a script to the specified node and execute it, noting the format of Yaml, Note Indent

Webauth.yml

1 ---2 3 -Name:hostname4 5 hosts: "{{host}}"6 7 Sudo:yes8 9 tasks:Ten  One -name:copy file to "{{host}}" A  - copy:src=/tmp/webauth.sh dest=/tmp/webauth.sh mode=0775 -  the     -  - -Name:execute WebAuth -  +Script:/tmp/webauth.sh

If you want to run the playbook, you can use

# ansible-playbook/opt/ansible/webauth.yml--extra-vars ' host= ' nodes '

The results will be returned at this time.

PLAY [hostname] ***************************************************************Gathering FACTS***************************************************************OK: [192.168.10.110] TASK: [CopyfileTo"{{host}}"] ***********************************************OK: [192.168.10.110] TASK: [Execute WebAuth]*******************************************************changed: [192.168.10.110] PLAY RECAP********************************************************************192.168.10.110: ok=3Changed=1unreachable=0Failed=0

The ability to perform a successful description of playbook is no problem, if reported wrong, then follow the prompts to modify.

Second, Python invokes API execution playbook

The following uses Python to invoke API interface execution above playbook

1 Python code: test.py2 ImportSYS3 ImportAnsible.playbook4  fromAnsibleImportCallbacks5  fromAnsibleImportUtils6 7Stats =callbacks. Aggregatestats ()8PLAYBOOK_CB = callbacks. Playbookcallbacks (verbose=Utils. verbosity)9RUNNER_CB = callbacks. Playbookrunnercallbacks (stats, verbose=Utils. verbosity)Ten defExecute (play,params): OnePB =Ansible.playbook.PlayBook ( Aplaybook=Play, -stats=stats, -callbacks=PLAYBOOK_CB, therunner_callbacks=RUNNER_CB, -check=False, -extra_vars=eval (params) -   ) +   PrintPB -   returnPb.run () + if __name__=='__main__': ARes=execute (sys.argv[1],sys.argv[2]) at   PrintRes

The Playbook=play in the code is given by the command line the Playbook script path that needs to be executed, stats is the process state used to collect execution, PLAYBOOK_CB is the result of PLAYBOOK_CB to output execution, Runner_ The callbacks is used to output the results of the playbook execution period.

Perform

' {"host": "Cloudeploy-nodes"} '

return Result:

PLAY [hostname] ***************************************************************Gathering FACTS***************************************************************OK: [192.168.10.110] TASK: [CopyfileTo"{{host}}"] ***********************************************changed: [192.168.10.110] TASK: [Execute WebAuth]*******************************************************OK: [192.168.10.110]{'192.168.10.110': {'Unreachable':0,'skipped':0,'OK':2,'changed':1,'Failures':0}}

Third, Java calls Python program for playbook execution

Use Java to invoke Python for Java execution playbook

Test.java

1 ImportJava.io.InputStreamReader;2 3 ImportJava.io.BufferedReader;4 5 ImportJava.io.InputStream;6 7 Importjava.io.IOException;8 9  Public classtest{Ten  One    Public Static voidMain (String args[])throwsioexception{ A  -String command= "python/opt/ansible/test.py/opt/ansible/hostname.yml {' host ': '" +args[0]+ "'}"; -  theProcess Process =runtime.getruntime (). exec (command); -  - PrintStream (Process.getinputstream ()); -  + PrintStream (Process.geterrorstream ()); -  +System.out.println ("Finish"); A  at   } -  - Private Static voidPrintStream (InputStream inputstream) { -  -   if(InputStream = =NULL){ -  inSYSTEM.OUT.PRINTLN ("Input null"); -  to     return; +  -   } the  *String line = ""; $ Panax Notoginseng     Try(BufferedReader input =NewBufferedReader (NewInputStreamReader (InputStream))) -  the     { +  A        while(line = Input.readline ())! =NULL) { the  + System.out.println (line); -  $       } $  -}Catch(IOException E1) { -  theSystem.out.println ("output stream failed" +E1); - Wuyi e1.printstacktrace (); the  -     } Wu  -   } About  $}

Call

#java Test Cloudeploy

The path to the playbook is fixed and can be passed in as a parameter.

Execution Result:

PLAY [hostname] ***************************************************************Gathering FACTS***************************************************************OK: [192.168.10.110]task: [Returnhostname] *******************************************************changed: [192.168.10.110]task: [CopyfileTo"{{host}}"] ***********************************************OK: [192.168.10.110]task: [Execute WebAuth]*******************************************************changed: [192.168.10.110]{'192.168.10.110': {'Unreachable':0,'skipped':0,'OK':4,'changed':2,'Failures':0}}finish

Indicates successful execution, viewing the corresponding node, the file will be copied and executed

Resources:

Github:playbook Source: https://github.com/ansible/ansible/blob/release1.8.4/lib/ansible/playbook/__init__.py

Kisips:ansible Document Playbook Advanced: http://www.kisops.com/?p=42

361way:ansible Section 10 ansible API http://www.361way.com/ansible-api/4446.html

Stackoverflow:running Ansible-playbook using Python API http://stackoverflow.com/questions/27590039/ Running-ansible-playbook-using-python-api

Ansible:developing API http://docs.ansible.com/ansible/developing_api.html

Google:ansible Forum Https://groups.google.com/forum/#!topic/ansible-project/V1PoNJcXV_w

Ansible Playbook API Development Call test

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.