6

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...)

  • 1
    how closely does a handler fit what you are looking for? – chicks Sep 26 '15 at 14:37
  • A handler will run between the roles and tasks section of a playbook. I need something that runs after the tasks section, i'e like post_tasks. – Amir Abiri Sep 26 '15 at 19:11
  • 1
    handlers run after everything if there is a trigger. It sounds like you expect them not to fire after tasks, but they do. – chicks Sep 26 '15 at 19:14
  • 2
    Right, handlers run at the very end. – Michael Hampton Sep 26 '15 at 21:00
  • Sorry I don't understand - a handler defined and triggered in a role would run before or after the tasks section of the top-level playbook? – Amir Abiri Sep 27 '15 at 3:41
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.