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:

  1. Open or create the ansible.cfg file in your project directory or system-wide configuration path (e.g., /etc/ansible/ansible.cfg).

  2. 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.

  3. Ensure the directory exists and has appropriate permissions:

    sudo mkdir -p /var/log/ansible
    sudo chmod -R 755 /var/log/ansible
    
  4. Verify the configuration by running a playbook:

    ansible-playbook playbook.yml
    

    Logs will be written to the specified file.

Common Log File Locations

  1. Project-Specific Logs: If you specify a relative path in ansible.cfg, logs will be stored in the project directory:

    ./ansible.log
    
  2. System-Wide Logs: When using a global configuration file, logs are often stored under:

    /var/log/ansible/ansible.log
    
  3. 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:

  1. Install and configure logrotate:

    sudo apt install logrotate
    
  2. 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

  1. Centralize Logs: Use centralized logging tools like Elasticsearch, Splunk, or Graylog for better visibility.

  2. Secure Logs: Restrict access to log files to protect sensitive information.

  3. Use Separate Logs for Projects: Store logs in project-specific directories for easier debugging and organization.

  4. Enable Debug Logging for Issues: Use higher verbosity levels only when troubleshooting to avoid excessive log size.

  5. 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.

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Support this project by contributing today.

Patreon Buy me a Pizza