问题可能是由于缓冲区未清空引起的。可以通过使用“interact”命令在任务结束后执行一个额外的交互步骤来清空缓冲区。代码示例如下:
name: Execute command with expect expect: command: some_command responses: 'password': "{{ my_password }}" register: result
name: Clear buffer and write to log expect: command: interact chdir: "{{ playbook_dir }}" when: result is succeeded
name: Write result to log file copy: content: "{{ result.stdout }}" dest: "/var/log/my_log_file.log"
这个解决方案在执行完命令后,使用expect模块的“interact”命令清理缓冲区。然后,该结果被写入日志文件。