Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible playbook execute in this order: task, role, task, role, task

Tags:

ansible

Forgive my newbie question, but I would like to execute three tasks and use two roles in a playbook, in the order:

  1. task
  2. role
  3. task
  4. role
  5. task

This is what I have so far (task, role, task):

--- - name: Task Role Task   hosts: 127.0.0.1   connection: local   gather_facts: false    pre_tasks:    - name: Do this task first      foo:    roles:   - role: this role second     foo:    post_tasks:     - name: Do this task third      foo: 

Is this possible or should I be changing my tasks into roles?

like image 371
simon Avatar asked Jun 10 '15 17:06

simon


People also ask

In what order does Ansible playbook is executed?

Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.

In which order do tasks execute inside plays or roles?

When you execute a playbook, in each play, all roles will be executed first, followed by the tasks, top down in the order that they are written.


2 Answers

I recommend you create roles for post and pre tasks for you ansible.

Your site.yml must be some like this:

--- - hosts: localhost   remote_user: "{{remote_user}}"   sudo: yes   gather_facts: false   roles:     - pre     - main_role     - post 

in roles folder you must have three roles, pre, post and main_role.

like image 194
siavolt Avatar answered Sep 21 '22 13:09

siavolt


Each "hosts:"-block will be executed one by one. So you can use multiple blocks to force the desired execution order:

--- - hosts: 127.0.0.1   tasks:    - name: Do this task first  - hosts: 127.0.0.1   roles:   - role: this role second  - hosts: 127.0.0.1   tasks:    - name: Do this task third 
like image 31
Michael Wyraz Avatar answered Sep 22 '22 13:09

Michael Wyraz