Created
December 5, 2017 13:18
-
-
Save hitsumabushi/73411a6bcb900a05c027ae0c8a39a9b3 to your computer and use it in GitHub Desktop.
sacloud inventory (for AWX container)
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 sys | |
import argparse | |
import json | |
import os | |
import subprocess | |
# for exclude hosts, check tags | |
filtering_tag = "__with_sacloud_inventory" | |
# Install usacloud | |
is_usacloud_installed = None | |
if sys.version_info.major == 3 and sys.version_info.minor > 3: | |
import shutil | |
is_usacloud_installed = shutil.which('usacloud') | |
else: # python < 3.4 | |
import distutils.spawn | |
is_usacloud_installed = distutils.spawn.find_executable('usacloud') | |
if not is_usacloud_installed: | |
subprocess.check_output("curl -fsSL http://releases.usacloud.jp/usacloud/repos/setup-yum.sh | sh", shell=True) | |
parser = argparse.ArgumentParser(description='sacloud inventory') | |
parser.add_argument('--list', action='store_true', default=True, help='List all active Droplets as Ansible inventory (default: True)') | |
parser.add_argument('--host', action='store', help='Get all Ansible inventory variables about a specific Droplet') | |
args = parser.parse_args() | |
if args.list: | |
s = subprocess.check_output("usacloud server list --out json --max=1000", shell=True) | |
j = json.loads(s.decode('utf-8')) | |
inventory = {} | |
hostvars = {} | |
for i in j: | |
# filter hosts by tags | |
match = False | |
for tag in i['Tags']: | |
if tag == filtering_tag: | |
match = True | |
break | |
if not match: | |
continue | |
zone = i['Zone']['Name'] | |
host = i['Name'] | |
interfaces = i['Interfaces'] | |
ahost = i['Name'] | |
if zone not in inventory.keys(): | |
inventory[zone] = [] | |
inventory[zone].append(host) | |
for tag in i['Tags']: | |
if tag not in inventory.keys(): | |
inventory[tag] = [] | |
inventory[tag].append(host) | |
hostvars[host] = { | |
'ansible_host': ahost, | |
'name': host, | |
'sacloud_id': i['ID'], | |
'sacloud_tags': i['Tags'], | |
'sacloud_interfaces': [ifc for ifc in i['Interfaces']], | |
'sacloud_disks': [d for d in i['Disks']], | |
} | |
for k, v in inventory.items(): | |
v.sort() | |
inventory['_meta'] = {"hostvars":hostvars} | |
print(json.dumps(inventory, sort_keys=True, indent=2, separators=(',', ': '))) | |
elif args.host is not None or args.host == "": | |
s = subprocess.check_output("usacloud server read --out json {}".format(args.host), shell=True) | |
j = json.loads(s.decode('utf-8'))[0] | |
interfaces = j['Interfaces'] | |
ahost = j['Name'] | |
i = { | |
'ansible_host': ahost, | |
'name': j['Name'], | |
'sacloud_id': j['ID'], | |
'sacloud_tags': j['Tags'], | |
'sacloud_interfaces': [ifc for ifc in interfaces], | |
'sacloud_disks': [d for d in j['Disks']] | |
} | |
print(json.dumps(i, sort_keys=True, indent=2, separators=(',', ': '))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment