2013-09-29 23:10:28 +00:00
|
|
|
Python API
|
|
|
|
==========
|
2012-03-31 02:28:30 +00:00
|
|
|
|
2013-12-26 19:32:01 +00:00
|
|
|
.. contents:: Topics
|
|
|
|
|
2012-08-31 03:55:31 +00:00
|
|
|
There are several interesting ways to use Ansible from an API perspective. You can use
|
2013-10-05 17:50:53 +00:00
|
|
|
the Ansible python API to control nodes, you can extend Ansible to respond to various python events, you can
|
|
|
|
write various plugins, and you can plug in inventory data from external data sources. This document
|
|
|
|
covers the Runner and Playbook API at a basic level.
|
|
|
|
|
2013-10-06 01:19:19 +00:00
|
|
|
If you are looking to use Ansible programmatically from something other than Python, trigger events asynchronously,
|
2014-01-28 16:04:34 +00:00
|
|
|
or have access control and logging demands, take a look at :doc:`tower`
|
2013-10-05 17:50:53 +00:00
|
|
|
as it has a very nice REST API that provides all of these things at a higher level.
|
|
|
|
|
|
|
|
Ansible is written in its own API so you have a considerable amount of power across the board.
|
|
|
|
This chapter discusses the Python API.
|
2012-03-31 02:28:30 +00:00
|
|
|
|
2013-10-04 14:41:44 +00:00
|
|
|
.. _python_api:
|
|
|
|
|
2012-03-31 02:28:30 +00:00
|
|
|
Python API
|
|
|
|
----------
|
2012-03-08 18:36:47 +00:00
|
|
|
|
|
|
|
The Python API is very powerful, and is how the ansible CLI and ansible-playbook
|
2012-12-12 09:55:52 +00:00
|
|
|
are implemented.
|
2012-03-08 18:36:47 +00:00
|
|
|
|
|
|
|
It's pretty simple::
|
|
|
|
|
|
|
|
import ansible.runner
|
|
|
|
|
|
|
|
runner = ansible.runner.Runner(
|
|
|
|
module_name='ping',
|
|
|
|
module_args='',
|
|
|
|
pattern='web*',
|
|
|
|
forks=10
|
|
|
|
)
|
|
|
|
datastructure = runner.run()
|
|
|
|
|
|
|
|
The run method returns results per host, grouped by whether they
|
|
|
|
could be contacted or not. Return types are module specific, as
|
2013-12-21 07:06:38 +00:00
|
|
|
expressed in the :doc:`modules` documentation.::
|
2012-03-08 18:36:47 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"dark" : {
|
|
|
|
"web1.example.com" : "failure message"
|
2013-04-11 04:43:16 +00:00
|
|
|
},
|
2012-03-08 18:36:47 +00:00
|
|
|
"contacted" : {
|
|
|
|
"web2.example.com" : 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
A module can return any type of JSON data it wants, so Ansible can
|
|
|
|
be used as a framework to rapidly build powerful applications and scripts.
|
|
|
|
|
2013-10-04 14:41:44 +00:00
|
|
|
.. _detailed_api_example:
|
|
|
|
|
2012-03-09 12:42:53 +00:00
|
|
|
Detailed API Example
|
|
|
|
````````````````````
|
|
|
|
|
|
|
|
The following script prints out the uptime information for all hosts::
|
|
|
|
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import ansible.runner
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# construct the ansible runner and execute on all hosts
|
|
|
|
results = ansible.runner.Runner(
|
|
|
|
pattern='*', forks=10,
|
2012-08-24 23:33:14 +00:00
|
|
|
module_name='command', module_args='/usr/bin/uptime',
|
2012-03-09 12:42:53 +00:00
|
|
|
).run()
|
|
|
|
|
|
|
|
if results is None:
|
|
|
|
print "No hosts found"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print "UP ***********"
|
|
|
|
for (hostname, result) in results['contacted'].items():
|
|
|
|
if not 'failed' in result:
|
|
|
|
print "%s >>> %s" % (hostname, result['stdout'])
|
|
|
|
|
|
|
|
print "FAILED *******"
|
|
|
|
for (hostname, result) in results['contacted'].items():
|
|
|
|
if 'failed' in result:
|
|
|
|
print "%s >>> %s" % (hostname, result['msg'])
|
|
|
|
|
|
|
|
print "DOWN *********"
|
|
|
|
for (hostname, result) in results['dark'].items():
|
|
|
|
print "%s >>> %s" % (hostname, result)
|
|
|
|
|
|
|
|
Advanced programmers may also wish to read the source to ansible itself, for
|
|
|
|
it uses the Runner() API (with all available options) to implement the
|
|
|
|
command line tools ``ansible`` and ``ansible-playbook``.
|
|
|
|
|
2013-10-05 16:31:16 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:doc:`developing_inventory`
|
|
|
|
Developing dynamic inventory integrations
|
|
|
|
:doc:`developing_modules`
|
|
|
|
How to develop modules
|
|
|
|
:doc:`developing_plugins`
|
|
|
|
How to develop plugins
|
|
|
|
`Development Mailing List <http://groups.google.com/group/ansible-devel>`_
|
|
|
|
Mailing list for development topics
|
|
|
|
`irc.freenode.net <http://irc.freenode.net>`_
|
|
|
|
#ansible IRC chat channel
|
|
|
|
|