The Holy Java

Building the right thing, building it right, fast

Ansible: Best practices for deriving host-level var from a group var

Posted by Jakub Holý on March 19, 2014

I have a cluster and a group variable (in test/staging/prod) holding the hostname of the cluster master. For each host, I want to derive a variable which is set to either “slave” or “master” and use it in templates.

This can be done with set_fact:

# group_vars/staging:


# roles/xxx/tasks/main.yml:
- name: Set default jboss_host_type
  set_fact: jboss_host_type=slave
- name: Set jboss_host_type to master if master
  set_fact: jboss_host_type=master
  when: jboss_master_host == inventory_hostname

We could simplify that by using more advanced Jinja:

# roles/xxx/tasks/main.yml:
- name: Set jboss_host_type var
  set_fact: jboss_host_type={{ 'master' if jboss_master_host == inventory_hostname else 'slave' }}

but it is preferred not to use logic coded in Jinja2.

// Ansible 1.5.3

Sorry, the comment form is closed at this time.

%d bloggers like this: