1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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:
import netaddr
except Exception, e:
raise errors.AnsibleFilterError('python-netaddr package is not installed')
except ImportError:
# 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 ----
@ -500,22 +505,18 @@ def hwaddr(value, query = '', alias = 'hwaddr'):
return False
class mac_linux(netaddr.mac_unix): pass
mac_linux.word_fmt = '%.2x'
def macaddr(value, query = ''):
return hwaddr(value, query, alias = 'macaddr')
def _need_netaddr(*args, **kwargs):
raise errors.AnsibleFilterError('python-netaddr package is not installed')
# ---- Ansible filters ----
class FilterModule(object):
''' IP address and network manipulation filters '''
def filters(self):
return {
filter_map = {
# IP addresses and networks
'ipaddr': ipaddr,
'ipwrap': ipwrap,
@ -526,6 +527,11 @@ class FilterModule(object):
# MAC / HW addresses
'hwaddr': hwaddr,
'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)