Ansible logs are crucial for debugging, auditing, and monitoring your automation workflows. By default, Ansible does not log to a file unless explicitly configured. This article explains where Ansible logs are stored, how to enable logging, and best practices for managing log files.
Where Are Ansible Logs Stored?
Default Behavior
By default, Ansible logs output to the terminal (stdout) during playbook execution. To capture logs in a file, you must configure the logging settings in the ansible.cfg
file.
Enabling Logging in Ansible
To enable logging, follow these steps:
Open or create the
ansible.cfg
file in your project directory or system-wide configuration path (e.g.,/etc/ansible/ansible.cfg
).Add or modify the following lines under the
[defaults]
section:[defaults] log_path = /var/log/ansible/ansible.log
This configuration stores logs in
/var/log/ansible/ansible.log
.Ensure the directory exists and has appropriate permissions:
sudo mkdir -p /var/log/ansible sudo chmod -R 755 /var/log/ansible
Verify the configuration by running a playbook:
ansible-playbook playbook.yml
Logs will be written to the specified file.
Common Log File Locations
Project-Specific Logs: If you specify a relative path in
ansible.cfg
, logs will be stored in the project directory:./ansible.log
System-Wide Logs: When using a global configuration file, logs are often stored under:
/var/log/ansible/ansible.log
Custom Paths: You can define any valid file path for logs in the
log_path
setting.
Using Environment Variables for Logging
Set the log file path dynamically using the ANSIBLE_LOG_PATH
environment variable:
export ANSIBLE_LOG_PATH=/custom/path/ansible.log
This overrides the log_path
setting in the configuration file for the current session.
Log Levels and Verbosity
To capture detailed information in logs, use the -v
(verbose) option during playbook execution:
-v
: Basic verbosity.-vv
: More detailed logs.-vvv
: Debug-level logs.
Example:
ansible-playbook playbook.yml -vvv
The increased verbosity helps in diagnosing complex issues.
Managing Log File Size
Large log files can become difficult to manage. Use log rotation to limit file size and maintain historical data:
Install and configure
logrotate
:sudo apt install logrotate
Add a custom configuration for Ansible logs:
/var/log/ansible/ansible.log { rotate 7 daily compress missingok notifempty }
This configuration rotates the log file daily, keeps seven days of logs, and compresses older logs.
Best Practices for Ansible Logging
Centralize Logs: Use centralized logging tools like Elasticsearch, Splunk, or Graylog for better visibility.
Secure Logs: Restrict access to log files to protect sensitive information.
Use Separate Logs for Projects: Store logs in project-specific directories for easier debugging and organization.
Enable Debug Logging for Issues: Use higher verbosity levels only when troubleshooting to avoid excessive log size.
Rotate Logs Regularly: Prevent disk space issues by enabling log rotation.
Conclusion
Ansible logs are not stored by default but can be enabled and customized through the ansible.cfg
file or environment variables. By setting up logging properly and following best practices, you can efficiently manage and analyze logs to optimize your automation workflows.
Learn More About Ansible Logging
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Explore more tips for managing Ansible logs in Ansible by Examples.
Donate
Support this project by contributing today.