Skip to content

example of how to use and write a custom ansible callback plugin for sending task and playbook output on run to an external endpoint - WIP

Notifications You must be signed in to change notification settings

patchandpray/ansible-callback-plugin

Repository files navigation

ANSIBLE CALLBACK PLUGIN

This repository shows how to use a custom ansible callback plugin to send the output of every task to an API endpoint.

The plugin will record the play and upon detecting a succesfull or failed task will be triggered to store the output of that task and send it using the python requests module to an API endpoint.

Configuration of the plugin is done by passing extra_vars to the playbook that will use the callback plugin.

We will be using a mock api server to be found in the api_server directory to handle the requests that we send to it to demonstrate the working of the callback plugin.

Configuration parameters

It is possible to expose configuration parameters to the callback plugin as extra_vars or from playbook variables.

  • API Endpoint url
  • username
  • password

[extra_vars]
callback_url = the api endpoint callback url
username = the username for api authentication
password = the password for api authentication

Outputs

For tasks the current payload is sent to the API backend

state = success or failed
task_name = the name of the task that was succesfull or failed
task_output = the output of the task

How it works

The custom ansible callback plugin is to be placed in a directory called callback_plugins in the directory where the playbook is run from. In the ansible.cfg file specify the custom callback_plugins directory: [defaults]
callback_plugins = ./callback_plugins

Every callback plugin there of type notification will be picked up and run for all the runner calls it is configured for.

The plugin will make use of the python requests library to send the captured payload from a runner call as payload to a configured API endpoint. HTTPBasicAuth is supported for simulating calls that require API authentication.

For simulating the API endpoint a small server is implemented using Flask and flask_restfull, this can be found in the api_server directory.

All default available callback_plugins can be found in your ansible install location, usually /usr/lib/python2.7/site-packages/ansible/plugins/callback

How to run

Be sure to have the api_server provided running or have your own API endpoint available which supports the fields {'state, 'task_name', task_output'}

Setup api server

Using the provided api_server:

  • python3 -m venv env or virtualenv env (for python2.x)
  • source env/bin/activate
  • pip install -r requirements.txt
  • optionally configure api server defaults in server_config.ini
  • make run

Run the callback plugin using Makefile

Be sure to run from virtualenv as python requests library is required.
make playbook
You might have to change the extra vars passed for that makefile target to fit your configuration.

Or run Manual

ansible-playbook playbook.yml -e callback_url=<http://api_host:port/tasks> -e username=<basic_auth_username> -e password=<basic_auth_password>

About

example of how to use and write a custom ansible callback plugin for sending task and playbook output on run to an external endpoint - WIP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published