# How to have a sequence of local tasks in a role

**URL:** <https://forum.ansible.com/t/how-to-have-a-sequence-of-local-tasks-in-a-role/24497>\
**Category:** Ansible Project\
**Created:** [May 16, 2017, 5:51pm UTC](https://forum.ansible.com/t/how-to-have-a-sequence-of-local-tasks-in-a-role/24497 "2017-05-16T17:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabriel\_Birke](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@Gabriel\_Birke](https://forum.ansible.com/u/Gabriel_Birke)\
**Post date:** [May 16, 2017, 5:51pm UTC](https://forum.ansible.com/t/how-to-have-a-sequence-of-local-tasks-in-a-role/24497/1 "2017-05-16T17:51:55Z")

</div>

I’m building a “deployment” role similar to what [Ansistrano](https://github.com/ansistrano/deploy) does:

1. Check out code
2. Run Project-specific build steps
3. Create archive
4. Extract archive on target machine
5. Symlink new folder on target machine

I’d like to write these steps as one role that can be included in a playbook, with project-specifc “hook” files that are included at certain points of the role. However, steps 1-3 should **not** be executed on the machine that is deployed to, but on the machine that runs the playbook. As far as I know “delegate\_to” does not work when using “include”. If I wrote this as a _playbook_, I’d just split steps 1-3 into a play that is executed on localhost and steps 4-5 in a play that is executed. But a _role_ has its tasks/main.yml as an entry point and no easy way to run different tasks on different machines. I have several ideas on how to solve this, but I don’t like any of them:

- Split the role into two, one “build” and one “deploy”, adapt the including playbooks to use plays that each include one role. It would feel weird, because the “deploy” role is kind of dependent on the “build” role without really bein able to specify that dependency.
- Branch inside the role, depending on a variable. Playbooks must have two plays that have that role, both with different variables.
- add a “delegate\_to” statement to each task that should be executed locally (steps 1-3 are at more than only 3 tasks). This is error-prone for the hook files where the tasks belonging to the “build” phase also would have to have “delegate\_to” statements with each task.

Any other way to achive my goal (preferrably with just one role)?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 17, 2017, 6:03pm UTC](https://forum.ansible.com/t/how-to-have-a-sequence-of-local-tasks-in-a-role/24497/2 "2017-05-17T18:03:50Z")

</div>

> I'm building a "deployment" role similar to what Ansistrano  
> \<[https://github.com/ansistrano/deploy&gt](https://github.com/ansistrano/deploy&gt); does:
> 
> &nbsp;&nbsp;&nbsp;1. Check out code  
> &nbsp;&nbsp;&nbsp;2. Run Project-specific build steps  
> &nbsp;&nbsp;&nbsp;3. Create archive  
> &nbsp;&nbsp;&nbsp;4. Extract archive on target machine  
> &nbsp;&nbsp;&nbsp;5. Symlink new folder on target machine
> 
> I'd like to write these steps as one role that can be included in a  
> playbook, with project-specifc "hook" files that are included at certain  
> points of the role. However, steps 1-3 should \*not\* be executed on the  
> machine that is deployed to, but on the machine that runs the playbook. As  
> far as I know "delegate\_to" does not work when using "include". If I wrote  
> this as a \*playbook\*, I'd just split steps 1-3 into a play that is executed  
> on localhost and steps 4-5 in a play that is executed. But a \*role\* has its  
> tasks/main.yml as an entry point and no easy way to run different tasks on  
> different machines. I have several ideas on how to solve this, but I don't  
> like any of them:

I don't understand this statement, task in a role can have delegate\_to.  
So it's easy to run task(s) on different hosts.

> &nbsp;&nbsp;&nbsp;- Split the role into two, one "build" and one "deploy", adapt the  
> &nbsp;&nbsp;&nbsp;including playbooks to use plays that each include one role. It would feel  
> &nbsp;&nbsp;&nbsp;weird, because the "deploy" role is kind of dependent on the "build" role  
> &nbsp;&nbsp;&nbsp;without really bein able to specify that dependency.  
> &nbsp;&nbsp;&nbsp;- Branch inside the role, depending on a variable. Playbooks must have  
> &nbsp;&nbsp;&nbsp;two plays that have that role, both with different variables.  
> &nbsp;&nbsp;&nbsp;- add a "delegate\_to" statement to each task that should be executed  
> &nbsp;&nbsp;&nbsp;locally (steps 1-3 are at more than only 3 tasks). This is error-prone for  
> &nbsp;&nbsp;&nbsp;the hook files where the tasks belonging to the "build" phase also would  
> &nbsp;&nbsp;&nbsp;have to have "delegate\_to" statements with each task.

Instead of adding delegate\_to on each task you can wrap them with block and just add deletegate\_to to the block.
