简体   繁体   English

捕获任务的返回代码作为 Ansible Playbook 的退出代码

[英]Capture Return Code of task as exit code for Ansible Playbook

Let's start from the very beginning:让我们从头开始:

  • I have a bash script which returns different exit codes accordingly with the error reported.我有一个 bash 脚本,它会根据报告的错误返回不同的退出代码。
  • I launch that script with ansible BUT, when the script fails the ansible-playbook exit code which I check with "echo $?"我用 ansible BUT 启动该脚本,当脚本失败时,我用“echo $?”检查了 ansible-playbook 退出代码。 is different from the exit code of the remote script.与远程脚本的退出代码不同。

I know Ansible has the following exit codes:我知道 Ansible 有以下退出代码:

*0* -- OK or no hosts matched

*1* -- Error

*2* -- One or more hosts failed

*3* -- One or more hosts were unreachable

*4* -- Parser error

*5* -- Bad or incomplete options

*99* -- User interrupted execution

*250* -- Unexpected error

So the question is if there is any way to have the return code of the task as the exit code of the Ansible Playbook.所以问题是是否有任何方法可以将任务的返回码作为 Ansible Playbook 的退出码。

Regards,问候,

Q: "Is any way to have the return code of the task as the exit code of the AnsiblePlaybook?" Q: “有什么方法可以将任务的返回码作为 AnsiblePlaybook 的退出码?”

A: There is no such option. A:没有这样的选择。 It's possible to use ansible-runner instead.可以使用 ansible-runner 代替。 See Artifacts .请参阅工件

ansible-runner is complex and doesn't work on macOS. ansible-runner 很复杂,不适用于 macOS。

Here is the end of my playbook.yml:这是我的 playbook.yml 的结尾:

  tasks:
    - name: Prepare VM
      script: ./logic.bash &> ~/logic.bash.log
      register: returned
      ignore_errors: yes
    - name: read log
      shell: |
        cat ~/logic.bash.log
      register: file_content
    - name: print log
      debug:
        verbosity: 2
        msg: "{{ file_content.stdout }}"
    - name: print RC
      debug:
        msg: "RETURN_CODE: {{returned.rc}}"

And here is the code I use in a script called run.bash to execute the playbook and check the RC of the task.这是我在名为 run.bash 的脚本中使用的代码,用于执行 playbook 并检查任务的 RC。

ID="${RANDOM}"
ansible-playbook -vvvv -i ${BUZYFORM_INVENTORY_FILE} playbook.yml > /tmp/"${ID}"
RC="$(grep RETURN_CODE /tmp/${ID} | cut -d"|" -f2)"
cat /tmp/"${ID}"
[[ ${RC} -gt 0 ]] && exit ${RC} || true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM