# What is the best way to run a playbook after provisioning?

**URL:** <https://forum.ansible.com/t/what-is-the-best-way-to-run-a-playbook-after-provisioning/14930>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 11:59pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-run-a-playbook-after-provisioning/14930 "2014-04-02T23:59:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Lieberman](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@Michael\_Lieberman](https://forum.ansible.com/u/Michael_Lieberman)\
**Post date:** [April 2, 2014, 11:59pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-run-a-playbook-after-provisioning/14930/1 "2014-04-02T23:59:10Z")

</div>

I have a provision playbook: [https://gist.github.com/mlieberman85/c144fcc384cfcdc992d8](https://gist.github.com/mlieberman85/c144fcc384cfcdc992d8) and the last line of it doesn’t work because roles can’t be variables I guess.

Also there’s no way to conditionally include playbooks only task files. What is the best practice to have a single provision set of tasks that runs locally to provision an instance and then based off of variables runs a role on the spun up instance(s)?

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [April 3, 2014, 4:51pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-run-a-playbook-after-provisioning/14930/2 "2014-04-03T16:51:26Z")

</div>

Hi Michael,

I think the way you’d want to do this is to provision your machines, then use the add\_hosts (or the group\_by) module to dynamically create groups of servers based on variable data . Then, create playbooks that target each group. So, for example, you create a series of web servers, add them to a group called _web_ with [add\_host](http://docs.ansible.com/add_host_module.html) or [group\_by](http://docs.ansible.com/group_by_module.html), then target the group _‘web’_ in a playbook using include statements. If there are hosts in the web group, the playbook will run. For included playbooks that have no hosts for groups that are targeted, the playbook will be skipped.

I do something like this, albeit on a very small scale, in a sample playbook I wrote to spin up a cloud instance, wait for it to come online, then install Docker on it. [https://github.com/angstwad/ansible-docker-rackspace/blob/master/playbooks/rax-servers.yml](https://github.com/angstwad/ansible-docker-rackspace/blob/master/playbooks/rax-servers.yml)

If you look at that play, the second task is adding the server created in the first task to a group called “dockergroup”. I can then later do work on that group by creating a play that targets “hosts: dockergroup”.

Hope that helps.
