Get other group variables

28 views
Skip to first unread message

geoffre...@gmail.com

unread,
Jan 5, 2016, 1:58:31 PM1/5/16
to Ansible Project
Hello everyone !

I'm digging into Ansible capabilities a bit further, and I'm looking to implement with a beautiful manner the concept of VIP.
To do so, I implemented this variable in the group_vars of my inventory:

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



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 ? 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 %}



Thank you very much !

-- Geoff

Brian Coca

unread,
Jan 5, 2016, 2:28:09 PM1/5/16
to Ansible Project
once the inventory is `compiled` there are not group vars, just host
vars, but you can get it by using any host in the group (as long as
you don't override them per host).

{% for group in <THEMAGICVAR> %}
{% for vip in hostvars[groups[group][0]]['vips'] %}
{{ vip.name }} IN A {{ vip.ip }}
{% end for %}
{% end for %}

> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/7f70ee5c-8787-4f9d-bb2f-b1b9b62d6f03%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Mike Biancaniello

unread,
Jan 5, 2016, 3:04:33 PM1/5/16
to Ansible Project
You could also create a VIP list in 'all' and then reference them in the group_vars:

group_vars/all/vips:
all_vips:

 
- name: cluster1_vip
    ip
: 1.2.3.4

    otherattr
: yes
 
- name: cluster1_othervip
    ip
: 1.2.3.5
    otherattr
: yes
 
- name: cluster2_vip
    ip
: 1.2.4.4
    otherattr
: yes
 
- name: cluster2_othervip
    ip
: 1.2.4.5
    otherattr
: yes



group_vars/firstcluster:
vips:
 
- cluster1_vip
 
- cluster1_othervip



group_vars/secondcluster:
vips:
 
- cluster2_vip
 
- cluster2_othervip



templates/zone.j2
{% for vip in all_vips %}

{{ vip.name }}      IN A     {{ vip.ip }}
{% end for %}



templates/vips.j2
{% for vip_name in vips %}
{% for vip in all_vips|selectattr("name", "equalto", vip_name) %}
virtual server {{ vip.name }} {{ vip.ip }}
{% endfor %}{# vip #}
{% endfor %}{# vip_name #}





Reply all
Reply to author
Forward
0 new messages