# Complex fact setting in Ansible playbooks

**URL:** <https://forum.ansible.com/t/complex-fact-setting-in-ansible-playbooks/26992>\
**Category:** Ansible Project\
**Created:** [March 14, 2018, 9:08am UTC](https://forum.ansible.com/t/complex-fact-setting-in-ansible-playbooks/26992 "2018-03-14T09:08:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giulio\_Vito\_de\_Musso](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@Giulio\_Vito\_de\_Musso](https://forum.ansible.com/u/Giulio_Vito_de_Musso)\
**Post date:** [March 14, 2018, 9:08am UTC](https://forum.ansible.com/t/complex-fact-setting-in-ansible-playbooks/26992/1 "2018-03-14T09:08:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Giulio\_Vito\_de\_Musso](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@Giulio\_Vito\_de\_Musso](https://forum.ansible.com/u/Giulio_Vito_de_Musso)\
**Post date:** [March 15, 2018, 10:52am UTC](https://forum.ansible.com/t/complex-fact-setting-in-ansible-playbooks/26992/2 "2018-03-15T10:52:36Z")

</div>

Hi,

for future reference, I solved this issue by using the Jinja’s {% if %} construct to build the conditions between the services:

C.service

`  
{% set required\_service = “network.target” %}  
{% if ‘B\_group’ in group\_names %}  
{% set required\_service = “B.service” %}  
{% else %}  
{% if ‘A\_group’ in group\_names %}  
{% set required\_service = “A.service” %}  
{% endif %}  
{% endif %}  
[Unit]  
Description=C Service  
After={{ required\_service }}  
BindsTo={{ required\_service }}  
Requires={{ required\_service }}

[…]  
`

Regards  
Giulio
