Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleJamesWalker/48123438849af7897dd0 to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/48123438849af7897dd0 to your computer and use it in GitHub Desktop.
Ansible with_items_sequence

Very quick with_items_sequence lookup plugin. I needed the ability to create a list of workers on a remoter server, based on the structure these workers could be on differrent autoscale groups, and based on size different worker counts per instance. My playbook creates docker containers that are run via supervisor, but you have to have unique names for each of the containers if you want to have more instances, and I couldn't find a clean way to do this without a new lookup plugin.

Run the playbook to test with: ansible-playbook -i localhost, playbook.yml

Note: This was thrown togehter very quickly, I'll hopfully have some time to expand this plugin with proper documention and error handling.

# Kyle James Walker <KyleJamesWalker@gmail.com>
#
# This file is a lookup plugin for 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 <http://www.gnu.org/licenses/>.
from ansible.errors import AnsibleError
import ansible.utils as utils
class LookupModule(object):
"""Very quickly thrown together, needs more flexability,
and of course documentation.
Follow the sequence plugin later:
https://github.com/ansible/ansible/blob/devel/lib/ansible/
runner/lookup_plugins/sequence.py
"""
def __init__(self, basedir, **kwargs):
"""absorb any keyword args"""
self.basedir = basedir
def reset(self):
"""set sensible defaults"""
self.items = None
self.src_dict = None
self.flatten_key = None
self.start = 1
self.count = None
self.end = None
self.stride = 1
self.format = "%d"
def run(self, terms, inject=None, **kwargs):
results = []
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
self.reset()
for term in terms:
setattr(self, term, terms[term])
for x in self.items:
count = self.count
if isinstance(count, basestring):
count = x.get(count)
for n in xrange(1, count + 1):
results.append((x, n),)
return results
---
- name: Testing My custom with_ plugin
hosts: localhost
connection: local
vars:
run_flat:
- name: hello
proc_cnt: 2
- name: world
proc_cnt: 1
tasks:
- name: Test 1 - hello_1;hello_2;world_1;world_2
debug:
msg: "{{item.0.name}}_{{item.1}}"
with_items_sequence:
items: "{{run_flat}}"
count: 2
- name: Test 2 - hello_1;hello_2;world_1
debug:
msg: "{{item.0.name}}_{{item.1}}"
with_items_sequence:
items: "{{run_flat}}"
count: proc_cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment