Browse Source

use check instead of try, except block in filter ip_from_address

python3
Edoardo Putti 7 years ago
parent
commit
7eca6ef950
1 changed files with 4 additions and 4 deletions
  1. +4
    -4
      filter_plugins/ip_from_inventory.py

+ 4
- 4
filter_plugins/ip_from_inventory.py View File

@ -5,10 +5,10 @@ def ip_from_inventory(hostvars, hostname):
replace this ``{{ hostvars[hostname]['ansible_host'] }}`` replace this ``{{ hostvars[hostname]['ansible_host'] }}``
with something nicer such as `` {{ hostvars | ip_from_inventory(hostname) }}`` 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))
if not hostname in hostvars:
raise AnsibleError('<ip_from_inventory> hostname "{hostname}" not found in inventory'.format(hostname=hostname))
return hostvars[hostname]['ansible_host']
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):


Loading…
Cancel
Save