From 21d3cf19c41febc4132c6963701138148e35c077 Mon Sep 17 00:00:00 2001 From: Edoardo Putti Date: Sun, 27 Aug 2017 15:55:43 +0200 Subject: [PATCH] add custom error for ansible in ip_from_hostname filter --- filter_plugins/ip_from_inventory.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/filter_plugins/ip_from_inventory.py b/filter_plugins/ip_from_inventory.py index 80e324f..3cde075 100644 --- a/filter_plugins/ip_from_inventory.py +++ b/filter_plugins/ip_from_inventory.py @@ -1,9 +1,14 @@ +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) }}`` """ - return hostvars[hostname]['ansible_host'] + 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):