0

我有一个可以像这样进行测试的可靠剧本

---
- 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

为什么我没有收到完整的错误消息?

谢谢

4

1 回答 1

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"
    ]
}
于 2020-09-22T00:08:42.780 回答