Ansible - Invoking tasks after roles using post_tasks

When using the roles plugin, tasks will be performed in this order:

  • pre_tasks
  • If the pre_tasks include the notify module, the handlers will be run. 
  • If the role contains any dependencies, the dependencies will be run.
  • tasks
  • If the tasks include the notify module, the handlers will be run. 
  • post_tasks
  • If the post_tasks include the notify module, the handlers will be run. 

Take for example the following playbook. Notice the post_tasks modules appears first, then the roles module, then the pre_tasks module. 

---
- hosts: all

  post_tasks:
    - name: this is a post task
      debug:
        msg: "this is a post task"

  roles:
    - foo

  pre_tasks:
    - name: this is a pre task
      debug:
        msg: "this is a pre task"

 

However, when this playbook is run, the pre_tasks will be invoked first, then the tasks, and finally the post tasks, like this.

TASK [this is a pre task]
ok: [server1.example.com] => {
    "msg": "this is a pre task"
}

TASK [foo : this is a task] 
ok: [server1.example.com] => {
    "msg": "this is a task"
}

TASK [this is a post task]
ok: [server1.example.com] => {
    "msg": "this is a post task"
}


 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 18195 in the box below so that we can be sure you are a human.