24 lines
773 B
Python
24 lines
773 B
Python
from ansible.plugins.lookup import LookupBase
|
|
from ansible.errors import AnsibleError
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(self, terms, variables=None, **kwargs):
|
|
if not variables or 'hostvars' not in variables:
|
|
raise AnsibleError("hostvars is not available in this context")
|
|
|
|
results = []
|
|
|
|
for term in terms:
|
|
if term not in variables['hostvars']:
|
|
raise AnsibleError(f"Host '{term}' not found in hostvars")
|
|
|
|
host = variables['hostvars'][term]
|
|
ip = host.get('ansible_host') or host.get('ansible_default_ipv4', {}).get('address')
|
|
|
|
if not ip:
|
|
raise AnsibleError(f"No IP found for host '{term}'")
|
|
|
|
results.append(ip)
|
|
|
|
return results
|