我有一个有趣的剧本,像这样
---
- hosts: localhost
connection: local
tasks:
- set_fact:
build_date_time: "{{ ansible_date_time }}"
- hosts: localhost
connection: local
gather_facts: false
vars:
role: Appvariables
base_image_tag: Base
roles:
- role: begin-building-ami
- hosts: just_created
remote_user: "{{ default_user }}"
vars:
role: AppName
roles:
- { role: role1, become: yes }
- role: role2
- { role: role3, become: yes }
- { role: role4, become: yes }
- { role: role5, become: yes }
- hosts: localhost
connection: local
gather_facts: false
vars:
role: Appname
ansible_date_time: "{{ build_date_time }}"
roles:
- finish-building-ami
在这种情况下,我们有一种情况可以执行finish-building-ami角色,即在烘焙ami之后终止实例。如果任何以前的role1-role5中的任何一个失败,则流程失败它将停止剧本,并且我们有需要自动终止的失败实例。现在,我们将继续操作,如果失败则手动终止。
因此,即使在上述剧本中任何一个role1-role5都失败了,也需要运行finish-building-ami(强制性角色,在该角色中我们停止该实例,并采取ami并最终终止该实例)。
答案 0 :(得分:1)
您可以重写现有的剧本以使用import_role
或include_role
任务而不是roles
部分。这使您可以使用块:
---
- hosts: localhost
gather_facts: false
tasks:
- block:
- import_role:
name: role1
- import_role:
name: role2
become: true
- import_role:
name: role3
rescue:
- set_fact:
role_failed: true
- hosts: localhost
gather_facts: false
tasks:
- debug:
msg: This task runs after our roles.