Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append date and timestamp in the Ansible log file?

Tags:

ansible

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
like image 730
Naveen S Avatar asked Sep 18 '25 12:09

Naveen S


2 Answers

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.

like image 167
Konstantin Suvorov Avatar answered Sep 21 '25 01:09

Konstantin Suvorov


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.

like image 27
Wile E. Quixote Avatar answered Sep 21 '25 03:09

Wile E. Quixote