How to use set_fact to set a new host fact based on when conditionals.

739 views
Skip to first unread message

Alex Lien

unread,
Jul 6, 2017, 2:20:30 AM7/6/17
to Ansible Project

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.

Pshem Kowalczyk

unread,
Jul 6, 2017, 4:17:03 AM7/6/17
to Ansible Project
You're most likely after inventory_hostname variable (not ansible_hostname).

kind regards
Pshem


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b4039197-4934-4bc4-94ef-40996b6fde06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Jul 6, 2017, 9:06:57 AM7/6/17
to Ansible Project
using set_fact that way requires you to check if those vars are
defined, i believe this is a better approach:

vars:
is_master: "{{ inventory_hostname == master_hostname }}"
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.
----------
Brian Coca

Alex Lien

unread,
Jul 6, 2017, 7:36:28 PM7/6/17
to ansible...@googlegroups.com
Hi Brian,

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

Regards,
Alex


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/697Ke5ficy0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7eMHd%2BWdX4fBtKwfHABZHkF8nf-sLYhu9GZTz3QEY3M7Q%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages