Hello you all!
I’m using Ansible to configure some Linux services on different Linux machines and I need to set the services precedence depending on the host’s group.
To be clear, I need to add three Linux services A.service, B.service and C.service.
The A.service must be added only on the hosts of the host group A, the B.service must be added only on the hosts of the host group B and the C.service must be added only on the hosts of the host group C.
In case a single host belongs to more than one group we have to install the corresponding services…
…BUT there is a precedence in the services done through the After configuration of the systemd service file.
A.service
After=network.target [..]
B.service
After=A.service [..]
C.service
`
After=B.service
[…]
`
So if on a single host we have more than one service configured we should set the right precedence with the available services; moreover if an host belongs to the A and C groups, the C.service cannot depend on B.service (which will not be available on that machine) but on the A.service.
I was wandering how to implement this configuration in Ansible and thought to use a service template like this:
C.service
After={{ C_after }} [..]
The C_after template should be set to:
-
network.target if the current host belongs only to the C group
-
A.service if the current host belongs to the A and C groups
-
B.service if the current host belongs to the B and C groups
Is there any way to set the C_after and B_after templates with this complex logic based on the host’s group? I would prefer to not use an infinite set of when statements checking the host’s groups and setting the templates as facts.
Thank you
Regards