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.

Forivin
  • 14,780
  • 27
  • 106
  • 199

1 Answers1

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.

shaps
  • 1,159
  • 6
  • 12
  • 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 Apr 14 '16 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 Apr 14 '16 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 Apr 14 '16 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 Apr 14 '16 at 15:29