# loading tasks file based on ansible\_fqdn value

**URL:** <https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179>\
**Category:** Ansible Project\
**Created:** [July 2, 2023, 3:12pm UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179 "2023-07-02T15:12:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 2, 2023, 3:12pm UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/1 "2023-07-02T15:12:33Z")

</div>

hi,

I want to take different sligthly routes in a playbook based on the particular remote. All remotes are in the same hosts group.  
I am trying to do this by setting a fact based on the ansible\_fqdn value.  
Based on that fact ('keycloak' in my example here) I am including different tasks files.

these 2 options do not work though:

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 2, 2023, 5:56pm UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/2 "2023-07-02T17:56:36Z")

</div>

There’s a problem with your “when:” conditionals.

First of all, throw out the second, because the strings that are “when:” expressions are already being evaluated in a Jinja2 context, so the mustaches shouldn’t be there.

In the first option, on this line:

```auto
  when: ansible_fqdn == VM-0213.step.zrz.internal

```

the “ansible\_fqdn” part is okay, but the expression on the right-hand side “==” should be a string. Instead it’s the value of the variable “VM” minus the octal number 0213’s missing attribute “step”'s missing attribute “zrz”'s missing attribute “internal”.

So, yeah, no wonder that doesn’t work.

Try this instead:

```auto
  when: ansible_fqdn == "VM-0213.step.zrz.internal"

```

and let us know how you get on.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 2, 2023, 6:03pm UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/3 "2023-07-02T18:03:45Z")

</div>

Or, better yet in my opinion, set the default value for “level” in group\_vars/\<name\_of\_group\>.yml, then for hosts which need a different “level” set that in an appropriately named host\_vars file, like host\_vars/VM-0213.step.zrz.internal.yml.

This let’s you expresses facts about hosts as data rather than as results of expressions in set\_fact or other tasks, task which you no longer need to run.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 3, 2023, 6:10am UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/4 "2023-07-03T06:10:46Z")

</div>

when: ansible\_fqdn == “VM-0213.step.zrz.internal”

does not create any error in the set\_fact TASK. It does not set anything either unfortunately. Which, thinking of it, seems logical as we just compare the string ansible\_fqdn with another string and not the value of the variale {{ ansible\_fqdn }}. so that seems to be a dead end.

the second option seems too complex for my current scenario. I am trying to gather all variables into a singe place. And having to create a host\_vars/.yml file for each host on each execution contradicts that a bit too much.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 3, 2023, 8:55am UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/5 "2023-07-03T08:55:46Z")

</div>

I could actually manage this with:

- ansible.builtin.set\_fact:  
level: keycloak  
when "‘VM-0312.step.zrz.internal’ in inventory\_hostname’

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 3, 2023, 9:34am UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/6 "2023-07-03T09:34:13Z")

</div>

> - ansible.builtin.set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;level: keycloak  
> &nbsp;&nbsp;when: ansible\_fqdn == VM-0312.step.zrz.internal

Make your live easier and put the logic into the \*vars\*. For example,

&nbsp;&nbsp;\> cat group\_vars/all/level.yml  
&nbsp;&nbsp;level\_dict:  
&nbsp;&nbsp;&nbsp;&nbsp;VM-0311.step.zrz.internal: passkey  
&nbsp;&nbsp;&nbsp;&nbsp;VM-0312.step.zrz.internal: keycloak  
&nbsp;&nbsp;&nbsp;&nbsp;VM-0313.step.zrz.internal: falsekey  
&nbsp;&nbsp;level: "{{ level\_dict[ansible\_fqdn] }}"

Given the inventory for testing

&nbsp;&nbsp;\> cat hosts  
&nbsp;&nbsp;host1 ansible\_fqdn=VM-0311.step.zrz.internal  
&nbsp;&nbsp;host2 ansible\_fqdn=VM-0312.step.zrz.internal  
&nbsp;&nbsp;host3 ansible\_fqdn=VM-0313.step.zrz.internal

use \*set\_fact" if you want to 'instantiate' the variable (put the  
variable into the \*hostvars\*). You can omit \*set\_fact\* if you don't  
need hostvars.\*.level. For example, the play

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level: "{{ level }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: level

gives (abridged)

&nbsp;&nbsp;ok: [host1] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;level: passkey  
&nbsp;&nbsp;ok: [host2] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;level: keycloak  
&nbsp;&nbsp;ok: [shot3] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;level: falsekey

As a result, the code is cleaner. Also the concentration of data into  
a single point of failure (a dictionary and assignment isolated in  
\*vars\*) makes the code more robust.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 3, 2023, 10:51am UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/7 "2023-07-03T10:51:48Z")

</div>

This indicates that your inventory\_hostname and your ansible\_fqdn are not the same, which can certainly be the case.

But Vladimir Botka is correct, and so is your intuition, that you should have all these variables in the same place. However, that place shouldn’t be in your tasks:.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 3, 2023, 10:57am UTC](https://forum.ansible.com/t/loading-tasks-file-based-on-ansible-fqdn-value/38179/8 "2023-07-03T10:57:00Z")

</div>

Rule of thumb: If you find you have to edit your task: sections / files to add, remove, or rename hosts, you’re doing it wrong.
