From 5d15515bbf691929a8fc970603bd23c705b036f6 Mon Sep 17 00:00:00 2001 From: Matt Spaulding Date: Sun, 28 Jul 2013 13:54:17 -0700 Subject: [PATCH 1/2] Add indexed_items lookup plugin --- .../runner/lookup_plugins/indexed_items.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/ansible/runner/lookup_plugins/indexed_items.py diff --git a/lib/ansible/runner/lookup_plugins/indexed_items.py b/lib/ansible/runner/lookup_plugins/indexed_items.py new file mode 100644 index 0000000000..c1db1fdee2 --- /dev/null +++ b/lib/ansible/runner/lookup_plugins/indexed_items.py @@ -0,0 +1,44 @@ +# (c) 2012, Michael DeHaan +# +# 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 + +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_indexed_items expects a list") + + items = flatten(terms) + return zip(range(len(items)), items) + From b7154cf2cfbc22695188f3e8ff59f64665cd3986 Mon Sep 17 00:00:00 2001 From: Matt Spaulding Date: Sun, 28 Jul 2013 14:16:25 -0700 Subject: [PATCH 2/2] Add tests for indexed_items lookup plugin --- test/lookup_plugins.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/lookup_plugins.yml b/test/lookup_plugins.yml index 7d2141cc0d..511e09b701 100644 --- a/test/lookup_plugins.yml +++ b/test/lookup_plugins.yml @@ -77,3 +77,28 @@ - name: cleanup test file, again action: file path=/tmp/ansible-test-with_password state=absent +# indexed_items lookup plugin + - name: create directory for indexed_items + file: path=/tmp/ansible-test-with_indexed_items-data state=directory + - name: test indexed_items + shell: echo "{{ item.0 }}" > /tmp/ansible-test-with_indexed_items-data/{{ item.1 }} + with_indexed_items: + - a + - b + - c + - name: check indexed_items content + shell: test -f /tmp/ansible-test-with_indexed_items-data/{{ item.1 }} && + test "{{ item.0 }}" = "$(cat /tmp/ansible-test-with_indexed_items-data/{{ item.1 }})" + with_indexed_items: + - a + - b + - c + - name: cleanup indexed_items test + file: path=/tmp/ansible-test-with_indexed_items-data/{{ item.1 }} state=absent + with_indexed_items: + - a + - b + - c + - name: remove with_indexed_items directory + file: path=/tmp/ansible-test-with_indexed_items-data state=absent +