Missing a split function in Jinja

308 views
Skip to first unread message

Patrick Ogenstad

unread,
Mar 22, 2015, 11:15:57 AM3/22/15
to ansibl...@googlegroups.com
Hi,

I'm setting up a new test network and had some plans for how I wanted to setup DNS. However I ran into a brick wall since Jinja doesn't have a split function.

However I found that Ansible has it's own Jinja functions and I added a simple function to ansible/runner/filter_plugins/core.py:

def get_shard(a, separator, index):
    values = a.split(separator)
    index = int(index)
    return values[index]

(then added it under filters in the FilterModule class within that core.py file).

This solved my problem but I don't know if this is the right place for such code, or if the function has the best name. However I would like to see this in Ansible in the future. Would you accept a filter like this in core.py? If not could it be submitted somewhere else?

So what I wanted was to be able to create a vars file as below to be able to create DNS entries under a specific domain and automatically create the reverse DNS entry.

This is my vars file:
---
domain:
        hosts:
            ise-1:
                ip: 172.29.50.100
            nas-1:
                ip: 172.29.52.37
            ns2803-asw-01:
                ip: 172.29.50.3
            ns2803-ap-01:
                ip: 172.29.50.4
            srv-base-1:
                ip: 172.29.50.34
            srv-dev-1:
                ip: 172.29.50.39
            srv-esxi-1:
                ip: 172.29.50.10
            srv-file-1:
                ip: 172.29.50.37
            srv-master-1:
                ip: 172.29.50.42
            srv-mon-1:
                ip: 172.29.50.43
            srv-script-1:
                ip: 172.29.50.35
            tsrv-control-1:
                ip: 172.29.54.10
        aliases:
            www:
                host: srv-base-1
        hosts:
            tsrv-base-1:
                ip: 172.29.54.34
            tsrv-file-1:
                ip: 172.29.54.37

reverse_domain:
    50.29.172.in-addr.arpa:
    54.29.172.in-addr.arpa:
###

These are my tasks:
##
- name: Update forward zones
  template: src=forward.db.j2 dest="/etc/bind/zones/{{ item.key }}.db"
  with_dict: domain

- name: Update reverse zones
  template: src=reverse.db.j2 dest="/etc/bind/zones/rev.{{ item.key }}.db"
  with_dict: reverse_domain
##

# forward.db.j2 ##
{% for zone in domain %}
{% if zone == item.key  %}

{% for host in domain[zone]['hosts'] %}
{{ host }}       IN A {{ domain[zone]['hosts'][host]['ip'] }}
{% endfor %}

{% endif %}
{% endfor %}
###

# reverse.db.j2 ## Indented to have some kind of readability.
{% for zone in reverse_domain %}
 {% if zone == item.key  %}
  {% for forward in domain %}
   {% for host in domain[forward]['hosts'] %}
    {% if domain[forward]['hosts'][host]['ip']|get_shard('.', 0) ==     zone|get_shard('.', 2) %}
     {% if domain[forward]['hosts'][host]['ip']|get_shard('.', 1) ==      zone|get_shard('.', 1) %}
      {% if domain[forward]['hosts'][host]['ip']|get_shard('.', 2) ==       zone|get_shard('.', 0) %}
      {{ domain[forward]['hosts'][host]['ip']|get_shard('.', 3) }} IN PTR    {{    host}}.{{ forward }}.
      {% endif %}
     {% endif %}
    {% endif %}
   {% endfor %}
  {% endfor %}
 {% endif %}
{% endfor %}
###

An entry in the reverse zone would then look like this:

Basically it splits the ip address and the reverse domain name and then compares the parts to see if a particular host should be included in the reverse zone.

Best regards
Patrick

Matt Martz

unread,
Mar 22, 2015, 12:01:10 PM3/22/15
to Patrick Ogenstad, ansibl...@googlegroups.com
Because jinja2 variables are Python objects, they also carry the methods of the Python object.  As such you can use .split() directly in a playbook/template such as:

domain[forward]['hosts'][host]['ip'].split('.')[0]

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Patrick Ogenstad

unread,
Mar 22, 2015, 12:26:44 PM3/22/15
to ansibl...@googlegroups.com, pat...@ogenstad.com
Ah, cool. Thanks Matt! I'll use that instead.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-devel+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages