
Let's say you have the following playbook which will create the /tmp/foo.txt file and displays the JSON output.
---
- hosts: localhost
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
register: out
- debug:
var: out
...
By default, the default Ansible configuration file (/etc/ansible/ansible.cfg) has log_path commented out.
#log_path = /var/log/ansible.log
When commented out, Ansible will log events to the machines syslog (system log daemon). As an example, syslog may write Ansible events to the /var/log/messages file.
Mar 22 12:55:08 [localhost] ansible-command: Invoked with creates=None executable=None _uses_shell=True strip_empty_ends=True _raw_params=ping -c4 127.0.0.1 removes=None argv=None warn=True chdir=None stdin_add_newline=True stdin=None
There are a number of ways to configure Ansible to log events to a log file, where the higher option in this list will take precedence over the lower option.
- ANSIBLE_LOG_PATH variable
- log_path in ansible.cfg in the present working directory
- log_path in .ansible.cfg in the user home directory (e.g. /home/john.doe/.ansible.cfg)
- log_path in /etc/ansible/ansible.cfg
Here is how you could set the log file using the ANSIBLE_LOG_PATH variable on a Linux system. Be aware that you would need to issue the export command before issuing the ansible-playbook command.
export ANSIBLE_LOG_PATH=/home/john.doe/ansible.log
ansible-playbook test.yml
This isn't very practical as it's not reasonable to have to run the export ANSIBLE_LOG_PATH before running the ansible-playbook command.
You can export ANSIBLE_LOG_PATH in your users hidden .bash_profile file (e.g. /home/john.doe/.bash_profile) to make this setting permanent for the individual user.
~]$ cat /home/john.doe/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
export ANSIBLE_LOG_PATH=/home/$USER/ansible.log
Or you could use a bash shell script that first exports ANSIBLE_LOG_PATH and then run the ansible-playbook command. This is cool because then you don't need to export ANSIBLE_LOG_PATH in each users hidden .bash_profile file on your Linux system. For example, the bash shell script may have something like this.
#!/bin/bash
export ANSIBLE_LOG_PATH=/home/$USER/ansible.log
ansible-playbook example.yml
While it seems to be possible to set ANSIBLE_LOG_PATH as an environment variable in a playbook, I've not been able to get this to work properly. Weird.
---
- hosts: localhost
environment:
ANSIBLE_LOG_PATH: /home/$(whoami)/ansible.log
tasks:
- name: create /tmp/foo.txt
file:
path: /tmp/foo.txt
state: touch
register: out
- debug:
var: out
...
Likewise, you can define the log_path directive in your Ansible configuration file (ansible.cfg), where the higher option in this list will take precedence over the lower options.
- log_path in ansible.cfg in the present working directory
- log_path in .ansible.cfg in the user home directory (e.g. /home/john.doe/.ansible.cfg)
- log_path in /etc/ansible/ansible.cfg
In this example, Ansible will log events to /var/log/ansible.log.
[defaults]
log_path = /var/log/ansible.log
Create and rotate the log file
You may need to create the log file and update the permission so that it has the write permission for user, group and other. You may want to also configure logrotate to rotate the log files.
touch /var/log/ansible.log
chmod 0666 /var/log/ansible.log
ansible.log should have something like this.
PLAY [localhost]
TASK [create /tmp/foo.txt]
changed: [localhost]
TASK [debug]
ok: [localhost] => {
"out": {
"changed": true,
"dest": "/tmp/bar.txt",
"diff": {
"after": {
"atime": 1673502951.122303,
"mtime": 1673502951.122303,
"path": "/tmp/bar.txt",
"state": "touch"
},
"before": {
"atime": 1673502951.1182466,
"mtime": 1673502951.1182466,
"path": "/tmp/bar.txt",
"state": "absent"
}
},
"failed": false,
"gid": 100,
"group": "users",
"mode": "0644",
"owner": "john.doe",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 65234
}
}
PLAY RECAP
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Did you find this article helpful?
If so, consider buying me a coffee over at