[英]Capture Return Code of task as exit code for Ansible Playbook
Let's start from the very beginning:让我们从头开始:
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 .
请参阅工件。
Use set_stats to customize the playbook's output.使用set_stats自定义 playbook 的输出。
Use ansible-runner to run the playbook.使用ansible-runner运行剧本。
Get the customized output from artifacts/ID/stdout .从artifacts/ID/stdout获取自定义输出。
Create wrapper.创建包装器。 FWIW, see as a hint arwrapper.bash and Ansible Runner Usage Examples .
FWIW,作为提示arwrapper.bash和Ansible Runner Usage Examples 。
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.