0

I've a role pkg_install, is there any way based on status of the role execution can we execute post_tasks? also ignore errors if role pkg_install failed?

- name: status file
  hosts: localhost
  gather_facts: false
  roles:
   - role: "pkg_install"
     register: status
     ignore_errors: true

  post_tasks
    - name: status DEBUG
      debug:
        msg: "pkg_install success"
      when: status is successful
  • You cannot register on a role declaration, only on a task inside that role. Moreover, by default, ansible will stop if something goes wrong inside your role. So your post_task will be played only if playing the role was successful. – Zeitounator May 21 '20 at 07:16

1 Answers1

1

note that using ignore_errors like in your code will be like putting ignore_errors on each task in the role so if any task in the role fails it will go on to the next one in the role and will do the post_tasks.

If you want the role to stop when it fails but the rest of the play to continue and know if the role failed, you can do something like:

- name: status file
  hosts: localhost
  gather_facts: false
  tasks:
    - block:
        - import_role: 
            name: "pkg_install"
      rescue:
        - set_fact:
            role_success: no
  post_tasks:
    - name: status DEBUG
      debug:
        msg: "pkg_install success"
      when: role_success|default('yes')|bool

The rescue tasks will only run if the role failed. here's the docs on blocks for more information https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html