Ansible is a pythonpackage and is a complete unpackandplay software. The only requirement on the client is that ssh has python and python is installed with the python-simplejson package, which is easy to deploy to the terminal. This article will introduce ansible as a method example for python module library. you can refer to it for reference. Ansible is a python package and is a complete unpack and play software. The only requirement on the client is that ssh has python and python is installed with the python-simplejson package, which is easy to deploy in container. This article will introduce ansible as a method example for python module library. you can refer to it for reference.
Preface
Ansible is a new automatic O & M tool developed based on Python. it integrates the advantages of many O & M tools (puppet, cfengine, chef, func, fabric, implements batch system configuration, batch program deployment, and batch running commands. Ansible works based on modules and does not support batch deployment. The modules run by ansible are actually deployed in batches. ansible only provides a framework.
It mainly includes:
(1) connection plugins: communicates with the monitored end;
(2) host inventory: specifies the host for the operation. it is a host defined in the configuration file;
(3) core modules, command modules, and custom modules of various modules;
(4) logging email and other functions are completed by using the plug-in;
(5) playbook: when the script executes multiple tasks, it is not necessary to allow the node to run multiple tasks at a time.
Asible is a very good tool in O & M Tools. I personally like it. you can configure yml files as needed to meet different business needs, because you do not need to install the client, it is very easy to get started. in some cases, you may need to write ansible as a python library component to your own script, today's script shows how ansible works with python scripts, that is, how to use ansible in python scripts.
First, let's look at the first example:
#!/usr/bin/python import ansible.runnerimport ansible.playbookimport ansible.inventoryfrom ansible import callbacksfrom ansible import utilsimport json # the fastest way to set up the inventory # hosts listhosts = ["10.11.12.66"]# set up the inventory, if no group is defined then 'all' group is used by defaultexample_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 to run system commands through ansible in a python script. next we will look at the second example to connect to our yml file.
The content of a simple yml file is as follows:
- hosts: sample_group_name tasks: - name: just an uname command: uname -a
The python script for calling 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 = 22 )# with its variables to modify the playbookexample_host.set_variable( 'var', 'foo') ## secondly set up the group where the host(s) has to be addedexample_group = ansible.inventory.group.Group( name = 'sample_group_name' )example_group.add_host(example_host) ## the last step is set up the invetory 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 each hostprint json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
For more information about how to use ansible as a python module library, refer to PHP!