Created
October 31, 2019 15:28
-
-
Save samdoran/6d6190440619e85e9d08f29263deee3c to your computer and use it in GitHub Desktop.
Generate an Ansible inventory with a lot of hosts
This file contains 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 | |
import argparse | |
import os | |
import sys | |
import yaml | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--number', '-n', default=100) | |
parser.add_argument('--name', '-f', default='hosts.yml') | |
parser.add_argument('--force', action='store_true') | |
args = parser.parse_args() | |
filename = args.name | |
force = args.force | |
number = args.number | |
if os.path.isfile(filename): | |
if not force: | |
sys.exit('{} already exists'.format(filename)) | |
width = len(number) | |
hosts = {} | |
for host in range(1, int(number) + 1): | |
hostname = 'host_{:0{width}}'.format(host, width=width) | |
hosts[hostname] = {'var1': 'somevalue'} | |
inventory = {'all': {'hosts': hosts}} | |
with open(filename, 'w') as f: | |
f.write(yaml.dump(inventory)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment