# Best practices for deriving host-level var from a group var?

**URL:** <https://forum.ansible.com/t/best-practices-for-deriving-host-level-var-from-a-group-var/14799>\
**Category:** Ansible Project\
**Created:** [March 18, 2014, 4:32pm UTC](https://forum.ansible.com/t/best-practices-for-deriving-host-level-var-from-a-group-var/14799 "2014-03-18T16:32:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Holy](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jakub\_Holy](https://forum.ansible.com/u/Jakub_Holy)\
**Post date:** [March 18, 2014, 4:32pm UTC](https://forum.ansible.com/t/best-practices-for-deriving-host-level-var-from-a-group-var/14799/1 "2014-03-18T16:32:35Z")

</div>

Hello!

I have the following group var in group\_vars/staging:

jboss\_master\_host: [barad-dur.example.com](http://barad-dur.example.com)

And I need to set, for each host in a play, its jboss\_host\_type = master | slave based on that variable - the jboss\_host\_type is used in multiple places in templates.

I have found the following solution, adding this to my tasks:

- name: Set jboss\_host\_type var  
set\_fact: jboss\_host\_type={{ ‘master’ if jboss\_master\_host == inventory\_hostname else ‘slave’ }}

The question is, is this the right way to do it? Is there a better way?

Thank you!

PS: Ansible 1.5.3

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 18, 2014, 4:45pm UTC](https://forum.ansible.com/t/best-practices-for-deriving-host-level-var-from-a-group-var/14799/2 "2014-03-18T16:45:20Z")

</div>

We generally dislike to see logic coded in Jinja2, but that will indeed function.

I’d be tempted to do this, personally, to remove the Jinja2 logic:

- set\_fact: jboss\_host\_type=slave

- set\_fact: jboss\_host\_type=master  
when: jboss\_master\_host == inventory\_hostname

---

<div class="post-metadata">

**Author:** ![Jakub\_Holy](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jakub\_Holy](https://forum.ansible.com/u/Jakub_Holy)\
**Post date:** [March 19, 2014, 8:04am UTC](https://forum.ansible.com/t/best-practices-for-deriving-host-level-var-from-a-group-var/14799/3 "2014-03-19T08:04:14Z")

</div>

Thanks a lot, Michael! I haven’t thought of “when” at all. I’ll use this.
