简体   繁体   English

Ansible获得其他组变速器

[英]Ansible get other group vars

I'm digging into Ansible capabilities a bit further, and I'm looking to implement with a beautiful manner the concept of VIP. 我正在深入研究Ansible的功能,我希望以优美的方式实现VIP的概念。 To do so, I implemented this variable in the group_vars of my inventory: 为此,我在我的库存的group_vars中实现了这个变量:

group_vars/firstcluster: group_vars / firstcluster:

vips:
  - name: cluster1_vip
    ip: 1.2.3.4
  - name: cluster1.othervip
    ip: 1.2.3.5

group_vars/secondcluster: group_vars / secondcluster:

vips:
  - name: cluster2_vip
    ip: 1.2.4.4
  - name: cluster2.othervip
    ip: 1.2.4.5

and in the inventory: 并在库存中:

[firstcluster]
node10
node11

[secondcluster]
node20
node21

My question: if I want to set up a DNS server, that gathers all the VIP and associated names (without redundancy for esthetism), how do I proceed? 我的问题:如果我想建立一个DNS服务器,收集所有VIP和相关名称(没有美学冗余),我该如何处理? In short: is it possible to get all group variables notwithstanding the hosts underneath? 简而言之:尽管主机位于下方,是否可以获得所有组变量?

like: 喜欢:

{% for group in <THEMAGICVAR> %}
{% for vip in group.vips %}
{{ vip.name }}      IN A     {{ vip.ip }}
{% end for %}
{% end for %}

I think you can not access variables of any group directly, but you can access the groups hosts and from the hosts you can access the variables. 我认为您不能直接访问任何组的变量,但您可以访问组主机,并从主机访问变量。 So looping over all groups and then only picking the first host of each group should do it. 因此,遍历所有组,然后只选择每个组的第一个主机应该这样做。

The magic var you're looking for is groups . 你正在寻找的神奇变种是groups Also important is hostvars . 同样重要的是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 -%}

Docs: Magic Variables, and How To Access Information About Other Hosts 文档: 魔术变量,以及如何访问有关其他主机的信息


If a host belongs to multiple groups, you might want to filter duplicate entries. 如果主机属于多个组,您可能希望过滤重复的条目。 In that case you need to collect all values in a dict first and output it in a separate loop like so: 在这种情况下,您需要先收集dict中的所有值,然后将其输出到一个单独的循环中,如下所示:

{% 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 http://stackoverflow.com/a/18048884/2753241#}
        {%- endfor -%}
      {%- endif -%}
    {%- endif -%}
  {%- endfor -%}
{%- endfor -%}


{% for name, ip in vips.iteritems() %}
{{ name }} IN A {{ ip }}
{% endfor %}

All ansible groups are stored in the global variable groups , so you can do something like this if you want to iterate through everything: 所有ansible组都存储在全局变量groups ,因此如果要迭代所有内容,可以执行以下操作:

All groups:
{% for g in groups %}
{{ g }}
{% endfor %}

Hosts in group "all":
{% for h in groups['all'] %}
{{ h }}
{% endfor %}

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM