向剧本中添加一个任务,即使其他角色失败也必须运行。

huangapple go评论69阅读模式
英文:

Adding a task to a playbook which must run even when other roles failed

问题

我有以下的剧本:

---
- hosts: "{{ target }}"
  gather_facts: no

  roles:
   - role1
   - role2

  tasks:
    - block:
        - name: Role3
          ansible.builtin.include_role:
            name: role3
      rescue:
        - meta: clear_host_errors

    - ansible.builtin.include_role:
        name: final-role

我期望在这里实现以下目标:

  • 如果 role1 或 role2 失败,就无需运行 final-role。
  • 如果 role 3 失败,我想要运行 final-role(强制性的)。
  • 如果 role 3 失败,当执行剧本后(也就是在运行 final-role 后),它应该返回 FAILED 状态(就像如果 role3 失败且不在 block 内运行时一样)。

前两个目标我已经实现了。
然而,如何在运行 final-role 后引起播放失败,如果 role3 在此之前失败,哪种方法最好呢?

英文:

I have the following playbook:

---
- hosts: "{{ target }}"
  gather_facts: no

  roles:
   - role1
   - role2

  tasks:
    - block:
        - name: Role3
          ansible.builtin.include_role:
            name: role3
      rescue:
        - meta: clear_host_errors

    - ansible.builtin.include_role:
        name: final-role

I was expecting to achieve the following here:

  • If role1 or role2 fails, there is no need to run final-role.
  • If role 3 fails, I want to run final-role (mandatory).
  • If role 3 fails, when finishing executing the playbook (this is, after running final-role), it should return FAILED status (as it would do if role3 failed and if it was not running inside the block).

The first 2 I achieved them already.
However what would be the best way to cause the play to FAIL after running-final role, if role3 failed before?

答案1

得分: 2

只有在role3失败时才运行final_role

    - block:
        - name: 运行role3
          ansible.builtin.include_role:
            name: role3

      rescue:
        - meta: 清除主机错误

        - name: 在失败时运行最终角色
          ansible.builtin.include_role:
            name: final_role
       
        - name: 在回滚和修复后报告失败
          ansible.builtin.fail:
            msg: role3失败

始终运行final_role,并在role3失败时报告最终失败

    - block:
        - name: 运行role3
          ansible.builtin.include_role:
            name: role3

      rescue:
        - meta: 清除主机错误

        - name: 注册失败
          ansible.builtin.set_fact:
            role3_failed: true

      always:
        - name: 运行最终角色
          ansible.builtin.include_role:
            name: final_role
       
        - name: 如果需要,报告失败
          ansible.builtin.fail:
            msg: role3失败
          when: role3_failed | d(false) | bool
英文:

I did not really get if you want to run final_role only if role3 failed or always so here are the two options:

Notes:

  • although dash (-) is not strictly forbidden for role names, it will trigger errors in tools like ansible-lint and will be unconditionnaly transformed to _ when uploading to galaxy. So I renamed final-role to final_role in the below examples as a good practice
  • I'm not really sure why you use clear_host_errors in your playbook and left it untouched. But it is not mandatory/useful for the current issue

final_role only on failure:

    - block:
        - name: Run role3
          ansible.builtin.include_role:
            name: role3

      rescue:
        - meta: clear_host_errors

        - name: Run final role as we failed
          ansible.builtin.include_role:
            name: final_role
       
        - name: Report failure after rollback and remedy
          ansible.builtin.fail:
            msg: role3 failed

final_role always and final failure if role3 failed:

    - block:
        - name: Run role3
          ansible.builtin.include_role:
            name: role3

      rescue:
        - meta: clear_host_errors

        - name: Register failure
          ansible.builtin.set_fact:
            role3_failed: true

      always:
        - name: Run final role
          ansible.builtin.include_role:
            name: final_role
       
        - name: Report failure if needed
          ansible.builtin.fail:
            msg: role3 failed
          when: role3_failed | d(false) | bool

huangapple
  • 本文由 发表于 2023年6月19日 23:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定