是否可以以某种方式使其在执行剧本后,最后打印失败角色的所有任务,但由于启用了 inore_errors 选项而执行了剧本? 角色必须包含在剧本中,如下所示:
- name: Playbook
hosts: all
vars_files:
- "../vars/passwords.yml"
roles:
- role: ansible-role-1
- role: ansible-role-2
我尝试使用 post_tasks 来做到这一点,并且我正在寻找它的集合(例如 ansible.posix 在任务执行时间结束时打印),但我还没有找到解决方案
您可以通过使用 Ansible 的阻止功能以及救援和始终阻止来实现此目的。以下是您可以修改剧本以在最后打印失败任务的方法:
- name: Playbook
hosts: all
vars_files:
- "../vars/passwords.yml"
tasks:
- name: Execute roles with ignore_errors
block:
- include_role:
name: ansible-role-1
ignore_errors: yes
- include_role:
name: ansible-role-2
ignore_errors: yes
rescue:
- debug:
msg: "Role {{ ansible_failed_task | default('Unknown Role') }} failed!"
post_tasks:
- name: Print failed tasks
debug:
msg: "Failed task: {{ item }}"
loop: "{{ ansible_playbook_failed_tasks | map(attribute='task_action') | list }}"
when: ansible_playbook_failed_tasks is defined