如何使用 log_plays 为 Ansible 中的 playbook 执行生成日志文件

How to use log_plays to generate a log file for playbook execution in Ansible

我是 运行 Playbook,用于在 Windows 目标机器上安装内部软件。在此过程中,我正在打印 "msg" 的日志,以便我可以将其转发给质量检查/合规团队。 但是,我不知道如何使用 debug > "msg" 的输出生成日志文件并将其放在 Windows 主机上。 我知道 log_plays 可能有用,但我找不到任何关于如何实际使用该模块的示例。

任何示例代码将不胜感激。

所以,很遗憾,/var/log/ansible/hosts is hardcoded but otherwise it should behave as you would expect. You can enable the callback via ansible.cfg or the $ANSIBLE_STDOUT_CALLBACK environment variable:

env ANSIBLE_STDOUT_CALLBACK=log_plays ansible-playbook -i host1,host2 the_file.yml

请注意 ad-hoc mode does not load callback plugins,因此您需要明确要求:

env ANSIBLE_LOAD_CALLBACK_PLUGINS=yes ANSIBLE_STDOUT_CALLBACK=log_plays \
    ansible -i host1,host2 -m ping '*'

如果 /var/log/ansible/hosts 部分让您感到厌烦,还有 $ANSIBLE_LOG_PATH 会导致 ansible 将日志输出抄写到文件中,并且它在临时模式下工作正常:

env ANSIBLE_LOG_PATH=$PWD/my-log ansible -i host1,host2 -m ping '*'

可能有点晚了,但是您可以将 log_path=mylogfile 添加到 ansible.cfg 文件的 defaults 部分

参见上面答案中提供的link。