0

I have 4 playbooks. 2 of them are deploying services on my target machines and 2 of them are removing them again. Now I want to put them in roles. But I'm not sure what the best-practice is.
The 2 deploy playbooks are doing the exact same thing only with different variables and templates. Same applies to the remove playbooks.

Atm my structure looks like this:

ansible.cfg
ssh_key
inventoryfile
group_vars
    ....
roles
    deployservicegroupA
       vars
           ...
       templates
           ...
       tasks
           main.yml (this file simply includes the two tasks right below)
           copy-service-templates.yml
           start-services.yml
    deployservicegroupB
       vars
           ...
       templates
           ...
       tasks
           main.yml (this file simply includes the two tasks right below)
           copy-service-templates.yml
           start-services.yml
    removeservicegroupA
       vars
           ...
       templates
           ...
       tasks
           main.yml (this file simply includes the two tasks right below)
           remove-services.yml
           cleanup.yml
    removeservicegroupB
       vars
           ...
       templates
           ...
       tasks
           main.yml (this file simply includes the two tasks right below)
           remove-services.yml
           cleanup.yml

Is this they way it was intended to be done by users?
I'm especially wondering about my tasks that do the exact same thing, but can be found in different roles. And also if I should include my tasks in the main.yml task file.

2
  • Are you running the roles on different groups or a single group ( either deploy or remove )?
    – shaps
    Commented Apr 14, 2016 at 11:42
  • There is a group for every service to specify on which machines they should be deployed.
    – Forivin
    Commented Apr 14, 2016 at 13:56

1 Answer 1

2

As per your comment you are using a group for each service, you can use group_vars to specify the variables you want to use.

You can then merge the roles together, the only thing you will have to do is load specific templates based on the group you are running your play on.

4
  • So you would suggest to use one role instead of multiple ones even though the services have absolutely nothing to do with each other?
    – Forivin
    Commented Apr 14, 2016 at 14:48
  • What I meant here is that this is an option. To me it makes perfect sense to have different roles for different services ( even though you do the same thing ), as I see a role as a standalone entity ( a service, if you want ). If you're bothered about the fact you will have to maintain 2x roles then nothing stops you from using a single, more generic role which can handle both the services
    – shaps
    Commented Apr 14, 2016 at 15:16
  • Well, I just thought that there might be some way to defining a task more globally (like a library in programming) so that multiple roles can make use of it. I'm just not sure if that would fit into the "best practices".
    – Forivin
    Commented Apr 14, 2016 at 15:21
  • I don't really think it would fit into the best practices, as a role should be a well defined entity, that you can share and reuse, so it is in fact a library, but a very specific one :)
    – shaps
    Commented Apr 14, 2016 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.