2016-09-23 22:48:37 +02:00
Python API
==========
2017-08-07 20:40:29 +02:00
.. note :: This document is out of date: 'ansible.parsing.dataloader' and 'ansible.runner' are not available in the current version of Ansible.
2016-09-23 22:48:37 +02:00
.. contents :: Topics
Please note that while we make this API available it is not intended for direct consumption, it is here
for the support of the Ansible command line tools. We try not to make breaking changes but we reserve the
right to do so at any time if it makes sense for the Ansible toolset.
The following documentation is provided for those that still want to use the API directly, but be mindful this is not something the Ansible team supports.
There are several interesting ways to use Ansible from an API perspective. You can use
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 execution and Playbook API at a basic level.
If you are looking to use Ansible programmatically from something other than Python, trigger events asynchronously,
2017-01-07 20:38:52 +01:00
or have access control and logging demands, take a look at :doc: `../tower`
2016-09-23 22:48:37 +02: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.
.. note :: Ansible relies on forking processes, as such the API is not thread safe.
2018-03-02 17:34:24 +01:00
.. _python_api_example:
2016-09-23 22:48:37 +02:00
2018-03-02 17:34:24 +01:00
Python API example
------------------
2016-09-23 22:48:37 +02:00
2018-03-02 17:34:24 +01:00
This example is by no means comphrehensive and is not meant to show you how to use all of Ansible's feautres,
it is just a simple demonstration on how to minmally run a couple of tasks.::
2016-09-23 22:48:37 +02:00
#!/usr/bin/env python
import json
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
2017-09-20 17:33:38 +02:00
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
2016-09-23 22:48:37 +02:00
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
class ResultCallback(CallbackBase):
"""A sample callback plugin used for performing an action as results come in
If you want to collect all results into a single object for processing at
the end of the execution, look into utilizing the `` json `` callback plugin
or writing your own custom callback plugin
"""
def v2_runner_on_ok(self, result, **kwargs):
"""Print a json representation of the result
This method could store the result in an instance attribute for retrieval later
"""
host = result._host
2017-09-20 17:33:38 +02:00
print(json.dumps({host.name: result._result}, indent=4))
2016-09-23 22:48:37 +02:00
2018-03-02 17:34:24 +01:00
# since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
2017-09-20 17:33:38 +02:00
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
2018-03-02 17:34:24 +01:00
options = Options(connection='local', module_path=['/to/mymodules'], forks=10, become=None, become_method=None, become_user=None, check=False, diff=False)
2016-09-23 22:48:37 +02:00
# initialize needed objects
2018-03-02 17:34:24 +01:00
loader = DataLoader() # Takes care of finding and reading yaml, json and ini files
2016-09-23 22:48:37 +02:00
passwords = dict(vault_pass='secret')
2018-03-02 17:34:24 +01:00
# Instantiate our ResultCallback for handling results as they come in, Ansible expects this to be one of it's main display outlets
2016-09-23 22:48:37 +02:00
results_callback = ResultCallback()
2018-03-02 17:34:24 +01:00
# create inventory, use path to host config file as source or hosts in a comma separated string
2018-01-16 16:57:11 +01:00
inventory = InventoryManager(loader=loader, sources='localhost,')
2018-03-02 17:34:24 +01:00
# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
2017-09-20 17:33:38 +02:00
variable_manager = VariableManager(loader=loader, inventory=inventory)
2016-09-23 22:48:37 +02:00
2018-03-02 17:34:24 +01:00
# create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
2016-09-23 22:48:37 +02:00
play_source = dict(
name = "Ansible Play",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='ls'), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)
2018-03-02 17:34:24 +01:00
# Create play object, playbook objects use .load instead of init or new methods,
# this will also automatically create the task objects from the info provided in play_source
2016-09-23 22:48:37 +02:00
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
2018-03-02 17:34:24 +01:00
# actually run it, instantiate task queue manager, which takes care of forking and setting up all objects to iterate over host list and tasks
2016-09-23 22:48:37 +02:00
tqm = None
try:
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
2018-03-02 17:34:24 +01:00
stdout_callback=results_callback, # Use our custom callback instead of the `` default `` callback plugin, which prints to stdout
2016-09-23 22:48:37 +02:00
)
2018-03-02 17:34:24 +01:00
result = tqm.run(play) # most interesting data for a play is actually sent to the callback's methods
2016-09-23 22:48:37 +02:00
finally:
2018-03-02 17:34:24 +01:00
# we always need to cleanup child procs, and the strucutres we use to communicate with them
2016-09-23 22:48:37 +02:00
if tqm is not None:
tqm.cleanup()
2018-03-02 17:34:24 +01:00
.. note :: Ansible emits warnings and errors via the display object, which prints directly to stdout, stderr and the Ansible log.
2016-09-23 22:48:37 +02:00
Advanced programmers may also wish to read the source to ansible itself,
for it uses the API (with all available options) to implement the `` ansible ``
command line tools (`` lib/ansible/cli/ `` ).
.. 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