Ansible-至少一项失败循环时跳过任务 [英] Ansible - Skip task when at least one item fails in loop
问题描述
我正在使用sqlplus运行一些SQL脚本.在运行之前,我将从该目录中获取所有sql文件列表,并将其存储在sql_out
中,如下所示.
I am running few SQL scripts using sqlplus present. Before running I am getting all the list of sql files from that directory and storing it in sql_out
as shown below.
问题是如果其中一个sql脚本失败,则其余sql脚本仍将执行.如果任何一个脚本失败,我想完全跳过该任务.有什么办法可以跳过.我认为我们可以使用with_items
,但不确定如何实现.有帮助吗?
The problem is if one of the sql script fails the rest of the sql scripts still executes. I want to skip the task completely if any one of the script fails. Is there any way to skip. I reckon we can use with_items
but not sure how to implement. Any help?
- name: "Get sql files from directory"
shell: ls {{ directory }}/{{ scripts_path }}/*.sql
register: sql_out
tags:
- sql
- name: "Execute each SQL Scripts"
script: sqlplus.sh {{ db_username }} {{ db_password }} {{
connection_string }} {{ schema }} {{ item }}
delegate_to: localhost
with_items: sql_out.stdout_lines
tags:
- sql
推荐答案
AFAIK从当前的Ansible 2.3版开始是不可能的.
AFAIK this is not possible as of current Ansible version 2.3.
Task executor works in such way that it executes every loop iteration first and only then analyzes task/items results.
您应该重构您的shell脚本,以便能够将脚本列表作为参数接收,并在脚本内(而不是使用Ansible)对其进行迭代.这也会大大提高速度.
You should refactor your shell script to be able to receive scripts list as parameter and iterate them inside the script, not with Ansible. This will also give you significant speed boost.
这篇关于Ansible-至少一项失败循环时跳过任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!