包含返回码的剧本中的剧本

时间:2019-02-21 14:05:07

标签: ansible

我想在主剧本中加入playbook。但是,如果包含播放簿中的任何内容失败,则主播放簿应退出而不执行其中指定的其他任务。

MasterPlaybook.yml

    - include: playbook1.yml
    - include: playbook2.yml

    - name: copy file
      shell: echo "hello"

    - name: list other one
      shell: echo "Hi"

playbook1.yml

  - name: list task
    shell: ls /tmp/ | grep text.html


   - name: list file 
     shell: ls /root/ | grep text2.html

playbook2.yml

   - name: list task
     shell: ls /tmp/ | grep text.html


   - name: list file 
     shell: ls /root/ | grep text2.html

因此,在上面的示例中,如果任何任务在playbook1.yml中失败,则master中的其余任务不应执行

1 个答案:

答案 0 :(得分:2)

对于剧本,您可以使用any_errors_fatal: yes

对于任务,您可以使用rescuemeta: end_play

更新:

您有list of tasks,而不是剧本。因此,您的文件名具有误导性。

无论如何,您可能想要这样的东西:

- hosts: all
  tasks:
    - block:
        - include_tasks: file1.yml
      rescue:
        - meta: end_play
    - include_tasks: file2.yml
    - shell: echo ok

在这种情况下,如果file1.yml中的任何任务失败,则meta: end_play将触发以立即停止剧本。