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

Hello!

I have the following group var in group_vars/staging:

jboss_master_host: 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

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

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