Manage DNS servers using Ansible #93
2 changed files with 14 additions and 1 deletions
|
@ -20,9 +20,11 @@ import dns.rdtypes.ANY.TXT
|
||||||
import dns.rdtypes.IN.A
|
import dns.rdtypes.IN.A
|
||||||
import dns.rdtypes.IN.AAAA
|
import dns.rdtypes.IN.AAAA
|
||||||
import dns.rdtypes.IN.SRV
|
import dns.rdtypes.IN.SRV
|
||||||
|
import dns.reversename
|
||||||
import dns.serial
|
import dns.serial
|
||||||
import dns.zone
|
import dns.zone
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.common.validation import check_type_list
|
||||||
|
|
||||||
|
|
||||||
class RName(dns.name.Name):
|
class RName(dns.name.Name):
|
||||||
|
@ -250,12 +252,20 @@ def product_dict(dct, keys=None):
|
||||||
|
|
||||||
def make_hosts_records(hosts):
|
def make_hosts_records(hosts):
|
||||||
for host, addrs in hosts.items():
|
for host, addrs in hosts.items():
|
||||||
for addr in addrs:
|
for addr in check_type_list(addrs):
|
||||||
name = dns.name.from_text(host, origin=dns.name.empty)
|
name = dns.name.from_text(host, origin=dns.name.empty)
|
||||||
decoded = ipaddress.ip_address(addr)
|
decoded = ipaddress.ip_address(addr)
|
||||||
yield AAAA(addr, name) if decoded.version == 6 else A(addr, name)
|
yield AAAA(addr, name) if decoded.version == 6 else A(addr, name)
|
||||||
|
|
||||||
|
|
||||||
|
def make_reverse_hosts_records(hosts):
|
||||||
|
for host, addrs in hosts.items():
|
||||||
|
for addr in check_type_list(addrs):
|
||||||
|
name = dns.name.from_text(host)
|
||||||
|
reverse = dns.reversename.from_address(addr)
|
||||||
|
yield PTR(name, reverse)
|
||||||
|
|
||||||
|
|
||||||
def make_records(args, ty):
|
def make_records(args, ty):
|
||||||
# TODO: Ça n'est pas du tout élégant, mais :
|
# TODO: Ça n'est pas du tout élégant, mais :
|
||||||
# 1. je n'ai pas réussi à spécifier dans `argument_spec` un type tiers
|
# 1. je n'ai pas réussi à spécifier dans `argument_spec` un type tiers
|
||||||
|
@ -321,6 +331,7 @@ def main() -> int:
|
||||||
"options": spec_options_of_type(SOA),
|
"options": spec_options_of_type(SOA),
|
||||||
},
|
},
|
||||||
"hosts": {"type": "dict", "default": {}},
|
"hosts": {"type": "dict", "default": {}},
|
||||||
|
"reverse_hosts": {"type": "dict", "default": {}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, ty in record_types.items():
|
for name, ty in record_types.items():
|
||||||
|
@ -348,6 +359,7 @@ def main() -> int:
|
||||||
|
|
||||||
records = itertools.chain(
|
records = itertools.chain(
|
||||||
make_records(module.params["soa"], SOA),
|
make_records(module.params["soa"], SOA),
|
||||||
|
make_reverse_hosts_records(module.params["reverse_hosts"]),
|
||||||
make_hosts_records(module.params["hosts"]),
|
make_hosts_records(module.params["hosts"]),
|
||||||
itertools.chain.from_iterable(
|
itertools.chain.from_iterable(
|
||||||
itertools.chain.from_iterable(
|
itertools.chain.from_iterable(
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
minimum: "{{ item.value.soa.minimum | default(knotd__soa_minimum)
|
minimum: "{{ item.value.soa.minimum | default(knotd__soa_minimum)
|
||||||
| community.general.to_seconds | int }}"
|
| community.general.to_seconds | int }}"
|
||||||
hosts: "{{ item.value.hosts | default(omit) }}"
|
hosts: "{{ item.value.hosts | default(omit) }}"
|
||||||
|
reverse_hosts: "{{ item.value.reverse_hosts | default(omit) }}"
|
||||||
ns: "{{ item.value.ns | default(omit) }}"
|
ns: "{{ item.value.ns | default(omit) }}"
|
||||||
mx: "{{ item.value.mx | default(omit) }}"
|
mx: "{{ item.value.mx | default(omit) }}"
|
||||||
cname: "{{ item.value.cname | default(omit) }}"
|
cname: "{{ item.value.cname | default(omit) }}"
|
||||||
|
|
Loading…
Reference in a new issue