我有一本非常有趣的剧本,可以进行这样的测试
---
- name: "test"
hosts: localhost
tasks:
- name: "list files"
block:
- name: "list files"
command: /usr/bin/example-command -x -y -z
register: output
rescue:
- script: test.py {{ output.msg }}
args:
executable: python3
它将失败,我想捕获错误味精并将其发送到python脚本test.py(目前,我只是将味精写入文件中
import sys
with open("/tmp/workfile", "w") as f:
f.write(sys.argv[1])
执行剧本,我看了/ tmp / workfile,我得到了
[Errno
为什么我没有收到完整的错误消息?
谢谢
答案 0 :(得分:1)
您需要在脚本中引用该参数。考虑以下剧本:
---
- name: test
hosts: localhost
gather_facts: false
vars:
output:
msg: This is a test.
tasks:
- script: test.py {{ output.msg }}
args:
executable: python3
register: script1
- script: test.py "{{ output.msg }}"
args:
executable: python3
register: script2
- debug:
msg:
- "{{ script1.stdout }}"
- "{{ script2.stdout }}"
test.py
的简单之处是:
import sys
print('argv[1]: ', sys.argv[1])
最终任务输出:
TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
"argv[1]: This\n",
"argv[1]: This is a test.\n"
]
}