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

220 views
Skip to first unread message

Jakub Holy

unread,
Mar 18, 2014, 12:32:35 PM3/18/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Mar 18, 2014, 12:45:20 PM3/18/14
to ansible...@googlegroups.com
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


--
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/19f18258-213d-4117-a886-7096714d2ea3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jakub Holy

unread,
Mar 19, 2014, 4:04:14 AM3/19/14
to ansible...@googlegroups.com
Thanks a lot, Michael! I haven't thought of "when" at all. I'll use this.
Reply all
Reply to author
Forward
0 new messages