0

I have a dynamic inventory for my project and on deployment the host_groups get loaded with inventory list. Meantime, I want to set a variable named master_ip with value from host_vars [master] in a role/defaults/main.yml.

My hosts are grouped as below:

[master] 
xx.eco.project.com

[slave]
yy.eco.project.com
zz.eco.project.com

As i cannot use if/else in YAML. Like in templates i use,

{% if inventory_hostname in groups['master'] %}
 master_ip: {{ lookup('dig', inventory_hostname) }}

as master_ip: 10.0.1.1. How is it possible?

mjm
  • 723
  • 3
  • 6
  • 16

1 Answers1

1

I have solved this issue by defining a macro function in templates to determine the master and return the value to the template variables for master_ip. I find this more meaningful than the initial idea of getting the value in defaults/main.yml .

{% macro master_ip() -%}
    {% for inventory_hostname in groups['master'] -%}
        {{ lookup('dig', inventory_hostname) }}
    {%- endfor -%}
{% endmacro -%}

Use in template

bind {{ master_ip() }}
mjm
  • 723
  • 3
  • 6
  • 16