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

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:
jboss_master_host: barad-dur.example.com


 


---
# 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

Tags: DevOps


Copyright © 2024 Jakub Holý
Powered by Cryogen
Theme by KingMob