Created
April 13, 2022 15:44
-
-
Save bechampion/39878eac71a55c3a99475a6dc3d2aa7e to your computer and use it in GitHub Desktop.
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/python | |
import os | |
import sys | |
import argparse | |
from google.cloud.resourcemanager import ProjectsClient | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
class IpsosInventory(object): | |
def __init__(self): | |
self.inventory = {} | |
self.read_cli_args() | |
if self.args.list: | |
self.inventory = self.example_inventory() | |
elif self.args.host: | |
self.inventory = self.empty_inventory() | |
else: | |
self.inventory = self.empty_inventory() | |
print(json.dumps(self.inventory)) | |
def example_inventory(self): | |
client = ProjectsClient() | |
project_pager = client.search_projects() | |
pjs = {} | |
for page in project_pager: | |
pjs[page.project_id] = { 'hosts': ['10.220.21.24', '10.220.21.27'], 'vars': { 'ansible_ssh_user': 'projectuser'} } | |
return(pjs) | |
# 'hostvars': { | |
# '10.220.21.24': { | |
# 'host_specific_var': 'testhost' | |
# }, | |
# '10.220.21.27': { | |
# 'host_specific_var': 'towerhost' | |
# } | |
# } | |
# } | |
# } | |
def empty_inventory(self): | |
return {'_meta': {'hostvars': {}}} | |
def read_cli_args(self): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--list', action = 'store_true') | |
parser.add_argument('--host', action = 'store') | |
self.args = parser.parse_args() | |
IpsosInventory() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment