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?
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.
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.
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” ?
(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.
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