I am writing an ansible playbook for installation of consul and nomad. Each of them can be either a client, or a server, or not installed at all.
What is the recommended way to represent which action should be done on which hosts in ansible?
- Using groups and roles? This causes high duplication between templates, as the client and server share the same configuration. Additionally, nomad configuration depends on consul, I do not know if I should check a variable in nomad template or if a host is in a group?
roles/
nomad_client/tasks/main.yml
nomad_server/tasks/main.yml
nomad_remove/tasks/main.yml
consul_client/tasks/main.yml
consul_server/tasks/main.yml
consul_remove/tasks/main.yml
all:
nomad_clients:
hosts:
a:
b:
nomad_servers:
hosts:
c:
d:
nomad_remove:
hosts:
e:
f:
/* etc with consul */
- Using variables with different states?
roles/
nomad/
tasks/
main.yml
^- include_tasks: '{{"nomad_remove.yml" if nomad_remove else "nomad.yml" if nomad_server_enable or nomad_client_enable else "error"}}'
nomad.yml
nomad_remove.yml
consul.yml
consul_remove.yml
all:
clients:
hosts:
a:
b:
vars:
nomad_client_enable: 1
nomad_server_enable: 0
nomad_remove: 0
consul_client_enable: 1
consul_server_enable: 0
consul_remove: 0
servers:
hosts:
c:
d:
vars:
nomad_client_enable: 1
nomad_server_enable: 1
nomad_remove: 0
consul_client_enable: 1
consul_server_enable: 1
consul_remove: 0
no_nomad:
hosts:
e:
f:
vars:
nomad_client_enable: 0
nomad_server_enable: 0
nomad_remove: 1
consul_client_enable: 0
consul_server_enable: 0
consul_remove: 1
Which design is easier to maintain and operate on? Additionally, nomad configuration depends on if consul is enabled or not. In templates when enabling a block of code, should I check a variable in role {% if consul_enable %}
or check hostname association {% if inventory_hostname in groups.consul_clients %}
? Which one is more commonly used, is there a difference?
Or it is all opinion based and cosmetics?