如何阻止ansible跳转到第二个任务,直到第一个任务成功执行?

如何阻止ansible跳转到第二个任务,直到第一个任务成功执行?,ansible,Ansible,我的剧本是这样的: --- - hosts: localhost gather_facts: false - name: execute task 1 shell: nohup sh test001.sh >> nohup.out 2>&1 & async: 120 poll: 10 - name: read generated log for task 1 shell: cat test001result*

我的剧本是这样的:

--- 
  - hosts: localhost
    gather_facts: false
  - name: execute task 1
    shell: nohup sh test001.sh >> nohup.out 2>&1 &
    async: 120
    poll: 10
  - name: read generated log for task 1
    shell: cat test001result*
    register: execution_status
  - name: If task 1 failed
    shell: nohup sh test002.sh >> nohup.out 2>&1 &
    when: execution_status.stdout.find('success') == -1
  - name: If task 1 passed
    shell: nohup sh test003.sh >> nohup.out 2>&1 &
    when: execution_status.stdout.find('success') != -1
现在任务1需要60秒才能完成,即test001result至少在60秒后生成。尽管添加了异步120秒,ansible执行仍在继续,尽管task1成功,test002.sh仍在执行。这是因为尚未生成日志(显示0字节)

如何纠正此问题?

给定脚本

shell>cat test001.sh
echo$(日期)测试\u 01已开始
睡眠3
echo’success’>test001result
echo$(日期)测试\u 01已完成
出口0
外壳>cat test002.sh
echo$(日期)测试\u 02已开始
睡眠3
echo$(日期)测试\u 02已完成
出口0
外壳>cat test003.sh
echo$(日期)测试\u 03已开始
睡眠3
echo$(日期)测试\u 03已完成
出口0
在剧本中,不要分离流程

nohup sh test001.sh>>nohup.out 2>&1&
只需运行它

sh test001.sh>>nohup.out 2>&1
比如剧本

shell>cat playbook.yml
-主机:本地主机
收集事实:错误
任务:
-名称:执行任务1
外壳:sh test001.sh>>nohup.out 2>&1
异步:20
投票:5
忽略错误:true
-名称:读取任务1的生成日志
外壳:cat test001result
寄存器:执行状态
忽略错误:true
-调试:
var:execution\u status.stdout
-名称:如果任务1失败
外壳:nohup sh test002.sh>>nohup.out 2>&1&
当:execution\u status.stdout不是搜索('success')时
-名称:如果任务1已通过
外壳:nohup sh test003.sh>>nohup.out 2>&1&
当:execution\u status.stdout为search('success')时
给予

shell>cat nohup.out
2020年8月25日星期二晚上09:50:35 CEST测试开始
2020年8月25日星期二晚上09:50:38 CEST测试完成
2020年8月25日星期二晚上09:50:41 CEST测试开始
2020年8月25日星期二晚上09:50:44 CEST测试完成
test001.sh
未成功时,应以相同的方式运行
test002.sh


指令
async
poll
用于运行可能无法在合理时间内结束的进程。让我们测试这个案例,并在test001.sh中将睡眠时间增加到30秒。在这种情况下,前两个任务都将失败。任务
执行任务1
将因
异步
超时而失败,任务
读取任务1
的生成日志将因缺少文件
test001result
而失败。我们必须为这两项任务设置
ignore\u errors:true
。现在剧本给出了

shell>ansible playbook playbook.yml
播放[本地主机]****
任务[执行任务1]****
致命:[localhost]:失败!=>更改=错误
消息:异步任务未在请求的时间内完成-20秒
…忽略
任务[读取为任务1生成的日志]****
致命:[localhost]:失败!=>更改=真
cmd:cat test001result
增量:“0:00:00.003367”
完:“2020-08-2522:11:56.422448”
msg:非零返回码
rc:1
开始:“2020-08-2522:11:56.419081”
stderr:'cat:test001result:没有这样的文件或目录'
标准线路:
标准输出:“”
标准输出线:
…忽略
任务[调试]****
确定:[本地主机]=>
执行状态\u.stdout:'
任务[如果任务1失败]****
已更改:[localhost]
任务[如果任务1通过]****
正在跳过:[localhost]
重演****
localhost:确定=4更改=2不可访问=0失败=0跳过=1获救=0忽略=2
shell>cat nohup.out
2020年8月25日星期二晚上10:11:35 CEST测试开始
2020年8月25日星期二晚上10:11:56 CEST测试开始
2020年8月25日星期二晚上10:11:59 CEST测试完成

请参阅。请参阅。确保你的问题符合这些规则。伊姆霍,这是你能得到合理答案的唯一方法。@VladimirBotka:我已经在这里添加了剧本的细节,你现在能提出什么建议吗?谢谢,它工作得很好。关键是不要用nohup分离进程。但这引出了另一个问题,在我的一个测试用例中,我发现任务1可能需要4天才能完成,ansible文档对异步没有硬性限制,但这样处理这么长时间运行的任务是否是一个好方法?
shell> ansible-playbook playbook.yml

PLAY [localhost] ****

TASK [execute task 1] ****
changed: [localhost]

TASK [read generated log for task 1] ****
changed: [localhost]

TASK [debug] ****
ok: [localhost] => 
  execution_status.stdout: success

TASK [If task 1 failed] ****
skipping: [localhost]

TASK [If task 1 passed] ****
changed: [localhost]

PLAY RECAP ****
localhost: ok=4 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0