Ansible 如何将失败模块的输出发送到文件

Ansible 如何将失败模块的输出发送到文件,ansible,ansible-2.x,Ansible,Ansible 2.x,下面是ansible fail模块的部分内容,我想将该模块的输出重定向到文件 - name: fail: msg: "SVRELOAD NOT DONE ON THIS PTS {{ ansible_hostname }}" when: last_restart_date.stdout != ansible_date_time.date register: failed_task 下面是我将如何做到这一点

下面是ansible fail模块的部分内容,我想将该模块的输出重定向到文件

     - name:
       fail:
         msg: "SVRELOAD NOT DONE ON THIS PTS {{ ansible_hostname }}"
       when: last_restart_date.stdout != ansible_date_time.date
       register: failed_task
下面是我将如何做到这一点(有点不同的方法)。为了演示解决方案,我尝试下载一个不存在的包。我捕捉到了这条信息,并写进了一个文件。你必须用拦网和救援调整你的剧本

剧本(带拦网、救援)

下面是文件
error msg.txt

{'msg': "No package matching 'does_not_exist' is available", 'failed': True, 'changed': False, 'ansible_facts': {'pkg_mgr': 'apt'}}

如果错误文件已存在且具有写入权限,则不必创建新文件。

因此,在失败之前,您应该将消息写入上一个任务中的日志文件(例如,在文件末尾某处添加一行)。我如何才能做到这一点?这也不适用于我消息:“SVRELOAD not DONE ON this PTS{ansible_hostname}”>>/tmp/ansible\u fail.txt
{'msg': "No package matching 'does_not_exist' is available", 'failed': True, 'changed': False, 'ansible_facts': {'pkg_mgr': 'apt'}}