2013-09-30 01:10:28 +02:00
Developing Dynamic Inventory Sources
====================================
2012-03-31 04:28:30 +02:00
2013-12-26 20:32:01 +01:00
.. contents :: Topics
2013-12-21 08:06:38 +01:00
:local:
2013-12-26 20:32:01 +01:00
2013-12-21 08:06:38 +01:00
As described in :doc: `intro_dynamic_inventory` , ansible can pull inventory information from dynamic sources, including cloud sources.
2012-03-31 04:28:30 +02:00
2013-09-30 01:03:51 +02:00
How do we write a new one?
2012-08-28 21:41:10 +02:00
2013-09-30 01:10:28 +02:00
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.
2012-03-31 04:28:30 +02:00
2013-10-04 16:41:44 +02:00
.. _inventory_script_conventions:
2012-03-31 04:28:30 +02:00
Script Conventions
2012-03-31 15:55:37 +02:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-31 04:28:30 +02:00
2014-01-22 06:05:21 +01:00
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::
2012-03-31 04:28:30 +02:00
{
2013-04-11 06:43:16 +02:00
"databases" : {
"hosts" : [ "host1.example.com", "host2.example.com" ],
"vars" : {
"a" : true
2012-11-27 00:13:56 +01:00
}
},
2013-04-11 06:43:16 +02:00
"webservers" : [ "host2.example.com", "host3.example.com" ],
"atlanta" : {
"hosts" : [ "host1.example.com", "host4.example.com", "host5.example.com" ],
"vars" : {
"b" : false
2012-11-27 00:13:56 +01:00
},
2014-02-21 05:02:07 +01:00
"children": [ "marietta", "5points" ]
2012-11-27 00:13:56 +01:00
},
2013-04-11 06:43:16 +02:00
"marietta" : [ "host6.example.com" ],
"5points" : [ "host7.example.com" ]
2012-03-31 04:28:30 +02:00
}
2013-08-23 18:33:57 +02:00
.. versionadded :: 1.0
2012-11-27 00:13:56 +01:00
Before version 1.0, each group could only have a list of hostnames/IP addresses, like the webservers, marietta, and 5points groups above.
2014-01-22 06:05:21 +01:00
When called with the arguments `` --host <hostname> `` (where <hostname> is a host from above), the script must return either an empty JSON
2012-11-27 00:13:56 +01:00
hash/dictionary, or a hash/dictionary of variables to make available to templates and playbooks. Returning variables is optional,
2012-03-31 04:28:30 +02:00
if the script does not wish to do this, returning an empty hash/dictionary is the way to go::
{
2013-04-11 06:43:16 +02:00
"favcolor" : "red",
"ntpserver" : "wolf.example.com",
"monitoring" : "pack.example.com"
2012-03-31 04:28:30 +02:00
}
2013-10-04 16:41:44 +02:00
.. _inventory_script_tuning:
2013-08-04 17:45:31 +02:00
Tuning the External Inventory Script
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2013-08-23 18:33:57 +02:00
.. versionadded :: 1.3
2013-08-04 17:45:31 +02:00
The stock inventory script system detailed above works for all versions of Ansible, but calling
2014-01-22 06:05:21 +01:00
`` --host `` for every host can be rather expensive, especially if it involves expensive API calls to
2013-11-16 23:01:26 +01:00
a remote subsystem. In Ansible
2013-08-04 17:45:31 +02:00
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
2014-01-22 06:05:21 +01:00
a value for "hostvars", the inventory script will not be invoked with `` --host `` for each host. This
2013-08-04 17:45:31 +02:00
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 },
}
}
}
2013-12-15 12:09:43 +01:00
.. seealso ::
2013-10-05 18:31:16 +02:00
: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
2014-01-28 17:04:34 +01:00
`Ansible Tower <http://ansible.com/ansible-tower> `_
2013-10-05 18:31:16 +02:00
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