from ansible.errors import AnsibleError, AnsibleParserError

def ip_from_inventory(hostvars, hostname):
    """
    replace this ``{{ hostvars[hostname]['ansible_host'] }}``
    with something nicer such as `` {{ hostvars | ip_from_inventory(hostname) }}``
    """
    try:
        return hostvars[hostname]['ansible_host']
    except KeyError:
        raise AnsibleError('hostname "{hostname}" not found in inventory'.format(hostname=hostname))

class FilterModule(object):
    def filters(self):
        return {
                'ip_from_inventory': ip_from_inventory,
        }


@@ -266,82 +266,35 @@
     
-try:%0A        return hostvars%5Bhostname%5D%5B'ansible_host'%5D%0A    except KeyError
+if not hostname in hostvars
 :%0A  
@@ -319,16 +319,36 @@
 eError('
+%3Cip_from_inventory%3E 
 hostname
@@ -413,16 +413,63 @@
 name))%0A%0A
+    return hostvars%5Bhostname%5D%5B'ansible_host'%5D%0A%0A
 class Fi

use check instead of try, except block in filter ip_from_address