ansible is an instance of a method used by the python module library

  • 2020-05-24 05:48:22
  • OfStack

preface

ansible is a newly emerged automated operation and maintenance tool. Based on the development of Python, it integrates the advantages of many operation and maintenance tools (puppet, cfengine, chef, func, fabric), and realizes the functions of batch system configuration, batch program deployment, and batch operation command. ansible works on a modular basis and does not have the ability to deploy in bulk. What really has batch deployment is the module that ansible is running, and ansible only provides one framework.

It mainly includes:

(1) connection plug-in connection plugins: responsible for communication with the monitored terminal;

(2) host inventory: the host of the specified operation, which is the host of the monitoring defined in a configuration file;

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

(4) logging mail and other functions with the help of plug-ins;

(5) playbook: when the script performs multiple tasks, it is not necessary to let the node run multiple tasks once.

Asible is operational tool is very good, personally, I prefer, can be configured according to requirements yml files to implement different business needs, because they do not need to install the client, it is very easy, and in some cases you may need to be ansible as python 1 library components written to your script, the script script today will show you how the next ansible with python script, which is how to use ansible python scripts, we gradually expanded.

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=(',', ': '))

This example shows how we run the python script and how we run the system commands through ansible. Let's take a look at the second example, docking with our yml file.

The simple yml file reads as follows:


- hosts: sample_group_name
 tasks:
 - name: just an uname
 command: uname -a

The python script to invoke 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=(',', ': '))

conclusion

The above is for you to show the two small examples hope that the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: