| 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_virtualmachineextension_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure Virtual Machine Extension facts |
| 16 |
description: |
| 17 |
- Get facts of Azure Virtual Machine Extension. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
virtual_machine_name: |
| 26 |
description: |
| 27 |
- The name of the virtual machine containing the extension. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the virtual machine extension. |
| 33 |
type: str |
| 34 |
tags: |
| 35 |
description: |
| 36 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
|
| 40 |
extends_documentation_fragment: |
| 41 |
- azure.azcollection.azure |
| 42 |
|
| 43 |
author: |
| 44 |
- Zim Kalinowski (@zikalino) |
| 45 |
|
| 46 |
''' |
| 47 |
|
| 48 |
EXAMPLES = ''' |
| 49 |
- name: Get information on specific Virtual Machine Extension |
| 50 |
azure_rm_virtualmachineextension_info: |
| 51 |
resource_group: myResourceGroup |
| 52 |
virtual_machine_name: myvm |
| 53 |
name: myextension |
| 54 |
|
| 55 |
- name: List installed Virtual Machine Extensions |
| 56 |
azure_rm_virtualmachineextension_info: |
| 57 |
resource_group: myResourceGroup |
| 58 |
virtual_machine_name: myvm |
| 59 |
''' |
| 60 |
|
| 61 |
RETURN = ''' |
| 62 |
extensions: |
| 63 |
description: |
| 64 |
- A list of dictionaries containing facts for Virtual Machine Extension. |
| 65 |
returned: always |
| 66 |
type: complex |
| 67 |
contains: |
| 68 |
id: |
| 69 |
description: |
| 70 |
- Resource ID. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/myvm/testVM/extens |
| 74 |
ions/myextension" |
| 75 |
resource_group: |
| 76 |
description: |
| 77 |
- Resource group name. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: myResourceGroup |
| 81 |
virtual_machine_name: |
| 82 |
description: |
| 83 |
- Virtual machine name. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: myvm |
| 87 |
name: |
| 88 |
description: |
| 89 |
- Virtual machine name. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: myextension |
| 93 |
location: |
| 94 |
description: |
| 95 |
- The resource location. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: eastus |
| 99 |
publisher: |
| 100 |
description: |
| 101 |
- Extension publisher. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: Microsoft.Azure.Extensions |
| 105 |
type: |
| 106 |
description: |
| 107 |
- Extension type. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
sample: CustomScript |
| 111 |
settings: |
| 112 |
description: |
| 113 |
- Extension specific settings dictionary. |
| 114 |
returned: always |
| 115 |
type: dict |
| 116 |
sample: { 'commandToExecute':'hostname' } |
| 117 |
auto_upgrade_minor_version: |
| 118 |
description: |
| 119 |
- Auto upgrade minor version flag. |
| 120 |
returned: always |
| 121 |
type: bool |
| 122 |
sample: true |
| 123 |
enable_automatic_upgrade: |
| 124 |
description: |
| 125 |
- Enable automatic upgrade flag. |
| 126 |
returned: always |
| 127 |
type: bool |
| 128 |
sample: true |
| 129 |
tags: |
| 130 |
description: |
| 131 |
- Resource tags. |
| 132 |
returned: always |
| 133 |
type: dict |
| 134 |
sample: { "mytag":"abc" } |
| 135 |
provisioning_state: |
| 136 |
description: |
| 137 |
- Provisioning state of the extension. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: Succeeded |
| 141 |
''' |
| 142 |
|
| 143 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 144 |
|
| 145 |
try: |
| 146 |
from azure.core.exceptions import ResourceNotFoundError |
| 147 |
except ImportError: |
| 148 |
|
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class AzureRMVirtualMachineExtensionInfo(AzureRMModuleBase): |
| 153 |
def __init__(self): |
| 154 |
|
| 155 |
self.module_arg_spec = dict( |
| 156 |
resource_group=dict( |
| 157 |
type='str', |
| 158 |
required=True |
| 159 |
), |
| 160 |
virtual_machine_name=dict( |
| 161 |
type='str', |
| 162 |
required=True |
| 163 |
), |
| 164 |
name=dict( |
| 165 |
type='str' |
| 166 |
), |
| 167 |
tags=dict( |
| 168 |
type='list', |
| 169 |
elements='str' |
| 170 |
) |
| 171 |
) |
| 172 |
|
| 173 |
self.results = dict( |
| 174 |
changed=False |
| 175 |
) |
| 176 |
self.resource_group = None |
| 177 |
self.virtual_machine_name = None |
| 178 |
self.name = None |
| 179 |
self.tags = None |
| 180 |
super(AzureRMVirtualMachineExtensionInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 181 |
|
| 182 |
def exec_module(self, **kwargs): |
| 183 |
|
| 184 |
for key in self.module_arg_spec: |
| 185 |
setattr(self, key, kwargs[key]) |
| 186 |
|
| 187 |
if self.name is not None: |
| 188 |
self.results['extensions'] = self.get_extensions() |
| 189 |
else: |
| 190 |
self.results['extensions'] = self.list_extensions() |
| 191 |
|
| 192 |
return self.results |
| 193 |
|
| 194 |
def get_extensions(self): |
| 195 |
response = None |
| 196 |
results = [] |
| 197 |
try: |
| 198 |
response = self.compute_client.virtual_machine_extensions.get(resource_group_name=self.resource_group, |
| 199 |
vm_name=self.virtual_machine_name, |
| 200 |
vm_extension_name=self.name) |
| 201 |
self.log("Response : {0}".format(response)) |
| 202 |
except ResourceNotFoundError as e: |
| 203 |
self.log('Could not get facts for Virtual Machine Extension.') |
| 204 |
|
| 205 |
if response and self.has_tags(response.tags, self.tags): |
| 206 |
results.append(self.format_response(response)) |
| 207 |
|
| 208 |
return results |
| 209 |
|
| 210 |
def list_extensions(self): |
| 211 |
response = None |
| 212 |
results = [] |
| 213 |
try: |
| 214 |
response = self.compute_client.virtual_machine_extensions.list(resource_group_name=self.resource_group, |
| 215 |
vm_name=self.virtual_machine_name) |
| 216 |
self.log("Response : {0}".format(response)) |
| 217 |
except ResourceNotFoundError as e: |
| 218 |
self.log('Could not get facts for Virtual Machine Extension.') |
| 219 |
|
| 220 |
if response is not None and response.value is not None: |
| 221 |
for item in response.value: |
| 222 |
if self.has_tags(item.tags, self.tags): |
| 223 |
results.append(self.format_response(item)) |
| 224 |
|
| 225 |
return results |
| 226 |
|
| 227 |
def format_response(self, item): |
| 228 |
d = item.as_dict() |
| 229 |
d = { |
| 230 |
'id': d.get('id', None), |
| 231 |
'resource_group': self.resource_group, |
| 232 |
'virtual_machine_name': self.virtual_machine_name, |
| 233 |
'location': d.get('location'), |
| 234 |
'name': d.get('name'), |
| 235 |
'publisher': d.get('publisher'), |
| 236 |
'type': d.get('type_properties_type'), |
| 237 |
'settings': d.get('settings'), |
| 238 |
'auto_upgrade_minor_version': d.get('auto_upgrade_minor_version'), |
| 239 |
'enable_automatic_upgrade': d.get('enable_automatic_upgrade', None), |
| 240 |
'tags': d.get('tags', None), |
| 241 |
'provisioning_state': d.get('provisioning_state') |
| 242 |
} |
| 243 |
return d |
| 244 |
|
| 245 |
|
| 246 |
def main(): |
| 247 |
AzureRMVirtualMachineExtensionInfo() |
| 248 |
|
| 249 |
|
| 250 |
if __name__ == '__main__': |
| 251 |
main() |