python - 在ansible中使用shell运行python脚本,但似乎无法使用print()输出stdout
问题描述
shell: sleep 5; nohup python /tmp/rsync.py {{check}} {{source}} {{destination}} {{dest_host}} &
register: status
failed_when: "'FAILED' in status.stdout"
我的剧本有类似上面的代码片段。在 python 脚本中,我使用了 print 语句。但是当我运行和使用调试标准输出是空的。对于调试,我尝试了这两种方法 -
- name: debug
debug: var=status.stdout_lines
- name: debug
debug: msg={{ status.stdout }}
为了打印到标准输出,我还使用了sys.stdout.write
. 即使这似乎不起作用。实现了我需要的一切。只有这行不通。
编辑:我必须在后台运行它。有没有新的方法来处理这个?
解决方案
使用 Ansible [1]的异步支持
根据你的 shell 命令执行的时间调整 async 的值(如果是 5 秒,使 async 略高于 5)
根据您希望检查 shell 是否完成执行的频率调整 poll 的值
- name: Execute shell
shell: sleep 5; nohup python /tmp/rsync.py {{check}} {{source}} {{destination}} {{dest_host}}
async: 100
poll: 1
register: status
failed_when: "'FAILED' in status.stdout"
推荐阅读
- java - Android比较字符串与数组并删除数组索引值
- angular - 创建模块的 Angular6 服务
- docker - 在 vespa 的同一个容器上部署多个应用程序包
- javascript - Prismic - 如何在不暴露访问令牌的情况下进行 API 调用
- javascript - 从另一个数组中选择同名项目
- sql - SQL Server 参数异常
- angular - url中的多个参数不起作用Angular 6
- python - 使用带有 Pandas 的 Google 表单替代已弃用的方法
- dart - Flutter Await 回调不给出任何响应
- json - Flutter JSON 重复索引