mirror of
https://github.com/ansible/awx.git
synced 2026-04-14 06:29:25 -02:30
Modify the windows azure inventory script to import all of the instances
and create a new group to identify primary hosts
This commit is contained in:
@@ -65,6 +65,7 @@ class AzureInventory(object):
|
|||||||
self.inventory = {}
|
self.inventory = {}
|
||||||
# Index of deployment name -> host
|
# Index of deployment name -> host
|
||||||
self.index = {}
|
self.index = {}
|
||||||
|
self.host_metadata = {}
|
||||||
|
|
||||||
# Read settings and parse CLI arguments
|
# Read settings and parse CLI arguments
|
||||||
self.read_settings()
|
self.read_settings()
|
||||||
@@ -87,50 +88,38 @@ class AzureInventory(object):
|
|||||||
elif not self.is_cache_valid():
|
elif not self.is_cache_valid():
|
||||||
self.do_api_calls_update_cache()
|
self.do_api_calls_update_cache()
|
||||||
|
|
||||||
if self.args.host:
|
if self.args.list_images:
|
||||||
data_to_print = self.get_host(self.args.host)
|
|
||||||
elif self.args.list_images:
|
|
||||||
data_to_print = self.json_format_dict(self.get_images(), True)
|
data_to_print = self.json_format_dict(self.get_images(), True)
|
||||||
elif self.args.list:
|
elif self.args.list or self.args.host:
|
||||||
# Display list of nodes for inventory
|
# Display list of nodes for inventory
|
||||||
if len(self.inventory) == 0:
|
if len(self.inventory) == 0:
|
||||||
data = json.loads(self.get_inventory_from_cache())
|
data = json.loads(self.get_inventory_from_cache())
|
||||||
else:
|
else:
|
||||||
data = self.inventory
|
data = self.inventory
|
||||||
|
|
||||||
# Add the `_meta` information.
|
if self.args.host:
|
||||||
_meta = {}
|
data_to_print = self.get_host(self.args.host)
|
||||||
if len(data) > 0:
|
else:
|
||||||
for host in set(reduce(lambda x, y: x + y,
|
# Add the `_meta` information.
|
||||||
[i for i in data.values()])):
|
_meta = {}
|
||||||
if host is not None:
|
if len(data) > 0:
|
||||||
_meta[host] = self.get_host(host, jsonify=False)
|
for host in set(reduce(lambda x, y: x + y,
|
||||||
data['_meta'] = _meta
|
[i for i in data.values()])):
|
||||||
|
if host is not None:
|
||||||
# JSONify the data.
|
_meta[host] = self.get_host(host, jsonify=False)
|
||||||
data_to_print = self.json_format_dict(data, pretty=True)
|
data['_meta'] = _meta
|
||||||
|
|
||||||
|
# JSONify the data.
|
||||||
|
data_to_print = self.json_format_dict(data, pretty=True)
|
||||||
print data_to_print
|
print data_to_print
|
||||||
|
|
||||||
def get_host(self, hostname, jsonify=True):
|
def get_host(self, hostname, jsonify=True):
|
||||||
"""Return information about the given hostname, based on what
|
"""Return information about the given hostname, based on what
|
||||||
the Windows Azure API provides.
|
the Windows Azure API provides.
|
||||||
"""
|
"""
|
||||||
# Strip ".cloudapp.net" off of the end of the hostname if
|
|
||||||
# it is present.
|
|
||||||
if hostname.endswith('.cloudapp.net'):
|
|
||||||
hostname = hostname.replace('.cloudapp.net', '')
|
|
||||||
|
|
||||||
# Retrieve information about the host.
|
|
||||||
host = self.sms.get_hosted_service_properties(hostname)
|
|
||||||
hsp = host.hosted_service_properties # Because reasons.
|
|
||||||
answer = {
|
|
||||||
'label': hsp.label,
|
|
||||||
'status': hsp.status.lower(),
|
|
||||||
}
|
|
||||||
if jsonify:
|
if jsonify:
|
||||||
return json.dumps(answer)
|
return json.dumps(self.host_metadata[hostname])
|
||||||
return answer
|
return self.host_metadata[hostname]
|
||||||
|
|
||||||
def get_images(self):
|
def get_images(self):
|
||||||
images = []
|
images = []
|
||||||
@@ -217,8 +206,9 @@ class AzureInventory(object):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
for deployment in self.sms.get_hosted_service_properties(cloud_service.service_name,embed_detail=True).deployments.deployments:
|
for deployment in self.sms.get_hosted_service_properties(cloud_service.service_name,embed_detail=True).deployments.deployments:
|
||||||
if deployment.deployment_slot == "Production":
|
self.add_deployment(cloud_service, deployment)
|
||||||
self.add_deployment(cloud_service, deployment)
|
#if deployment.deployment_slot == "Production":
|
||||||
|
# self.add_deployment(cloud_service, deployment)
|
||||||
except WindowsAzureError as e:
|
except WindowsAzureError as e:
|
||||||
print "Looks like Azure's API is down:"
|
print "Looks like Azure's API is down:"
|
||||||
print
|
print
|
||||||
@@ -227,20 +217,34 @@ class AzureInventory(object):
|
|||||||
|
|
||||||
def add_deployment(self, cloud_service, deployment):
|
def add_deployment(self, cloud_service, deployment):
|
||||||
"""Adds a deployment to the inventory and index"""
|
"""Adds a deployment to the inventory and index"""
|
||||||
|
for role in deployment.role_instance_list.role_instances:
|
||||||
|
for ie in role.instance_endpoints.instance_endpoints:
|
||||||
|
if ie.name == 'SSH':
|
||||||
|
self.add_instance(role.instance_name, deployment, ie.public_port, cloud_service)
|
||||||
|
break
|
||||||
|
|
||||||
|
def add_instance(self, hostname, deployment, ssh_port, cloud_service):
|
||||||
|
"""Adds an instance to the inventory and index"""
|
||||||
|
|
||||||
dest = urlparse(deployment.url).hostname
|
dest = urlparse(deployment.url).hostname
|
||||||
|
|
||||||
# Add to index
|
# Add to index
|
||||||
self.index[dest] = deployment.name
|
self.index[hostname] = deployment.name
|
||||||
|
|
||||||
|
self.host_metadata[hostname] = dict(ansible_ssh_host=dest,
|
||||||
|
ansible_ssh_port=int(ssh_port))
|
||||||
|
|
||||||
# List of all azure deployments
|
# List of all azure deployments
|
||||||
self.push(self.inventory, "azure", dest)
|
self.push(self.inventory, "azure", hostname)
|
||||||
|
|
||||||
# Inventory: Group by service name
|
# Inventory: Group by service name
|
||||||
self.push(self.inventory, self.to_safe(cloud_service.service_name), dest)
|
self.push(self.inventory, self.to_safe(cloud_service.service_name), hostname)
|
||||||
|
|
||||||
|
if int(ssh_port) == 22:
|
||||||
|
self.push(self.inventory, "primaries", hostname)
|
||||||
|
|
||||||
# Inventory: Group by region
|
# Inventory: Group by region
|
||||||
self.push(self.inventory, self.to_safe(cloud_service.hosted_service_properties.location), dest)
|
self.push(self.inventory, self.to_safe(cloud_service.hosted_service_properties.location), hostname)
|
||||||
|
|
||||||
def push(self, my_dict, key, element):
|
def push(self, my_dict, key, element):
|
||||||
"""Pushed an element onto an array that may not have been defined in the dict."""
|
"""Pushed an element onto an array that may not have been defined in the dict."""
|
||||||
|
|||||||
Reference in New Issue
Block a user