| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_deployment_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure Deployment facts |
| 16 |
description: |
| 17 |
- Get facts of Azure Deployment. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
name: |
| 26 |
description: |
| 27 |
- The name of the deployment. |
| 28 |
type: str |
| 29 |
|
| 30 |
extends_documentation_fragment: |
| 31 |
- azure.azcollection.azure |
| 32 |
|
| 33 |
author: |
| 34 |
- Zim Kalinowski (@zikalino) |
| 35 |
|
| 36 |
''' |
| 37 |
|
| 38 |
EXAMPLES = ''' |
| 39 |
- name: Get instance of Deployment |
| 40 |
azure_rm_deployment_info: |
| 41 |
resource_group: myResourceGroup |
| 42 |
name: myDeployment |
| 43 |
''' |
| 44 |
|
| 45 |
RETURN = ''' |
| 46 |
deployments: |
| 47 |
description: |
| 48 |
- A list of dictionaries containing facts for deployments. |
| 49 |
returned: always |
| 50 |
type: complex |
| 51 |
contains: |
| 52 |
id: |
| 53 |
description: |
| 54 |
- The identifier of the resource. |
| 55 |
returned: always |
| 56 |
type: str |
| 57 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/myDeployment" |
| 58 |
resource_group: |
| 59 |
description: |
| 60 |
- Resource group name. |
| 61 |
returned: always |
| 62 |
sample: myResourceGroup |
| 63 |
type: str |
| 64 |
name: |
| 65 |
description: |
| 66 |
- Deployment name. |
| 67 |
returned: always |
| 68 |
type: str |
| 69 |
sample: myDeployment |
| 70 |
provisioning_state: |
| 71 |
description: |
| 72 |
- Provisioning state of the deployment. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: Succeeded |
| 76 |
template_link: |
| 77 |
description: |
| 78 |
- Link to the template. |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/d01a5c06f4f1bc03a049ca17bbbd6e06d62657b3/101-vm-simple-linux/ |
| 82 |
azuredeploy.json" |
| 83 |
parameters: |
| 84 |
description: |
| 85 |
- Dictionary containing deployment parameters. |
| 86 |
returned: always |
| 87 |
type: dict |
| 88 |
outputs: |
| 89 |
description: |
| 90 |
- Dictionary containing deployment outputs. |
| 91 |
returned: always |
| 92 |
type: dict |
| 93 |
output_resources: |
| 94 |
description: |
| 95 |
- List of resources. |
| 96 |
returned: always |
| 97 |
type: complex |
| 98 |
contains: |
| 99 |
id: |
| 100 |
description: |
| 101 |
- Resource id. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkI |
| 105 |
nterfaces/myNetworkInterface" |
| 106 |
name: |
| 107 |
description: |
| 108 |
- Resource name. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: myNetworkInterface |
| 112 |
type: |
| 113 |
description: |
| 114 |
- Resource type. |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: Microsoft.Network/networkInterfaces |
| 118 |
depends_on: |
| 119 |
description: |
| 120 |
- List of resource ids. |
| 121 |
type: list |
| 122 |
returned: always |
| 123 |
sample: |
| 124 |
- "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGropup/providers/Microsoft.Network/virtualNet |
| 125 |
works/myVirtualNetwork" |
| 126 |
''' |
| 127 |
|
| 128 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 129 |
|
| 130 |
try: |
| 131 |
from azure.core.exceptions import ResourceNotFoundError |
| 132 |
except ImportError: |
| 133 |
|
| 134 |
pass |
| 135 |
|
| 136 |
|
| 137 |
class AzureRMDeploymentInfo(AzureRMModuleBase): |
| 138 |
def __init__(self): |
| 139 |
self.module_arg_spec = dict( |
| 140 |
resource_group=dict( |
| 141 |
type='str', |
| 142 |
required=True |
| 143 |
), |
| 144 |
name=dict( |
| 145 |
type='str' |
| 146 |
) |
| 147 |
) |
| 148 |
self.results = dict( |
| 149 |
changed=False |
| 150 |
) |
| 151 |
self.resource_group = None |
| 152 |
self.name = None |
| 153 |
|
| 154 |
super(AzureRMDeploymentInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 155 |
|
| 156 |
def exec_module(self, **kwargs): |
| 157 |
|
| 158 |
for key in self.module_arg_spec: |
| 159 |
setattr(self, key, kwargs[key]) |
| 160 |
|
| 161 |
if self.name: |
| 162 |
self.results['deployments'] = self.get() |
| 163 |
else: |
| 164 |
self.results['deployments'] = self.list() |
| 165 |
|
| 166 |
return self.results |
| 167 |
|
| 168 |
def get(self): |
| 169 |
response = None |
| 170 |
results = [] |
| 171 |
try: |
| 172 |
response = self.rm_client.deployments.get(self.resource_group, deployment_name=self.name) |
| 173 |
self.log("Response : {0}".format(response)) |
| 174 |
except ResourceNotFoundError as e: |
| 175 |
self.log('Could not get facts for Deployment.') |
| 176 |
|
| 177 |
if response: |
| 178 |
results.append(self.format_response(response)) |
| 179 |
|
| 180 |
return results |
| 181 |
|
| 182 |
def list(self): |
| 183 |
response = None |
| 184 |
results = [] |
| 185 |
try: |
| 186 |
response = self.rm_client.deployments.list_by_resource_group(self.resource_group) |
| 187 |
self.log("Response : {0}".format(response)) |
| 188 |
except Exception as e: |
| 189 |
self.log('Could not get facts for Deployment.') |
| 190 |
|
| 191 |
if response is not None: |
| 192 |
for item in response: |
| 193 |
results.append(self.format_response(item)) |
| 194 |
|
| 195 |
return results |
| 196 |
|
| 197 |
def format_response(self, item): |
| 198 |
d = item.as_dict() |
| 199 |
output_resources = {} |
| 200 |
for dependency in d.get('properties', {}).get('dependencies'): |
| 201 |
|
| 202 |
depends_on = [] |
| 203 |
for depends_on_resource in dependency['depends_on']: |
| 204 |
depends_on.append(depends_on_resource['id']) |
| 205 |
|
| 206 |
if not output_resources.get(depends_on_resource['id']): |
| 207 |
sub_resource = { |
| 208 |
'id': depends_on_resource['id'], |
| 209 |
'name': depends_on_resource['resource_name'], |
| 210 |
'type': depends_on_resource['resource_type'], |
| 211 |
'depends_on': [] |
| 212 |
} |
| 213 |
output_resources[depends_on_resource['id']] = sub_resource |
| 214 |
resource = { |
| 215 |
'id': dependency['id'], |
| 216 |
'name': dependency['resource_name'], |
| 217 |
'type': dependency['resource_type'], |
| 218 |
'depends_on': depends_on |
| 219 |
} |
| 220 |
output_resources[dependency['id']] = resource |
| 221 |
|
| 222 |
|
| 223 |
output_resources_list = [] |
| 224 |
for r in output_resources: |
| 225 |
output_resources_list.append(output_resources[r]) |
| 226 |
|
| 227 |
d = { |
| 228 |
'id': d.get('id'), |
| 229 |
'resource_group': self.resource_group, |
| 230 |
'name': d.get('name'), |
| 231 |
'provisioning_state': d.get('properties', {}).get('provisioning_state'), |
| 232 |
'parameters': d.get('properties', {}).get('parameters'), |
| 233 |
'outputs': d.get('properties', {}).get('outputs'), |
| 234 |
'output_resources': output_resources_list, |
| 235 |
'template_link': d.get('properties', {}).get('template_link', {}).get('uri'), |
| 236 |
'correlation_id': d.get('properties', {}).get('correlation_id') |
| 237 |
} |
| 238 |
return d |
| 239 |
|
| 240 |
|
| 241 |
def main(): |
| 242 |
AzureRMDeploymentInfo() |
| 243 |
|
| 244 |
|
| 245 |
if __name__ == '__main__': |
| 246 |
main() |