mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 00:38:45 -03:30
Make CloudForms inventory_script work
Fixes a few flake8 issues while at it.
This commit is contained in:
@@ -18,10 +18,11 @@ import json
|
|||||||
# http://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings
|
# http://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings
|
||||||
requests.packages.urllib3.disable_warnings()
|
requests.packages.urllib3.disable_warnings()
|
||||||
|
|
||||||
|
|
||||||
class CloudFormsInventory(object):
|
class CloudFormsInventory(object):
|
||||||
|
|
||||||
def _empty_inventory(self):
|
def _empty_inventory(self):
|
||||||
return {"_meta" : {"hostvars" : {}}}
|
return {"_meta": {"hostvars": {}}}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
''' Main execution path '''
|
''' Main execution path '''
|
||||||
@@ -43,7 +44,7 @@ class CloudFormsInventory(object):
|
|||||||
|
|
||||||
# This doesn't exist yet and needs to be added
|
# This doesn't exist yet and needs to be added
|
||||||
if self.args.host:
|
if self.args.host:
|
||||||
data2 = { }
|
data2 = {}
|
||||||
print json.dumps(data2, indent=2)
|
print json.dumps(data2, indent=2)
|
||||||
|
|
||||||
def parse_cli_args(self):
|
def parse_cli_args(self):
|
||||||
@@ -51,9 +52,9 @@ class CloudFormsInventory(object):
|
|||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on CloudForms')
|
parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on CloudForms')
|
||||||
parser.add_argument('--list', action='store_true', default=False,
|
parser.add_argument('--list', action='store_true', default=False,
|
||||||
help='List instances (default: False)')
|
help='List instances (default: False)')
|
||||||
parser.add_argument('--host', action='store',
|
parser.add_argument('--host', action='store',
|
||||||
help='Get all the variables about a specific instance')
|
help='Get all the variables about a specific instance')
|
||||||
self.args = parser.parse_args()
|
self.args = parser.parse_args()
|
||||||
|
|
||||||
def read_settings(self):
|
def read_settings(self):
|
||||||
@@ -97,30 +98,47 @@ class CloudFormsInventory(object):
|
|||||||
|
|
||||||
def get_hosts(self):
|
def get_hosts(self):
|
||||||
''' Gets host from CloudForms '''
|
''' Gets host from CloudForms '''
|
||||||
r = requests.get("https://" + self.cloudforms_hostname + "/api/vms?expand=resources&attributes=name,power_state", auth=(self.cloudforms_username,self.cloudforms_password), verify=False)
|
r = requests.get("https://{0}/api/vms?expand=resources&attributes=all".format(self.cloudforms_hostname),
|
||||||
|
auth=(self.cloudforms_username, self.cloudforms_password), verify=False)
|
||||||
obj = r.json()
|
obj = r.json()
|
||||||
|
|
||||||
#Remove objects that don't matter
|
# Create groups+hosts based on host data
|
||||||
del obj["count"]
|
for resource in obj.get('resources', []):
|
||||||
del obj["subcount"]
|
|
||||||
del obj["name"]
|
|
||||||
|
|
||||||
#Create a new list to grab VMs with power_state on to add to a new list
|
# Maintain backwards compat by creating `Dynamic_CloudForms` group
|
||||||
#I'm sure there is a cleaner way to do this
|
if 'Dynamic_CloudForms' not in self.inventory:
|
||||||
newlist = []
|
self.inventory['Dynamic_CloudForms'] = []
|
||||||
getnext = False
|
self.inventory['Dynamic_CloudForms'].append(resource['name'])
|
||||||
for x in obj.items():
|
|
||||||
for y in x[1]:
|
# Add host to desired groups
|
||||||
for z in y.items():
|
for key in ('vendor', 'type', 'location'):
|
||||||
if getnext == True:
|
if key in resource:
|
||||||
newlist.append(z[1])
|
# Create top-level group
|
||||||
getnext = False
|
if key not in self.inventory:
|
||||||
if ( z[0] == "power_state" and z[1] == "on" ):
|
self.inventory[key] = dict(children=[], vars={}, hosts=[])
|
||||||
getnext = True
|
# if resource['name'] not in self.inventory[key]['hosts']:
|
||||||
newdict = {'hosts': newlist}
|
# self.inventory[key]['hosts'].append(resource['name'])
|
||||||
newdict2 = {'Dynamic_CloudForms': newdict}
|
|
||||||
print json.dumps(newdict2, indent=2)
|
# Create sub-group
|
||||||
|
if resource[key] not in self.inventory:
|
||||||
|
self.inventory[resource[key]] = dict(children=[], vars={}, hosts=[])
|
||||||
|
# self.inventory[resource[key]]['hosts'].append(resource['name'])
|
||||||
|
|
||||||
|
# Add sub-group, as a child of top-level
|
||||||
|
if resource[key] not in self.inventory[key]['children']:
|
||||||
|
self.inventory[key]['children'].append(resource[key])
|
||||||
|
|
||||||
|
# Add host to sub-group
|
||||||
|
if resource['name'] not in self.inventory[resource[key]]:
|
||||||
|
self.inventory[resource[key]]['hosts'].append(resource['name'])
|
||||||
|
|
||||||
|
# Delete 'actions' key
|
||||||
|
del resource['actions']
|
||||||
|
|
||||||
|
# Add _meta hostvars
|
||||||
|
self.inventory['_meta']['hostvars'][resource['name']] = resource
|
||||||
|
|
||||||
|
print json.dumps(self.inventory, indent=2)
|
||||||
|
|
||||||
# Run the script
|
# Run the script
|
||||||
CloudFormsInventory()
|
CloudFormsInventory()
|
||||||
|
|||||||
@@ -682,6 +682,16 @@ SATELLITE6_HOST_FILTER = r'^.+$'
|
|||||||
SATELLITE6_EXCLUDE_EMPTY_GROUPS = True
|
SATELLITE6_EXCLUDE_EMPTY_GROUPS = True
|
||||||
SATELLITE6_INSTANCE_ID_VAR = 'foreman.id'
|
SATELLITE6_INSTANCE_ID_VAR = 'foreman.id'
|
||||||
|
|
||||||
|
# ---------------------
|
||||||
|
# ----- CloudForms -----
|
||||||
|
# ---------------------
|
||||||
|
CLOUDFORMS_ENABLED_VAR = 'power_state'
|
||||||
|
CLOUDFORMS_ENABLED_VALUE = 'on'
|
||||||
|
CLOUDFORMS_GROUP_FILTER = r'^.+$'
|
||||||
|
CLOUDFORMS_HOST_FILTER = r'^.+$'
|
||||||
|
CLOUDFORMS_EXCLUDE_EMPTY_GROUPS = True
|
||||||
|
CLOUDFORMS_INSTANCE_ID_VAR = 'id'
|
||||||
|
|
||||||
# ---------------------
|
# ---------------------
|
||||||
# -- Activity Stream --
|
# -- Activity Stream --
|
||||||
# ---------------------
|
# ---------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user