如何在日志文件中添加日期和时间戳? 目前,我有它作为log_path =在/ var/ansible-剧本/ ansible.log在ansible.cfg如何在日志文件中添加日期和时间戳?
每次我跑,我需要这个日志文件来与时间戳
例如保存ansible- 20160808142400.log
如何在日志文件中添加日期和时间戳? 目前,我有它作为log_path =在/ var/ansible-剧本/ ansible.log在ansible.cfg如何在日志文件中添加日期和时间戳?
每次我跑,我需要这个日志文件来与时间戳
例如保存ansible- 20160808142400.log
使用ANSIBLE_LOG_PATH环境变量。
执行剧本如下:
ANSIBLE_LOG_PATH=/tmp/ansible_$(date "+%Y%m%d%H%M%S").log ansible-playbook myplabook.yml
或者你可以写自己的回调插件,将记录你想要什么,你想要它。
如果您在基于UNIX的系统上运行,则可以利用inode的行为。在ansible.cfg中定义一条日志路径。我在$ HOME/.ansible中创建了一个目录。
log_path = $HOME/.ansible/log/ansible.log
创建你的剧本一个前期任务部分,包括以下任务:
- 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') }}"
当运行它创建日志文件,并开始写它的剧本ansible开始。日志文件然后重命名为ansible.log-YYYYmmddHHMMSS,并且可信过程继续写入,因为即使日志文件的名称已更改与其关联的inode尚未。