Ansible ansible.builtin.tree Callback Saving Host Events to Files
Unveiling the Power of ansible.builtin.tree Callback: Enhanced Visibility and Control in Ansible Automation
Introduction
Ansible, an open-source automation tool, is renowned for its ability to streamline IT tasks and configuration management. One of Ansible’s powerful features is its extensible callback system, which allows users to capture and handle events triggered during playbook execution. In this article, we’ll delve into the ansible.builtin.tree
callback, introduced in Ansible-core version 2.11, and explore how it can be utilized to save host events to files.
Understanding the Callback Parameters
directory
Parameter
The directory
parameter is a crucial component of the ansible.builtin.tree
callback. It specifies the path to the directory where per-host JSON files will be saved. When using ad-hoc commands, this directory can be set using the --tree
option. The default directory is “~/.ansible/tree.”
Let’s break down the relevant details:
Added in Ansible-core 2.11: The
directory
parameter was introduced in Ansible-core version 2.11, making it available for users in more recent releases.INI Entry:
[callback_tree] directory = ~/.ansible/tree
Users can configure the
directory
parameter in theansible.cfg
file under the[callback_tree]
section.Environment Variable:
export ANSIBLE_CALLBACK_TREE_DIR=~/.ansible/tree
Alternatively, the
directory
parameter can be set using theANSIBLE_CALLBACK_TREE_DIR
environment variable.
Practical Example
To illustrate the usage of the ansible.builtin.tree
callback, consider the following example playbook and configuration:
Inventory (inventory
)
localhost ansible_connection=local
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Playbook (ping.yml
)
---
- name: Ping module demo
hosts: all
tasks:
- name: Test connection
ansible.builtin.ping:
Ansible Configuration (ansible.cfg
)
[defaults]
callbacks_enabled=ansible.builtin.tree
[callback_tree]
directory = ~/.ansible/tree
In this example:
- The inventory file (
inventory
) contains a local host. - The playbook (
ping.yml
) is a simple one that pings all hosts to test the connection. - The Ansible configuration file (
ansible.cfg
) enables theansible.builtin.tree
callback and sets the directory for saving the host events to~/.ansible/tree
.
Execution
When the playbook is executed, the ansible.builtin.tree
callback will save host events to per-host JSON files in the specified directory (~/.ansible/tree
in this case). This information can be invaluable for troubleshooting, auditing, and reviewing the execution flow of Ansible playbooks.
By utilizing this callback, users can gain insights into the details of each host’s execution, providing a structured and organized way to capture events during playbook runs.
cat ~/.ansible/tree/localhost
{"changed": false, "ping": "pong"}
Conclusion
In conclusion, the ansible.builtin.tree
callback is a powerful tool for saving host events to files, enhancing Ansible’s capabilities for logging and analysis. Users can leverage this feature to improve the transparency and traceability of their automation workflows. As Ansible continues to evolve, exploring and understanding such features becomes essential for mastering the tool’s capabilities.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate