implemented inventory_hostname lookup in v2

This commit is contained in:
Brian Coca 2015-10-15 11:19:11 -04:00
parent 68121b7990
commit 9b088a1e40
3 changed files with 44 additions and 22 deletions

View file

@ -21,18 +21,35 @@ __metaclass__ = type
from ansible.errors import *
from ansible.plugins.lookup import LookupBase
from ansible.inventory import Inventory
class LookupModule(LookupBase):
def run(self, terms, inject=None, **kwargs):
### FIXME: Is this needed now that listify is run on all lookup plugin terms?
if not isinstance(terms, list):
raise AnsibleError("with_inventory_hostnames expects a list")
def get_hosts(self, variables, pattern):
#print(variables)
#print(variables['groups'])
hosts = []
if pattern in variables['groups']:
hosts = variables['groups'][pattern]
elif pattern in variables['groups']['all']:
hosts = [pattern]
return hosts
# FIXME: the inventory is no longer available this way, so we may have
# to dump the host list into the list of variables and read it back
# in here (or the inventory sources, so we can recreate the list
# of hosts)
#return self._flatten(inventory.Inventory(self.host_list).list_hosts(terms))
return terms
def run(self, terms, variables=None, **kwargs):
host_list = []
for term in terms:
patterns = Inventory.order_patterns(Inventory.split_host_pattern(term))
for p in patterns:
that = self.get_hosts(variables, p)
if p.startswith("!"):
host_list = [ h for h in host_list if h not in that]
elif p.startswith("&"):
host_list = [ h for h in host_list if h in that ]
else:
host_list.extend(that)
# return unique list
return list(set(host_list))