Ansible 获取其他组变量
Ansible get other group vars
我正在更深入地研究 Ansible 功能,我希望以一种漂亮的方式实现 VIP 的概念。
为此,我在清单的 group_vars
中实现了这个变量:
group_vars/firstcluster:
vips:
- name: cluster1_vip
ip: 1.2.3.4
- name: cluster1.othervip
ip: 1.2.3.5
group_vars/secondcluster:
vips:
- name: cluster2_vip
ip: 1.2.4.4
- name: cluster2.othervip
ip: 1.2.4.5
并在清单中:
[firstcluster]
node10
node11
[secondcluster]
node20
node21
我的问题:如果我想设置一个 DNS 服务器,收集所有 VIP 和相关名称(没有多余的美学冗余),我该如何进行?简而言之:除了下面的主机之外,是否有可能获得所有组变量?
喜欢:
{% for group in <THEMAGICVAR> %}
{% for vip in group.vips %}
{{ vip.name }} IN A {{ vip.ip }}
{% end for %}
{% end for %}
我认为您不能直接访问任何组的变量,但是您可以访问组主机,并且可以从主机访问变量。所以遍历所有组,然后只选择每个组的第一个主机就可以了。
您要查找的魔术变量是 groups
。同样重要的是 hostvars
.
{%- for group in groups -%}
{%- for host in groups[group] -%}
{%- if loop.first -%}
{%- if "vips" in hostvars[host] -%}
{%- for vip in hostvars[host].vips %}
{{ vip.name }} IN A {{ vip.ip }}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
文档:Magic Variables, and How To Access Information About Other Hosts
如果主机属于多个组,您可能需要过滤重复的条目。在这种情况下,您需要先收集字典中的所有值,然后像这样在单独的循环中输出它:
{% set vips = {} %} {# we store all unique vips in this dict #}
{%- for group in groups -%}
{%- for host in groups[group] -%}
{%- if loop.first -%}
{%- if "vips" in hostvars[host] -%}
{%- for vip in hostvars[host].vips -%}
{%- set _dummy = vips.update({vip.name:vip.ip}) -%} {# we abuse 'set' to actually add new values to the original vips dict. you can not add elements to a dict with jinja - this trick was found at
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{% for name, ip in vips.iteritems() %}
{{ name }} IN A {{ ip }}
{% endfor %}
所有 ansible 组都存储在全局变量 groups
中,所以如果你想遍历所有内容,你可以这样做:
All groups:
{% for g in groups %}
{{ g }}
{% endfor %}
Hosts in group "all":
{% for h in groups['all'] %}
{{ h }}
{% endfor %}
等等
我正在更深入地研究 Ansible 功能,我希望以一种漂亮的方式实现 VIP 的概念。
为此,我在清单的 group_vars
中实现了这个变量:
group_vars/firstcluster:
vips:
- name: cluster1_vip
ip: 1.2.3.4
- name: cluster1.othervip
ip: 1.2.3.5
group_vars/secondcluster:
vips:
- name: cluster2_vip
ip: 1.2.4.4
- name: cluster2.othervip
ip: 1.2.4.5
并在清单中:
[firstcluster]
node10
node11
[secondcluster]
node20
node21
我的问题:如果我想设置一个 DNS 服务器,收集所有 VIP 和相关名称(没有多余的美学冗余),我该如何进行?简而言之:除了下面的主机之外,是否有可能获得所有组变量?
喜欢:
{% for group in <THEMAGICVAR> %}
{% for vip in group.vips %}
{{ vip.name }} IN A {{ vip.ip }}
{% end for %}
{% end for %}
我认为您不能直接访问任何组的变量,但是您可以访问组主机,并且可以从主机访问变量。所以遍历所有组,然后只选择每个组的第一个主机就可以了。
您要查找的魔术变量是 groups
。同样重要的是 hostvars
.
{%- for group in groups -%}
{%- for host in groups[group] -%}
{%- if loop.first -%}
{%- if "vips" in hostvars[host] -%}
{%- for vip in hostvars[host].vips %}
{{ vip.name }} IN A {{ vip.ip }}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
文档:Magic Variables, and How To Access Information About Other Hosts
如果主机属于多个组,您可能需要过滤重复的条目。在这种情况下,您需要先收集字典中的所有值,然后像这样在单独的循环中输出它:
{% set vips = {} %} {# we store all unique vips in this dict #}
{%- for group in groups -%}
{%- for host in groups[group] -%}
{%- if loop.first -%}
{%- if "vips" in hostvars[host] -%}
{%- for vip in hostvars[host].vips -%}
{%- set _dummy = vips.update({vip.name:vip.ip}) -%} {# we abuse 'set' to actually add new values to the original vips dict. you can not add elements to a dict with jinja - this trick was found at
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{% for name, ip in vips.iteritems() %}
{{ name }} IN A {{ ip }}
{% endfor %}
所有 ansible 组都存储在全局变量 groups
中,所以如果你想遍历所有内容,你可以这样做:
All groups:
{% for g in groups %}
{{ g }}
{% endfor %}
Hosts in group "all":
{% for h in groups['all'] %}
{{ h }}
{% endfor %}
等等