From 616fda576740600cae768b3cb1ce039a9a53b54f Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Sun, 1 Feb 2015 15:15:53 -0800 Subject: [PATCH] First try at only failing if the filter is actually used. --- lib/ansible/runner/filter_plugins/ipaddr.py | 48 ++++++++++++--------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/ansible/runner/filter_plugins/ipaddr.py b/lib/ansible/runner/filter_plugins/ipaddr.py index 68e2fd778c4..d0408d6e7b4 100644 --- a/lib/ansible/runner/filter_plugins/ipaddr.py +++ b/lib/ansible/runner/filter_plugins/ipaddr.py @@ -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,32 +505,33 @@ 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 ''' + 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): - return { - - # IP addresses and networks - 'ipaddr': ipaddr, - 'ipwrap': ipwrap, - 'ipv4': ipv4, - 'ipv6': ipv6, - 'ipsubnet': ipsubnet, - - # MAC / HW addresses - 'hwaddr': hwaddr, - 'macaddr': macaddr - - } - + 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)