Skip to content

Instantly share code, notes, and snippets.

@jpmens
Created December 1, 2022 15:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmens/d48c5223f28e78496bd221f458d72454 to your computer and use it in GitHub Desktop.
Save jpmens/d48c5223f28e78496bd221f458d72454 to your computer and use it in GitHub Desktop.
Ansible filter convert IP to arpaname
# arpaname.py, (C)2022 by Jan-Piet Mens <jp@mens.de>
# Convert an IPv4 or IPv6 address in textual form into a string whose value is
# the reverse-map domain name of the address.
#
# - debug: msg="{{ "192.168.1.3" | arpaname }}"
# "3.1.168.192.in-addr.arpa."
#
# - debug: msg="{{ "2001:DB8::7" | arpaname }}"
# "7.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa."
import dns.reversename
def arpaname(ip_addr):
rev = dns.reversename.from_address(str(ip_addr))
return str(rev)
class FilterModule(object):
def filters(self):
return {
'arpaname' : arpaname,
'revip' : arpaname,
}
@jpmens
Copy link
Author

jpmens commented Dec 1, 2022

- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
     - debug: msg="192.168.1.3 is {{ "192.168.1.3" | arpaname }}"
     - debug: msg="2001:DB8::7 is {{ "2001:DB8::7" | arpaname }}"

@jpmens
Copy link
Author

jpmens commented Dec 1, 2022

TIL that | ansible.utils.ipaddr('revdns') exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment