Skip to content

Instantly share code, notes, and snippets.

@mgwilliams
Created March 30, 2012 03:12
Show Gist options
  • Save mgwilliams/d405dc48130fede9b3b7 to your computer and use it in GitHub Desktop.
Save mgwilliams/d405dc48130fede9b3b7 to your computer and use it in GitHub Desktop.
import sys
import yaml
import argparse
from ansible.utils import parse_yaml_from_file
from utils import *
import os
conn = cobbler_connection("127.0.0.1")
parser = argparse.ArgumentParser()
parser.add_argument('--extra-vars')
opts = parser.parse_args()
if opts.extra_vars:
extra_vars = split_opts(opts.extra_vars)
else:
extra_vars = {}
limit = extra_vars.get('limit-hosts', None)
if limit:
limit = limit.split(',')
playbook = []
group_data = system_groups(conn, limit)
tasks = extra_vars.get('tasks', None)
groups = group_data['groups']
hosts = group_data['meta']
if tasks:
tasks = tasks.split(',')
else:
tasks = [i for i in groups.keys() if not i == "ungrouped" ]
for host, meta in hosts.items():
vars_files = [
'../data/environments/%s.yaml' % meta['environment'],
'../data/locations/%s.yaml' % meta['location'],
]
group_vars = ['../data/groups/%s.yaml' % group for group in meta['groups'] if group in meta['groups'] and group in tasks and os.path.isfile('./data/groups/%s.yaml' % group)]
vars_files.extend(group_vars)
task_list = [{'include': '../tasks/%s.yaml' % task} for task in tasks if task in meta['groups'] and task in tasks and os.path.isfile('./tasks/%s.yaml' % task)]
handler_list = [{'include': '../tasks/handlers/%s.yaml' % task} for task in tasks if task in meta['groups'] and task in tasks and os.path.isfile('./tasks/handlers/%s.yaml' % task)]
play = {'hosts': host,
'vars_files': vars_files,
'tasks': task_list,
'handlers': handler_list
}
playbook.append(play)
print yaml.dump(playbook)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment