developing_inventory.rst 3.72 KB
Newer Older
Michael DeHaan committed
1 2
Developing Dynamic Inventory Sources
====================================
3

4
.. contents:: Topics
5
   :local:
6

7
As described in :doc:`intro_dynamic_inventory`, ansible can pull inventory information from dynamic sources, including cloud sources.
8

9
How do we write a new one?
10

Michael DeHaan committed
11 12
Simple!  We just create a script or program that can return JSON in the right format when fed the proper arguments.
You can do this in any language.
13

14 15
.. _inventory_script_conventions:

16
Script Conventions
17
``````````````````
18

Matt Jaynes committed
19
When the external node script is called with the single argument ``--list``, the script must return a JSON hash/dictionary of all the groups to be managed. Each group's value should be either a hash/dictionary containing a list of each host/IP, potential child groups, and potential group variables, or simply a list of host/IP addresses, like so::
20 21

    {
Trevor Wennblom committed
22 23 24 25
        "databases"   : {
            "hosts"   : [ "host1.example.com", "host2.example.com" ],
            "vars"    : {
                "a"   : true
26 27
            }
        },
Trevor Wennblom committed
28 29 30 31 32
        "webservers"  : [ "host2.example.com", "host3.example.com" ],
        "atlanta"     : {
            "hosts"   : [ "host1.example.com", "host4.example.com", "host5.example.com" ],
            "vars"    : {
                "b"   : false
33
            },
bryfry committed
34
            "children": [ "marietta", "5points" ]
35
        },
Trevor Wennblom committed
36 37
        "marietta"    : [ "host6.example.com" ],
        "5points"     : [ "host7.example.com" ]
38 39
    }

40
.. versionadded:: 1.0
41 42 43

Before version 1.0, each group could only have a list of hostnames/IP addresses, like the webservers, marietta, and 5points groups above.

Matt Jaynes committed
44
When called with the arguments ``--host <hostname>`` (where <hostname> is a host from above), the script must return either an empty JSON
45
hash/dictionary, or a hash/dictionary of variables to make available to templates and playbooks.  Returning variables is optional,
46 47 48
if the script does not wish to do this, returning an empty hash/dictionary is the way to go::

    {
Trevor Wennblom committed
49 50 51
        "favcolor"   : "red",
        "ntpserver"  : "wolf.example.com",
        "monitoring" : "pack.example.com"
52 53
    }

54 55
.. _inventory_script_tuning:

56 57 58
Tuning the External Inventory Script
````````````````````````````````````

59
.. versionadded:: 1.3
60 61

The stock inventory script system detailed above works for all versions of Ansible, but calling
Matt Jaynes committed
62
``--host`` for every host can be rather expensive,  especially if it involves expensive API calls to
63
a remote subsystem.  In Ansible
64 65
1.3 or later, if the inventory script returns a top level element called "_meta", it is possible
to return all of the host variables in one inventory script call.  When this meta element contains
Matt Jaynes committed
66
a value for "hostvars", the inventory script will not be invoked with ``--host`` for each host.  This
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
results in a significant performance increase for large numbers of hosts, and also makes client
side caching easier to implement for the inventory script.

The data to be added to the top level JSON dictionary looks like this::

    {

        # results of inventory script as above go here
        # ...

        "_meta" : {
           "hostvars" : {
              "moocow.example.com"     : { "asdf" : 1234 },
              "llama.example.com"      : { "asdf" : 5678 },
           }
        }

    }

86 87
.. seealso::

88 89 90 91 92 93
   :doc:`developing_api`
       Python API to Playbooks and Ad Hoc Task Execution
   :doc:`developing_modules`
       How to develop modules
   :doc:`developing_plugins`
       How to develop plugins
94
   `Ansible Tower <http://ansible.com/ansible-tower>`_
95 96 97 98 99
       REST API endpoint and GUI for Ansible, syncs with dynamic inventory
   `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