2

How do I append date and timestamp in the Ansible log file?

Currently I have it as

log_path=/var/ansible-playbooks/ansible.log

in the ansible.cfg.

Everytime I run, I need this log to file to be saved with the timestamp like

ansible-20160808142400.log
U880D
  • 8,601
  • 6
  • 24
  • 40
Naveen S
  • 31
  • 1
  • 3

2 Answers2

3

Use the ANSIBLE_LOG_PATH environment variable.

Execute playbook as follows:

ANSIBLE_LOG_PATH=/tmp/ansible_$(date "+%Y%m%d%H%M%S").log ansible-playbook myplabook.yml

Alternatively you can write your own Callback plugin that will log what you want and where you want it to.

U880D
  • 8,601
  • 6
  • 24
  • 40
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
2

If you're running on a UNIX based system you can take advantage of the behavior of inodes. Define a log path in your ansible.cfg. I created a directory in $HOME/.ansible.

log_path = $HOME/.ansible/log/ansible.log

Create a pre-task section in your playbooks and include the following task:

- name: Create the log file for this run
  shell: /bin/bash -l -c "mv {{ lookup('env', 'HOME') }}/.ansible/log/ansible.log  {{ lookup('env', 'HOME') }}/.ansible/log/ansible.log-{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
  delegate_to: localhost
  become: yes
  become_user: "{{ lookup('env', 'USER') }}"

When ansible starts running a playbook it creates the log file and starts writing to it. The log file is then renamed to ansible.log-YYYYmmddHHMMSS and the ansible process continues to write to it because even though the log file's name has changed the inode associated with it hasn't.