do post_tasks execute when a role fails?

2,840 views
Skip to first unread message

sergio....@mojix.com

unread,
Dec 1, 2016, 11:16:17 AM12/1/16
to Ansible Project
Hi, I created a role that has a rescue block where role is marked as failed:

--- #myrole
- block:
 
...
 
rescue:
   
- fail: msg='some message' #mark as fail ansible_failed_task is defined


On my playbook the idea is to send notification if ansible_failed_task is defined that's why "myrole" fails on rescue section. So on my playbook if that variable is defined I send a notification:


- hosts: automation
  gather_facts: True

  post_tasks:
    - include: notification.yml status=unstable action="Tests unstable"
      when: ansible_failed_task is defined

  roles:
    - { role: myrole, tags: tests}


When I run this playbook and "myrole" fails post_tasks section is never executed, so my question is that: Will post_tasks execute no matter whether role has failed or not? if not what should I use so post_tasks get executed no matter role's result?

Thanks in advance

Brian Coca

unread,
Dec 1, 2016, 3:10:36 PM12/1/16
to ansible...@googlegroups.com
To clarify as the question indicates some misunderstanding, roles can only fail on import, what is failing is a task (which happens to be in a role)​.

Tasks in post_tasks, are just like any other task, the only difference is ordering,  they get executed after all previous tasks/role sections.

Any normal task won't execute for a host if that host is in a failed state.

There is only one way to execute tasks after a failed state is via locating them inside the 'rescue' section of a block which contains the failing task. 

A different approach is negating the fail state to begin with, using failed_when or ignore_errors.


----------
Brian Coca

Brian Coca

unread,
Dec 1, 2016, 3:12:20 PM12/1/16
to ansible...@googlegroups.com
I forgot, there is also a 'always' section to blocks that runs tasks no matter the previous state.


----------
Brian Coca

Kai Stian Olstad

unread,
Dec 1, 2016, 3:13:52 PM12/1/16
to ansible...@googlegroups.com
On 01. des. 2016 14:31, sergio....@mojix.com wrote:
> Hi, I created a role that has a rescue block where role is marked as failed:
>
> --- #myrole
> - block:
> ...
> rescue:
> - fail: msg='some message' #mark as fail ansible_failed_task is defined

When ever Ansible run the fail module, it will immediately stop, no
other task will be executed for that host.


> On my playbook the idea is to send notification if ansible_failed_task is
> defined that's why "myrole" fails on rescue section. So on my playbook if
> that variable is defined I send a notification:
>
>
> - hosts: automation
> gather_facts: True
>
> post_tasks:
> - include: notification.yml status=unstable action="Tests unstable"
> when: ansible_failed_task is defined
>
> roles:
> - { role: myrole, tags: tests}
>
>
> When I run this playbook and "myrole" fails post_tasks section is never
> executed, so my question is that: Will post_tasks execute no matter whether
> role has failed or not? if not what should I use so post_tasks get executed
> no matter role's result?

The post_tasks is just a way of group tasks to be run at some sequence.
In a play you can have the following and they will run in the order they
are listed bellow
1. pre_tasks
2. roles
3. tasks
4. post_tasks

In your example when it hits fail: it will stop an no more task will be
executed.
So the rest of the roles section, tasks and post_tasks will not run for
that host.

--
Kai Stian Olstad

ssba...@redhat.com

unread,
Aug 31, 2017, 10:36:13 AM8/31/17
to Ansible Project
Sorry to revive this old thread but I (and many others) got the same problem which seems not to be addressed yet. My original post was at https://stackoverflow.com/questions/45983636/how-to-always-run-some-ansible-roles-after-previous-failures

If you you wrote is still true (ansible 2.3.x), it means that ansible *roles* are not really as useful as they were marketed as because once you get a failure inside a role it is impossible to run some other code after.

This "design limitation" is forcing people to abuse the use of "ignore_errors" in order to still be able to do something after the failure, ending up with tons of roles full of ignore_errors which we all know how nice they look in the console logs, where they are NOT colored red, hiding the original error.

So how are we supposed to re-use roles when is impossible to use them? Starting to replace role: foo with something like include: "../roles/foo/tasks/main.yaml" ?

Josh Smift

unread,
Sep 3, 2017, 8:21:01 AM9/3/17
to ansible...@googlegroups.com
Does 'meta: clear_host_errors' help in this situation?
http://docs.ansible.com/ansible/latest/playbooks_error_handling.html#resetting-unreachable-hosts
only mentions this as affecting unreachable hosts, but
http://docs.ansible.com/ansible/latest/meta_module.html mentions that it
clears the failed state in general.

-Josh (j...@care.com)

(apologies for the automatic corporate disclaimer that follows)
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

Niels Bertram

unread,
Dec 8, 2017, 4:52:27 PM12/8/17
to Ansible Project
Hmm if I had to develop specific error handling around roles, I would use the block statement. Have not been able to come up with something that I could not handle. N

tasks:
  - block:
      - include_role:
          name: myrole-that-fails
    rescue:
      - debug: msg="Help me, I just melted"
    always:
      - debug: msg="I run no matter what"
Reply all
Reply to author
Forward
0 new messages