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,22 +505,18 @@ 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 = {
def filters(self):
return {
# IP addresses and networks # IP addresses and networks
'ipaddr': ipaddr, 'ipaddr': ipaddr,
'ipwrap': ipwrap, 'ipwrap': ipwrap,
@ -526,6 +527,11 @@ class FilterModule(object):
# MAC / HW addresses # MAC / HW addresses
'hwaddr': hwaddr, 'hwaddr': hwaddr,
'macaddr': macaddr 'macaddr': macaddr
} }
def filters(self):
if netaddr:
return self.filter_map
else:
# Need to install python-netaddr for these filters to work
return dict((f, _need_netaddr) for f in self.filter_map)