Last active
September 8, 2017 15:58
-
-
Save evrardjp/ba2c30010dcf650d1a8b13bab05b2978 to your computer and use it in GitHub Desktop.
Static/Dynamic Inventory generation tool to see how they scale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Example custom dynamic inventory script for Ansible, in Python. | |
''' | |
import os | |
import sys | |
import argparse | |
from jinja2 import Template | |
INVENTORY_STRUCTURE = """ | |
{%- for groupname in inventory -%} | |
{%- if groupname != '_meta' -%} | |
[{{ groupname }}] | |
{% for host in inventory[groupname]['hosts'] -%} | |
{{ host }} ip={{inventory['_meta'][host]['ip']}} ansible_connection=local | |
{% endfor -%} | |
[{{ groupname }}:vars] | |
{% for key,value in inventory[groupname]['vars'].items() -%} | |
{{ key }}={{ value }} | |
{% endfor %} | |
{%- endif -%} | |
{%- endfor -%} | |
""" | |
# Generator | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--generate', action = 'store_true') | |
parser.add_argument('-g','--group',action='append',nargs=3, | |
metavar=('group_name','number_of_nodes','number_of_group_vars'),help='help:') | |
parser.add_argument('--hostvars', action = 'store') | |
parser.add_argument('--list', action = 'store_true') | |
parser.add_argument('--host', action = 'store') | |
args = parser.parse_args() | |
if args.generate or args.group: | |
if args.group is None: | |
group = [['groupa','10','1'],['groupb','5','1']] | |
else: | |
group = args.group | |
inventory = generate(group) | |
with open('inventory_dict', 'w') as invf: | |
invf.write(str(inventory)) | |
print(inventory) | |
with open('inventory','w') as static_inv: | |
t = Template(INVENTORY_STRUCTURE) | |
content = t.render(inventory=inventory) | |
print(content) | |
static_inv.write(content) | |
if args.list: | |
with open('inventory_dict', 'r') as inv: | |
inventory = inv.read() | |
print(inventory) | |
if args.host: | |
# meta optimisation is in use, and therefore host shouldn't be callsed | |
print("{}") | |
def generate(group): | |
inventory = dict() | |
hostvars = dict() | |
total_nodes = 0 | |
for groupname, amount_of_nodes, amount_of_group_vars in group: | |
amount_of_nodes = int(amount_of_nodes) | |
amount_of_group_vars = int(amount_of_group_vars) | |
inventory[groupname] = dict() | |
inventory[groupname]['hosts'] = [ 'host{}'.format(x) for x in range(total_nodes,total_nodes + amount_of_nodes)] | |
inventory[groupname]['vars'] = { 'variable' + str(x): str(x) for x in xrange(0, amount_of_group_vars) } | |
total_nodes += amount_of_nodes | |
# optimisation with meta | |
for i in xrange(0, total_nodes): | |
hostname = 'host' + str(i) | |
hostvars[hostname] = { 'ip': str(i) } | |
inventory['_meta'] = hostvars | |
return inventory | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment