I’m building a “deployment” role similar to what Ansistrano does:
- Check out code
- Run Project-specific build steps
- Create archive
- Extract archive on target machine
- 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)?
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:
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.
- 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.
Instead of adding delegate_to on each task you can wrap them with block and just add deletegate_to to the block.