blob: 365e55d8866ada0e773ad3ae0861d50049c4fb35 [file] [log] [blame]
#!/usr/bin/env python
# SPDX-FileCopyrightText: © 2017-2020 Open Networking Foundation <support@opennetworking.org>
# SPDX-License-Identifier: Apache-2.0
# unbound_revdns - ansible filter - ipv4 address or cidr and generates a
# reverse DNS string suitable for use with unbound, which requires them in a
# slightly unusual format when a RFC1918 address is used with a local-zone
# definition:
# https://nlnetlabs.nl/documentation/unbound/unbound.conf/#local-zone
# https://www.claudiokuenzler.com/blog/699/unbound-dns-not-serving-reverse-lookup-for-internal-addresses-rfc1918
from __future__ import absolute_import
import netaddr
class FilterModule(object):
def filters(self):
return {
"unbound_revdns": self.unbound_revdns,
}
def unbound_revdns(self, var):
(o1, o2, o3, o4) = netaddr.IPNetwork(var).network.words
revdns = "%d.%d.%d.%d.in-addr.arpa." % (o4, o3, o2, o1)
if o2 == 0 or o1 == 10:
revdns = "%d.in-addr.arpa." % (o1)
elif (
o3 == 0
or (o1 == 172 and o2 >= 16 and o2 <= 31)
or (o1 == 192 and o2 == 168)
):
revdns = "%d.%d.in-addr.arpa." % (o2, o1)
elif o4 == 0:
revdns = "%d.%d.%d.in-addr.arpa." % (o3, o2, o1)
return revdns