Ansible role post tasks
Is it possible to have "post tasks" in a role? i.e tasks defined in a role, that would run at the end of the play?
For example, I have a common
role, which most other roles depend on. The common
role provides some basic stuff to avoid repetition, such as tweaks to yum config, etc.
I notice that playbooks have post_tasks
. I was wondering if it is possible to define post tasks in roles themselves?
Update: Quoting the Ansible documentation:
Handlers: Running Operations On Change
...
These ‘notify’ actions are triggered at the end of each block of tasks in a playbook, and will only be triggered once even if notified by multiple different tasks.
...
Roles are described later on. It’s worthwhile to point out that handlers are automatically processed between ‘pre_tasks’, ‘roles’, ‘tasks’, and ‘post_tasks’ sections.
...
Evidently this part of the documentation is either misleading, or down right lying (=there is an error in it). It appears that handlers are fired at the end of each play, between the tasks
and post_tasks
, as this following simple test shows:
- name: Handlers test
hosts: all
gather_facts: no
roles:
- some-role
# some-role depends on common
tasks:
- name: This a task
shell: /bin/true
post_tasks:
- name: This is a post task
shell: /bin/true
And the common role:
# tasks/main.yml
- name: Registering handler
shell: /bin/true
notify: this is a handler
#handlers/main.yml
- name: this is a handler
shell: /bin/true
(Since nobody provided an answer I can't accept one, but didn't feel right answering my own question with tips from the comments...)
Solution 1:
This behavior is caused by a bug in Ansible 1.9.x, it's currently tracked here: https://github.com/ansible/ansible/issues/12575