From 1ff93e6b08ef9a0246d0b87deb8071d205d4e8e7 Mon Sep 17 00:00:00 2001 From: Steven Dossett Date: Sat, 1 Jun 2013 11:42:44 -0400 Subject: [PATCH 1/2] Plugin with_inventory_hostnames - loops generated from groups or hosts in inventory --- .../lookup_plugins/inventory_hostnames.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/ansible/runner/lookup_plugins/inventory_hostnames.py diff --git a/lib/ansible/runner/lookup_plugins/inventory_hostnames.py b/lib/ansible/runner/lookup_plugins/inventory_hostnames.py new file mode 100644 index 0000000000..d88f54b55d --- /dev/null +++ b/lib/ansible/runner/lookup_plugins/inventory_hostnames.py @@ -0,0 +1,45 @@ +# (c) 2012, Michael DeHaan +# (c) 2013, Steven Dossett +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +from ansible.utils import safe_eval +import ansible.utils as utils +import ansible.errors as errors +import ansible.inventory as inventory + +def flatten(terms): + ret = [] + for term in terms: + if isinstance(term, list): + ret.extend(term) + else: + ret.append(term) + return ret + +class LookupModule(object): + + def __init__(self, basedir=None, **kwargs): + self.basedir = basedir + + def run(self, terms, inject=None, **kwargs): + terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) + + if not isinstance(terms, list): + raise errors.AnsibleError("with_inventory_hostnames expects a list") + + return flatten(inventory.Inventory().list_hosts(terms)) + From e0a15d0c503b2eef055b4883f717ecac77379ef1 Mon Sep 17 00:00:00 2001 From: Steven Dossett Date: Mon, 3 Jun 2013 12:42:52 -0400 Subject: [PATCH 2/2] Updated to support inventory options from command line --- lib/ansible/runner/lookup_plugins/inventory_hostnames.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/runner/lookup_plugins/inventory_hostnames.py b/lib/ansible/runner/lookup_plugins/inventory_hostnames.py index d88f54b55d..6880887d84 100644 --- a/lib/ansible/runner/lookup_plugins/inventory_hostnames.py +++ b/lib/ansible/runner/lookup_plugins/inventory_hostnames.py @@ -34,12 +34,12 @@ class LookupModule(object): def __init__(self, basedir=None, **kwargs): self.basedir = basedir + self.host_list = kwargs['runner'].inventory.host_list def run(self, terms, inject=None, **kwargs): terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) if not isinstance(terms, list): raise errors.AnsibleError("with_inventory_hostnames expects a list") - - return flatten(inventory.Inventory().list_hosts(terms)) + return flatten(inventory.Inventory(self.host_list).list_hosts(terms))