Introduction
When managing infrastructure with Ansible, it’s essential to have insights into resource consumption and performance metrics during playbook execution. Ansible provides a powerful tool for this purpose: callback plugins. This article explores how callback plugins can help you assess resource consumption, troubleshoot issues, and gain deeper insights into Ansible playbook execution.
Understanding Ansible Callback Plugins
What are Ansible callback plugins? According to Ansible’s documentation, callback plugins enable adding new behaviors to Ansible when responding to events. These plugins control most of the output you see when running Ansible commands, but they can also be used to:
- Add additional output.
- Integrate with other tools.
- Marshal events to a storage backend.
Callback plugins are a versatile way to customize and enhance Ansible’s functionality.
Focus on cgroup_memory_Conclusion and cgroup_perf_recap
This article focuses on two specific callback plugins: cgroup_memory_Conclusion and cgroup_perf_recap. These plugins utilize cgroups (Control Groups) to profile resource consumption during Ansible tasks and playbook execution.
cgroup_memory_Conclusion: This plugin profiles the maximum memory usage of Ansible and individual tasks and provides a recap at the end using cgroups.
cgroup_perf_Conclusion: This plugin profiles system activity, including memory and CPU usage, of Ansible and individual tasks and displays a recap at the end of playbook execution.
You can learn more about these plugins and their parameters by using the ansible-doc command:
``bash
ansible-doc --type callback cgroup_memory_Conclusion
ansible-doc --type callback cgroup_perf_Conclusion
`
Both plugins require cgroups, so make sure to install the libcgroup-tools package in RHEL7 and RHEL8, which provides a userspace interface for managing cgroups:
`bash
dnf install -y libcgroup-tools
`
Please note that libcgroup was dropped from RHEL9 since most of its functionality is handled by systemd.
Links
- https://docs.ansible.com/ansible/latest/collections/community/general/cgroup_memory_Conclusion_callback.html
- https://docs.ansible.com/ansible/latest/collections/ansible/posix/cgroup_perf_Conclusion_callback.html
Configuring Callback Plugins
To use these callback plugins during playbook execution, you need to adjust the ansible.cfg configuration file. Here's an example of how our ansible.cfg file might look after these adjustments:
`ini
[defaults]
callback_whitelist=timer, profile_tasks, profile_roles, cgroup_perf_Conclusion, cgroup_memory_recap
[callback_cgroup_perf_Conclusion]
control_group=ansible
[callback_cgroupmemConclusion]
cur_mem_file = /sys/fs/cgroup/memory/ansible_profile/memory.usage_in_bytes
max_mem_file = /sys/fs/cgroup/memory/ansible_profile/memory.max_usage_in_bytes
``
You’re almost ready to collect resource consumption data during playbook execution.