I am having an issue sorting IPs via Jinja2 and Ansible. Here are my variables and jinja2 code for ansible templates.
roles/DNS/vars/main.yml:
---
DC1:
srv1:
ip: 10.2.110.3
srv2:
ip: 10.2.110.11
srv3:
ip: 10.2.110.19
srv4:
ip: 10.2.110.24
DC2:
srv5:
ip: 172.26.158.3
srv6:
ip: 172.26.158.11
srv7:
ip: 172.26.158.19
srv8:
ip: 172.26.158.24
roles/DNS/templates/db.example.com.j2:
$TTL 86400
@ IN SOA example.com. root.example.com. (
2014051001 ; serial
3600 ; refresh
1800 ; retry
604800 ; expire
86400 ; minimum
)
; Name server
IN NS dns01.example.com.
; Name server A record
dns01.example.com. IN A 10.2.110.92
; 10.2.110.0/24 A records in this Domain
{% for hostname, dnsattr in DC1.iteritems() %}
{{hostname}}.example.com. IN A {{dnsattr.ip}}
; 172.26.158.0/24 A records in this Domain
{% for hostname, dnsattr in DC2.iteritems() %}
{{hostname}}.example.com. IN A {{dnsattr.ip}}
roles/DNS/tasks/main.yml:
- name: Update DNS zone file db.example.com
template:
src: db.example.com.j2
dest: "/tmp/db.example.com"
with_items: "{{DC1,DC2}}"
- name: Restart DNS Server
service:
name: named
state: restarted
The DNS zone files get created correctly, but the IPs are not numerically sorted. I have tried using the following with no luck:
Sorts on hostname alphabetically
{% for hostname, dnsattr in center.iteritems() | sort %}
Does not find the attribute dnsattr
{% for hostname, dnsattr in center.iteritems() | sort(attribute='dnsattr.ip') %}
Does not find the attribute ip
{% for hostname, dnsattr in center.iteritems() | sort(attribute='ip') %}
main.yml
is badly formatted – duplicateip
property,db.example.com.j2
uses undefinedcenter
variable. And you state that The DNS zone files get created correctly. Really? – Konstantin Suvorovcenter
variable in the template? – Konstantin Suvorov[{host: 'srv1',ip:'10.2.110.3'}, etc.]
? – Konstantin Suvorov