Optimizing Ansible Automation Platform with Callback Plugins in ansible.cfg
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
Learn how to optimize Ansible using callback plugins in the ansible.cfg file. Configure callback_whitelist and callbacks_enabled for better performance.

Introduction
Ansible Automation Platform is a powerful automation tool used for configuration management, application deployment, and task automation. To harness the full potential of Ansible, it's essential to fine-tune its configuration according to your specific needs. In this article, we'll delve into the ansible.cfg file and explore how to optimize Ansible's performance by utilizing callback plugins. Specifically, we will focus on the[defaults] section of the ansible.cfg file and the configuration options callback_whitelist and callbacks_enabled.
See also: Leveraging Ansible Callback Plugins for Enhanced Performance
Understanding Callback Plugins
Callback plugins in Ansible are used to customize the output of playbooks and provide additional functionality during playbook execution. They can be configured globally in the ansible.cfg file or per-playbook using theansible.cfg setting in a playbook. Callback plugins offer a wide range of capabilities, from generating custom reports to monitoring playbook execution time.
The ansible.cfg File
The Ansible.cfg file is a central configuration file that governs Ansible's behavior. It is typically located in the/etc/ansible/ directory for system-wide configurations or in the project directory for playbook-specific configurations. To modify Ansible's behavior, you can edit this file to include various settings related to callback plugins.
Configuring Callback Plugins in ansible.cfg
Within theansible.cfg file, configuration settings for callback plugins are placed in the [defaults] section. In the provided snippet:
[defaults]
callback_whitelist=ansible.posix.timer,ansible.posix.profile_tasks,ansible.posix.profile_roles
callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks,ansible.posix.profile_roles
Two key options are utilized:
callback_whitelist: This option specifies the list of callback plugins that are allowed to run during playbook execution. Callback plugins not included in this list will be disabled. In the snippet, three callback plugins are whitelisted: ansible.posix.timer, ansible.posix.profile_tasks, and ansible.posix.profile_roles.
callbacks_enabled: This option specifies the callback plugins that should be enabled. By default, if a callback plugin is whitelisted, it is also enabled. However, you can use this option to enable or disable specific plugins explicitly. In this case, the same three plugins are listed for enabled callbacks.
Benefits of Callback Plugins
Configuring callback plugins inansible.cfg provides several benefits:
Performance Optimization: Callback plugins like ansible.posix.timer allow you to measure the execution time of tasks and roles, helping identify performance bottlenecks in your playbooks.
Profiling Playbooks: Callbacks such as ansible.posix.profile_tasks and ansible.posix.profile_roles enable detailed profiling of playbook execution, aiding in troubleshooting and optimization efforts.
Custom Reporting: Callback plugins can be customized to generate specific reports or logs, making it easier to track the progress of your Ansible automation.
Selective Execution: By configuring callback plugins, you can choose which callbacks are active, reducing noise and focusing on the information that matters most to you.
Links:
• https://docs.ansible.com/ansible/latest/collections/ansible/posix/timer_callback.html • https://docs.ansible.com/ansible/latest/collections/ansible/posix/profile_tasks_callback.html • https://docs.ansible.com/ansible/latest/collections/ansible/posix/profile_roles_callback.htmlSee also: Ansible Version Comparison: Compare Versions with version Test
Playbook Playbook
Suppose we have the helloworld.yml Ansible Playbook that prints the "Hello World" message on the screen.---
- name: Hello World Sample
hosts: all
tasks:
- name: Hello Message
ansible.builtin.debug:
msg: "Hello World!"
We can execute the helloworld.yml playbook within our Ansible Automation Platform using Job Templates.
Execution with Callback Plugins
In the following, the output with the timer, profile_tasks, and profile_roles Callback Plugins enabled in the ansible.cfg file:
• ansible.cfg
[defaults]
callback_whitelist=ansible.posix.timer,ansible.posix.profile_tasks,ansible.posix.profile_roles
callbacks_enabled=ansible.posix.timer,ansible.posix.profile_tasks,ansible.posix.profile_roles
The execution of the Job Template is like the following Figure:
Execution without Callback Plugins
In the following, the output without the timer, profile_tasks, and profile_roles Callback Plugins enabled in theansible.cfg file:
Conclusion
We can fine-tune our Ansible Automation Platform leveraging theansible.cfg file. It is a powerful tool for fine-tuning Ansible's behavior, and callback plugins offer a way to significantly enhance its performance and functionality. By configuring the [defaults] section of ansible.cfg with options like callback_whitelist and callbacks_enabled, you can tailor Ansible's callback plugin usage to your specific requirements. This control level helps improve performance and streamlines our Ansible Controller Jobs and Workflows, making them more efficient and informative.
See also: Creating a Custom Ansible Lookup Plugin in Python for Reading a File
Related Articles
• using ansible.builtin.template effectively • Ansible roles guideCategory: troubleshooting
Watch the video: Optimizing Ansible Automation Platform with Callback Plugins in ansible.cfg — Video Tutorial