Enable Logging

By default, Ansible does not log any information regarding its plays. If you wished, for example, to look back on the execution of a play completed yesterday, you wouldn’t be able too.

To address this, we’ll enable logging which writes all information displayed during an execution of a play to /var/log/ansible.log. To ensure this files does not grow too large, we’ll also configure logrotate so it’s periodically pruned.

Create File

Within /var/log/ directory create a new file called ansible.log.

touch /var/log/ansible.log

Ensure the ansible user has full control over the file.

chown ansible:ansible /var/log/ansible.log

Update Configuration

Open Ansible’s configuration file located at /etc/ansible/ansible.cfg and add the following line:

ℹ️
The directory /etc/ansible and file ansible.cfg may need to be manually created.
[defaults]
log_path = /var/log/ansible.log

Log Rotate

As with any log file, it’s best that its periodically rotated to control how large it grows. For this, we’ll use logrotate. If you do not already have logrotate, you can install it via the command sudo apt install logrotate.

Create a configuration file for ansible under logrotate’s configuration directory.

touch /etc/logrotate.d/ansible

Add the following contents to the file:

/var/log/ansible.log
{
    missingok
    monthly
    rotate 4
    notifempty
}

That’s it, no restarting of services required. Going forward the log file will be rotated monthly, with four files retatined; the oldest being deleted when logrotate runs.