Introduction
Ansible, a powerful open-source automation tool, simplifies complex IT tasks by automating configuration management, application deployment, and other repetitive operations. One of Ansible's strengths lies in its extensibility, allowing users to customize their workflows. In this article, we'll explore the use of Ansible callback plugins and how they can enhance playbook output.
Ansible Playbook Overview
Before diving into callback plugins, let's examine a simple Ansible playbook. The playbook named ping.yml showcases the basic functionality of the Ansible ping module. It verifies the connectivity to target hosts and reports the results.
``yaml
---
- name: Ping module Playbook
hosts: all
tasks:
- name: Test connection
ansible.builtin.ping:
`
Additionally, an inventory file specifies the target host, in this case, localhost with a local connection:
`ini
localhost ansible_connection=local
`
Executing the Playbook Without Callback Plugins
When running the playbook without callback plugins, the command looks like this:
`bash
ansible-playbook -i inventory ping.yml -v
`
The output shows the default Ansible playbook execution summary:
`
PLAY [Ping module Playbook] *
TASK [Gathering Facts]
ok: [localhost]
TASK [Test connection]
ok: [localhost] => {"changed": false, "ping": "pong"}
PLAY RECAP **
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
`
This standard output provides essential information, but what if you want a more detailed and customized report?
Introducing Callback Plugins
Callback plugins in Ansible offer a way to customize and extend the output of playbook runs. In the provided ansible.cfg configuration file, the following lines enable the callback plugin:
`ini
[defaults]
callbacks_enabled=community.general.yaml
stdout_callback = community.general.yaml
`
Now, when running the playbook with the callback plugin, use the same command:
`bash
ansible-playbook -i inventory ping.yml -v
`
The output is now enriched with additional details, providing a more structured and readable format:
``
PLAY [Ping module Playbook] *
TASK [Gathering Facts]
ok: [localhost]
TASK [Test connection]
ok: [localhost] => changed=false
ping: pong
PLAY RECAP **
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignore