# When more than one play in a playbook, how to define an order for running one before others

**URL:** <https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827>\
**Category:** Ansible Project\
**Created:** [October 19, 2016, 11:21am UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827 "2016-10-19T11:21:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [October 19, 2016, 11:21am UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827/1 "2016-10-19T11:21:55Z")

</div>

Hello,

in my inventory file, I will use aliases to deploy differents applications for the same user:

[reference]  
refweb1 PARAM=“[‘tomweb’,‘7.30.05005.1334’,‘1’]”  
refqry1 PARAM=“[‘query’,‘7.30.05005.1334’,‘1’]”  
refweb2 PARAM=“[‘restart’,‘1’]”

I need to deploy the query applicatio before yje tomweb application, so in my playbook I wrote 2 plays:  
the one I want to play first will run the role this way:

roles:

- { role: tomweb, when: module == ‘tomweb’ }

vars:  
module: “{{ PARAM[0] }}”

the play I want to run after the 1st one is then defined later with this rôle:

roles:

- { role: query, when: module == ‘query’ }

vars:  
module: “{{ PARAM[0] }}”

The last play will be like:

roles:

- { role: restart, when: module == ‘restart’ }

vars:  
module: “{{ PARAM[0] }}”

This way to proceed do work but I have to duplicate all pre\_tasks and variables, is there a way to say I want to run for sure the tomweb role first if the line with this module is present in the inventory file ?  
And so have only one play in my playbook ?

regards

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [October 24, 2016, 10:26am UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827/2 "2016-10-24T10:26:33Z")

</div>

Would the new directive strategy help if set in the first play?

hosts: all  
strategy: free  
roles:

- { role: tomweb, when: module == ‘tomweb’ }

vars:  
module: “{{ PARAM[0] }}”

would run this play and when done then run other plays, am I correct ?

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 24, 2016, 1:06pm UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827/3 "2016-10-24T13:06:35Z")

</div>

You can use a when: conditional against your role (if you don’t mind seeing lots of ‘skipped’ steps for each task in the role.

see [http://docs.ansible.com/ansible/playbooks\_conditionals.html#applying-when-to-roles-and-includes](http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes)

Jon

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [October 24, 2016, 1:15pm UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827/4 "2016-10-24T13:15:43Z")

</div>

Thank you but I already use a when condition on the role.  
But it won’t make the 1st play with the 1st role being run first

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 24, 2016, 1:38pm UTC](https://forum.ansible.com/t/when-more-than-one-play-in-a-playbook-how-to-define-an-order-for-running-one-before-others/22827/5 "2016-10-24T13:38:12Z")

</div>

Not sure I really understand but you can maybe use pre\_task before to set\_fact. And you can use multiple when: conditions if you need to.

Another way to solve this might be to use role dependencies:

[http://docs.ansible.com/ansible/playbooks\_roles.html#role-dependencies](http://docs.ansible.com/ansible/playbooks_roles.html#role-dependencies)

That way you could make tomweb role ‘depend’ on ‘query’ role, so the query role would run first before tomweb (if that’s what you want.

You might also want to consider using a handler to restart.

Also maybe think about using groups in your inventory

[ref-query]  
host1

[ref-tomweb]  
host1

then your playbook could have

hosts: ref-query  
roles:  
query

hosts: ref-tomweb  
roles:  
tomweb

hosts: all  
roles:  
restart

(restart could be a handler as well, so perhaps not necessary to be a separate role). Handlers are nice as they can be made to only do things (like restarts) if changes have occurred.

Or something like that.

I think it might be worth experimenting with how you organise your variables so that your playbooks run have to run roles against hosts - that way the playbooks don’t become complicated with lots of conditionals etc.

Hope this helps,

Jon
