developing_api.rst 3.17 KB
Newer Older
Michael DeHaan committed
1 2
Python API
==========
3

4 5
.. contents:: Topics

6
There are several interesting ways to use Ansible from an API perspective.   You can use
Michael DeHaan committed
7 8 9 10
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.

11
If you are looking to use Ansible programmatically from something other than Python, trigger events asynchronously, 
12
or have access control and logging demands, take a look at :doc:`tower` 
Michael DeHaan committed
13 14 15 16
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.
17

18 19
.. _python_api:

20 21
Python API
----------
Michael DeHaan committed
22 23

The Python API is very powerful, and is how the ansible CLI and ansible-playbook
24
are implemented.
Michael DeHaan committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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
40
expressed in the :doc:`modules` documentation.::
Michael DeHaan committed
41 42 43 44

    {
        "dark" : {
           "web1.example.com" : "failure message"
Trevor Wennblom committed
45
        },
Michael DeHaan committed
46 47 48 49 50 51 52 53
        "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.

54 55
.. _detailed_api_example:

Michael DeHaan committed
56 57 58 59 60 61 62 63 64 65 66 67 68
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,
Michael DeHaan committed
69
        module_name='command', module_args='/usr/bin/uptime',
Michael DeHaan committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    ).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``.

94 95 96 97 98 99 100 101 102 103 104 105 106
.. 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