Ansible multiple roles in a block with rescue/fail

552 views
Skip to first unread message

smitconsu...@gmail.com

unread,
Feb 22, 2018, 4:34:28 PM2/22/18
to Ansible Project

Hi,

I am trying to create a master playbook where I am including multiple roles in it. I am using block for each role in playbook along with rescue and fail section. Rescue section should perform rollback of the previous completed role and should fail playbook after rolling back and email failed details. I tried rescue section and it works but playbook does not fail and continue to next role. I tried fail inside rescue section that does not work as well. I can create fail task after completion of every block to fail the playbook but not sure how to register previous role's results. Can anyone help how to achieve this? Also, for email I was thinking to register the output of block in a var and send as email but register does not work with block. I appreciate your help. Thanks


- hosts: all

  gather_facts: false

  vars:

  pre_tasks:

    - include_vars: 'vars.yml' 

    - include_vars: 'vault1.yml' 

  tasks: 

- name Role1

  -  include_role:
      name: role1

- name: Role2

 block:
  - include_role:

     name: role2

 vars:

    vm: create
 # - fail: 

  #  msg: "Error: Role1 failed.” 

rescue: 
 - fail: 
 - name: Debug msg 
   debug: msg="Error:Role 1 failed..Rolling back role1."

 - include_role:

   name: remove_role1
Reply all
Reply to author
Forward
0 new messages