# How to use set\_fact to set a new host fact based on when conditionals.

**URL:** <https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907>\
**Category:** Ansible Project\
**Created:** [July 6, 2017, 6:20am UTC](https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907 "2017-07-06T06:20:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Lien](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Alex\_Lien](https://forum.ansible.com/u/Alex_Lien)\
**Post date:** [July 6, 2017, 6:20am UTC](https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907/1 "2017-07-06T06:20:29Z")

</div>

Hi all,

I am trying to set a new host fact (is\_master, is\_slave) based on comparison of a pre-assigned variable and ansible\_hostname fact.

group\_vars/

master\_hostname: host1

slave\_hostname: host2

Tasks

- name: Set master host fact

set\_fact:

is\_master: yes

when: master\_hostname == ansible\_hostname

- name: Set slave host fact

set\_fact:

is\_slave: yes

when: slave\_hostname == ansible\_hostname

- name: copy master configuration file

template:

src: master.j2

dest: /etc/master.conf

when: is\_master == True

- name: copy slave configuration file

template:

src: slave.j2

dest: /etc/slave.conf

when: is\_slave == True

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [host1]

ok: [host2]

TASK [Set is\_master variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [host1]

skipping: [host1]

TASK [Set is\_slave variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

skipping: [host2]

ok: [host2]

The goal I am trying to reach is to run certain tasks depending whether the host is master or slave. But these set\_fact tasks always get skipped even though condition should be true. Really appreciate your help in pointing out what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [July 6, 2017, 8:16am UTC](https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907/2 "2017-07-06T08:16:45Z")

</div>

You’re most likely after inventory\_hostname variable (not ansible\_hostname).

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 6, 2017, 1:06pm UTC](https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907/3 "2017-07-06T13:06:44Z")

</div>

using set\_fact that way requires you to check if those vars are  
defined, i believe this is a better approach:

vars:  
&nbsp;&nbsp;is\_master: "{{ inventory\_hostname == master\_hostname }}"  
&nbsp;&nbsp;is\_slave: "{{ inventory\_hostname == master\_hostname }}"

then you can just use them direcly in subsequent conditionals:

when: is\_master|bool

There are other approaches as well, i.e using group\_by, but I think  
the one above should work best for you.

---

<div class="post-metadata">

**Author:** ![Alex\_Lien](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Alex\_Lien](https://forum.ansible.com/u/Alex_Lien)\
**Post date:** [July 6, 2017, 11:36pm UTC](https://forum.ansible.com/t/how-to-use-set-fact-to-set-a-new-host-fact-based-on-when-conditionals/24907/4 "2017-07-06T23:36:15Z")

</div>

Hi Brian,

I made the changes based on your suggestions and they work fine. Thank You very much for your help.

Regards,  
Alex
