Reputation: 41
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
Upvotes: 3
Views: 9717
Reputation: 68269
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.
Upvotes: 3
Reputation: 41
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.
Upvotes: 2