Home > Backend Development > Python Tutorial > Examples of methods used by ansible as a python module library

Examples of methods used by ansible as a python module library

高洛峰
Release: 2017-02-21 10:29:43
Original
1902 people have browsed it

ansible is a python package, a complete unpack and play software. The only requirement for the client is to have ssh and python, and the python-simplejson package is installed. The deployment is ridiculously simple. The following article mainly introduces examples of methods for using ansible as a python module library. Friends in need can refer to it.

Preface

ansible is a new automated operation and maintenance tool. It is developed based on Python and integrates many operation and maintenance tools (puppet, cfengine, chef, func, fabric), it realizes functions such as batch system configuration, batch program deployment, and batch running of commands. Ansible works based on modules and does not have the ability to deploy in batches. What really has batch deployment is the module run by ansible, and ansible only provides a framework.

Mainly includes:

(1) Connection plugins: responsible for communicating with the monitored end;

(2) Host inventory : The specified operating host is the monitoring host defined in a configuration file;

(3) Various modules core modules, command modules, custom modules;

           (4) Use plug-ins to complete functions such as recording log emails;

             (5) Playbook: When the script performs multiple tasks, the node can run multiple tasks at one time if not necessary.

Ansible is a very good tool among operation and maintenance tools. I personally like it. The yml file can be flexibly configured according to the needs to achieve different business needs. Because there is no need to install a client, it is very easy to get started. In some cases, you may need to write ansible as a library component of python into your own script. Today's script will show how ansible is combined with a python script, that is, how to use ansible in a python script. We Unfold gradually.

Let’s look at the first example:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
 
# the fastest way to set up the inventory
 
# hosts list
hosts = ["10.11.12.66"]
# set up the inventory, if no group is defined then 'all' group is used by default
example_inventory = ansible.inventory.Inventory(hosts)
 
pm = ansible.runner.Runner(
 module_name = 'command',
 module_args = 'uname -a',
 timeout = 5,
 inventory = example_inventory,
 subset = 'all' # name of the hosts group 
 )
 
out = pm.run()
 
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))
Copy after login

This example shows how we How to run system commands through ansible in a python script. Let's look at the second example and connect it to our yml file.

The content of the simple yml file is as follows:

- hosts: sample_group_name
 tasks:
 - name: just an uname
 command: uname -a
Copy after login

The python script that calls the playbook is as follows:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import 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 = 22
 )
# with its variables to modify the playbook
example_host.set_variable( 'var', 'foo')
 
## secondly set up the group where the host(s) has to be added
example_group = ansible.inventory.group.Group(
 name = 'sample_group_name'
 )
example_group.add_host(example_host)
 
## the last step is set up the invetory itself
example_inventory = ansible.inventory.Inventory()
example_inventory.add_group(example_group)
example_inventory.subset('sample_group_name')
 
# setting callbacks
stats = 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" file
pb = ansible.playbook.PlayBook(
 playbook = "test.yml",
 stats = stats,
 callbacks = playbook_cb,
 runner_callbacks = runner_cb,
 inventory = example_inventory,
 check=True
 )
 
# running the playbook
pr = pb.run() 
 
# print the summary of results for each host
print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
Copy after login

For more examples of methods using ansible as a python module library, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template