DEV Community

Dennis Ploeger
Dennis Ploeger

Posted on

Better Ansible Playbook Output in Teamcity

We're using some good amount of Ansible playbooks in my company and we use TeamCity as our main CI service.

On TeamCity, our playbook output looks like this:

Ansible Playbook output without the plugin

πŸ€¨πŸ™πŸ˜ž

I don't know about you, but I quite dislike the Ansible playbook default output. It's quite packed and big and it's hard to check where Ansible is currently working on.

TeamCity has a very nice feature called Build Script Interaction, where you format your stdout according to specific standards so that it can be interpreted by TeamCity.

This way you can open and close blocks, set variables, mark errors, etc.

Did I say "open and close blocks"? Yes I did and this would make the perfect feature for Ansible playbooks, right?

Luckily, Ansible can be extended by plugins and the Ansible output is handled by so called callback plugins. Callback plugins react to certain events (check out the class definition of CallbackBase for a list of available methods to override) and some of those events regard the start of plays or tasks.

So I extended the original default output plugin and override the specific methods for plays and tasks.

And with this plugin in place, the logs now look like this:

Ansible Playbook output with the plugin

😍😍😍

All nicely structured and TeamCity can even calculate the times of plays and tasks!

Check it out on GitHub:

GitHub logo dodevops / ansible-teamcity-callback

An Ansible callback plugin to output suitable for TeamCity

Ansible Callback Plugin for Teamcity

Introduction

This Ansible callback plugin formats the output of an Ansible playbook, so that it can be better interpreted by TeamCity.

Why?

Because Ansible logs in Teamcity look like this:

Log without this plugin

With this plugin in place, they look like this:

Log with this plugin

All plays and tasks are nicely put into their own blocks allowing TeamCity to collapse them and calculate the different times.

Usage

You have different options of using this plugin:

  • In your playbook repository:
    • Create a folder named callback_plugins directly where your playbook lives
    • Download the file teamcity.py and place it in inside the callback_plugins directory
  • Somewhere else:
    • Download or clone the plugin, unzip it and place the folder somewhere accessible (i.e. use the Teamcity tools feature to distribute the plugin to all agents)
    • Use the following environment variable to tell Ansible where to find your plugin: export ANSIBLE_CALLBACK_PLUGINS=<path of your plugin>

Set the following environment…

Top comments (0)