First try at only failing if the filter is actually used.

This commit is contained in:
Toshio Kuratomi 2015-02-01 15:15:53 -08:00
parent 7e46554160
commit 616fda5767

View file

@ -19,8 +19,13 @@ from ansible import errors
try: try:
import netaddr import netaddr
except Exception, e: except ImportError:
raise errors.AnsibleFilterError('python-netaddr package is not installed') # in this case, we'll make the filters return error messages (see bottom)
netaddr = None
else:
class mac_linux(netaddr.mac_unix):
pass
mac_linux.word_fmt = '%.2x'
# ---- IP address and network filters ---- # ---- IP address and network filters ----
@ -500,32 +505,33 @@ def hwaddr(value, query = '', alias = 'hwaddr'):
return False return False
class mac_linux(netaddr.mac_unix): pass
mac_linux.word_fmt = '%.2x'
def macaddr(value, query = ''): def macaddr(value, query = ''):
return hwaddr(value, query, alias = 'macaddr') return hwaddr(value, query, alias = 'macaddr')
def _need_netaddr(*args, **kwargs):
raise errors.AnsibleFilterError('python-netaddr package is not installed')
# ---- Ansible filters ---- # ---- Ansible filters ----
class FilterModule(object): class FilterModule(object):
''' IP address and network manipulation filters ''' ''' IP address and network manipulation filters '''
filter_map = {
# IP addresses and networks
'ipaddr': ipaddr,
'ipwrap': ipwrap,
'ipv4': ipv4,
'ipv6': ipv6,
'ipsubnet': ipsubnet,
# MAC / HW addresses
'hwaddr': hwaddr,
'macaddr': macaddr
}
def filters(self): def filters(self):
return { if netaddr:
return self.filter_map
# IP addresses and networks else:
'ipaddr': ipaddr, # Need to install python-netaddr for these filters to work
'ipwrap': ipwrap, return dict((f, _need_netaddr) for f in self.filter_map)
'ipv4': ipv4,
'ipv6': ipv6,
'ipsubnet': ipsubnet,
# MAC / HW addresses
'hwaddr': hwaddr,
'macaddr': macaddr
}