仅在Play 1成功通过Ansible时如何执行Play 2

问题描述 投票:0回答:1

我有两个戏剧,每个戏剧都有一个任务。

第一次播放会检查每个目标上是否存在/var/test.dat。仅当第一部戏成功时,我才希望第二部戏同时运行,以并行执行这些脚本。

[如果第一个播放失败,即test.dat不存在,我希望在不执行第二个播放的情况下终止该剧本。为此,我将any_errors_fatal设置为true

我需要将ansible Play strategy设置为免费,因为每个脚本都需要30分钟才能完成。

我对Ansible的理解是有限的。

我知道,如果我在一个PLAY上同时拥有两个任务,并且将策略设置为释放,则两个任务将并行运行,这是我不希望的。

---
- name: Play 1- check for login and script
  hosts: all_hosts
  any_errors_fatal: true
  strategy: free
  tasks:

   - name: Check script existence
     shell: "ls /var/test.dat"
     register: checkscript
   - name:
     fail:
       msg: "script {{ scriptdet }} missing on {{ inventory_hostname }}"
     when: checkscript.rc != 0

- name: Play 2- Run scripts
  hosts: all_hosts
  user: "{{ USER }}"
  strategy: free
  tasks:
   - name: Execute backup script
     shell: "{{ scriptdet }}"
     args:
       chdir: ~/..

我尝试了上述剧本,但尽管第一部剧本的任务失败,但我看到第二部剧本仍在执行。

您能建议我如何使它工作?

parallel-processing ansible ansible-2.x acid-state
1个回答
0
投票

除非any_errors_fatal为true-Ansible将在所有主机上完成运行-即使某些其他主机发生故障。但是,您发现any_errors_fatal不适用于免费策略。

It's also impossible to create a global variable.

但是,您提到要使用自由策略来加快执行速度。但是,自由策略只会阻止"blocking new tasks for hosts that have already finished."

如果需要执行同一任务而不等待其他主机完成,则需要并行处理。策略与并行性无关。

在Ansible中,您可以使用forks调整并行主机的数量。但是默认值很低,and is only 5.

您想要将这个数字设置得更高-取决于正在执行的主机资源。根据我对大量CPU和内存的经验,可以将其调整为数百个。

您可以在ansible.cfg or in the command line中设置:

ansible.cfg

这样,您可以使用默认线性策略,将any_errors_fatal设置为true。

© www.soinside.com 2019 - 2024. All rights reserved.