1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

adding deployment facts (#53204)

This commit is contained in:
Zim Kalinowski 2019-03-19 12:10:31 +08:00 committed by GitHub
parent 5be1348081
commit 41be7851cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 259 additions and 0 deletions

View file

@ -0,0 +1,243 @@
#!/usr/bin/python
#
# Copyright (c) 2019 Zim Kalinowski, (@zikalino)
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_deployment_facts
version_added: "2.8"
short_description: Get Azure Deployment facts.
description:
- Get facts of Azure Deployment.
options:
resource_group:
description:
- The name of the resource group.
required: True
name:
description:
- The name of the deployment.
extends_documentation_fragment:
- azure
author:
- "Zim Kalinowski (@zikalino)"
'''
EXAMPLES = '''
- name: Get instance of Deployment
azure_rm_deployment_facts:
resource_group: myResourceGroup
name: myDeployment
'''
RETURN = '''
deployments:
description: A list of dictionaries containing facts for deployments.
returned: always
type: complex
contains:
id:
description:
- The identifier of the resource.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/myD
eployment"
resource_group:
description:
- Resource group name.
returned: always
sample: myResourceGroup
name:
description:
- Deployment name.
returned: always
sample: myDeployment
provisioning_state:
description:
- Provisioning state of the deployment.
returned: always
sample: Succeeded
template_link:
description:
- Link to the template.
returned: always
sample: "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/d01a5c06f4f1bc03a049ca17bbbd6e06d62657b3/101-vm-simple-linux/
azuredeploy.json"
parameters:
description:
- Dictionary containing deployment parameters.
returned: always
type: complex
outputs:
description:
- Dictionary containing deployment outputs.
returned: always
output_resources:
description:
- List of resources.
returned: always
type: complex
contains:
id:
description:
- Resource id.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkI
nterfaces/myNetworkInterface"
name:
description:
- Resource name.
returned: always
type: str
sample: myNetworkInterface
type:
description:
- Resource type.
returned: always
type: str
sample: Microsoft.Network/networkInterfaces
depends_on:
description:
- List of resource ids.
type: list
returned: always
sample:
- "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGropup/providers/Microsoft.Network/virtualNet
works/myVirtualNetwork"
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
try:
from msrestazure.azure_exceptions import CloudError
from azure.mgmt.devtestlabs import DevTestLabsClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass
class AzureRMDeploymentFacts(AzureRMModuleBase):
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
name=dict(
type='str'
)
)
self.results = dict(
changed=False
)
self.resource_group = None
self.name = None
super(AzureRMDeploymentFacts, self).__init__(self.module_arg_spec, supports_tags=False)
def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])
if self.name:
self.results['deployments'] = self.get()
else:
self.results['deployments'] = self.list()
return self.results
def get(self):
response = None
results = []
try:
response = self.rm_client.deployments.get(self.resource_group, deployment_name=self.name)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Deployment.')
if response:
results.append(self.format_response(response))
return results
def list(self):
response = None
results = []
try:
response = self.rm_client.deployments.list(self.resource_group)
self.log("Response : {0}".format(response))
except CloudError as e:
self.log('Could not get facts for Deployment.')
if response is not None:
for item in response:
results.append(self.format_response(item))
return results
def format_response(self, item):
d = item.as_dict()
output_resources = {}
for dependency in d.get('properties', {}).get('dependencies'):
# go through dependent resources
depends_on = []
for depends_on_resource in dependency['depends_on']:
depends_on.append(depends_on_resource['id'])
# append if not in list
if not output_resources.get(depends_on_resource['id']):
sub_resource = {
'id': depends_on_resource['id'],
'name': depends_on_resource['resource_name'],
'type': depends_on_resource['resource_type'],
'depends_on': []
}
output_resources[depends_on_resource['id']] = sub_resource
resource = {
'id': dependency['id'],
'name': dependency['resource_name'],
'type': dependency['resource_type'],
'depends_on': depends_on
}
output_resources[dependency['id']] = resource
# convert dictionary to list
output_resources_list = []
for r in output_resources:
output_resources_list.append(output_resources[r])
d = {
'id': d.get('id'),
'resource_group': self.resource_group,
'name': d.get('name'),
'provisioning_state': d.get('properties', {}).get('provisioning_state'),
'parameters': d.get('properties', {}).get('parameters'),
'outputs': d.get('properties', {}).get('outputs'),
'output_resources': output_resources_list,
'template_link': d.get('properties', {}).get('template_link').get('uri')
}
return d
def main():
AzureRMDeploymentFacts()
if __name__ == '__main__':
main()

View file

@ -27,3 +27,19 @@
ansible_ssh_pass: password123!
groupname: azure_vms
with_items: "{{ output.deployment.instances }}"
- name: Get Deployment Facts
azure_rm_deployment_facts:
resource_group: "{{ resource_group }}"
name: "{{ dns_label }}"
register: output
- debug:
var: output
- name: Assert that values are returned
assert:
that:
- not output.changed
- output.deployments[0]['provisioning_state'] != None
- output.deployments[0]['output_resources'] | length > 0
- output.deployments[0]['outputs'] | length > 0