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