Ansible methods used as a Python module library

Source: Internet
Author: User
Ansible is a python package, is a complete unpack and play software, the only requirement for clients is that SSH has python, and python-simplejson packages, deployment simple to heinous. The following article will give you the main introduction of Ansible as a Python module library use of the method example, the need for friends can reference.

Objective

Ansible is a new automated operation tools, based on Python development, the collection of a number of operations tools (puppet, Cfengine, Chef, func, fabric) The advantages of the implementation of batch system configuration, batch program deployment, batch Run command and other functions. Ansible is a module-based operation and does not have the capacity to deploy in bulk. The real batch deployment is the module that Ansible runs, and Ansible just provides a framework.

Mainly include:

(1), connection plug-in connection plugins: responsible for and be monitored to achieve communication;

(2), host Inventory: Specifies the operation of the host, is a configuration file inside the definition of monitoring host;

(3), various modules core module, command module, custom module;

(4), with the help of the plug-in to complete log mail and other functions;

(5), playbook: When a script performs multiple tasks, it is not necessary to allow the node to run multiple tasks at once.

Asible is a very good tool in the operation and maintenance tools, I personally prefer, can be flexibly configured according to the needs of yml files to achieve different business needs, because there is no need to install the client, get started or very easy, In some cases you may need to write ansible as a library component of Python into your own script, and today's script script will show how ansible is combined with a Python script, that is, how to use ansible in a Python script.

Let's take a look at the first example:

#!/usr/bin/python import ansible.runnerimport ansible.playbookimport ansible.inventoryfrom ansible Import Callbacksfrom ansible Import utilsimport JSON # The fastest to set up the inventory # hosts listhosts = ["10.11.12.66" ]# set up the inventory, if no group was defined then ' all ' group was used by Defaultexample_inventory = Ansible.inventory.i Nventory (hosts) PM = Ansible.runner.Runner (module_name = ' Command ', Module_args = ' uname-a ', timeout = 5, inventory = EX Ample_inventory, subset = ' All ' # Name of the "the hosts group"  ) out = Pm.run () print json.dumps (out, Sort_keys=true, Inden t=4, separators= (', ', ': '))

This example shows how we run the system commands in a Python script using ansible, and then we look at the second example, docking with our Yml file.

The simple yml file contents are as follows:

-Hosts:sample_group_name tasks:-Name:just an uname command:uname-a

The Python script that calls Playbook is as follows:

#!/usr/bin/python import ansible.runnerimport ansible.playbookimport ansible.inventoryfrom ansible Import Callbacksfrom ansible Import utilsimport JSON # # setting up the inventory # First of all, set up a host (or more) example _host = ansible.inventory.host.Host (name = ' 10.11.12.66 ', port = $) # with its variables to modify the Playbookexample_h Ost.set_variable (' var ', ' foo ') # # Secondly set up the group where the host (s) have to is Addedexample_group = Ansible.inve Ntory.group.Group (name = ' Sample_group_name ') Example_group.add_host (Example_host) # # # The last step was set up the Invetor Y itselfexample_inventory = ansible.inventory.Inventory () example_inventory.add_group (Example_group) Example_ Inventory.subset (' Sample_group_name ') # setting Callbacksstats = callbacks. Aggregatestats () PLAYBOOK_CB = callbacks. Playbookcallbacks (verbose=utils. verbosity) RUNNER_CB = callbacks. Playbookrunnercallbacks (stats, verbose=utils. verbosity) # Creating the Playbook instance to run, based on "Test.yml" filEPB = Ansible.playbook.PlayBook (playbook = "Test.yml", stats = stats, callbacks = PLAYBOOK_CB, runner_callbacks = Runner_ CB, Inventory = example_inventory, check=true) # running the PLAYBOOKPR = Pb.run () # Print the summary of results for EA CH Hostprint json.dumps (pr, sort_keys=true, indent=4, separators= (', ', ': '))

Related Article

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.