| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_publicipaddress_info |
| 15 |
|
| 16 |
version_added: "0.1.2" |
| 17 |
|
| 18 |
short_description: Get public IP facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for a specific public IP or all public IPs within a resource group. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Only show results for a specific Public IP. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- Limit results by resource group. Required when using name parameter. |
| 31 |
type: str |
| 32 |
tags: |
| 33 |
description: |
| 34 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- azure.azcollection.azure |
| 40 |
|
| 41 |
author: |
| 42 |
- Chris Houseknecht (@chouseknecht) |
| 43 |
- Matt Davis (@nitzmahone) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get facts for one Public IP |
| 48 |
azure_rm_publicipaddress_info: |
| 49 |
resource_group: myResourceGroup |
| 50 |
name: publicip001 |
| 51 |
|
| 52 |
- name: Get facts for all Public IPs within a resource groups |
| 53 |
azure_rm_publicipaddress_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
tags: |
| 56 |
- key:value |
| 57 |
''' |
| 58 |
|
| 59 |
RETURN = ''' |
| 60 |
azure_publicipaddresses: |
| 61 |
description: |
| 62 |
- List of public IP address dicts. |
| 63 |
- Please note that this option will be deprecated in 2.10 when curated format will become the only supported format. |
| 64 |
returned: always |
| 65 |
type: list |
| 66 |
example: [{ |
| 67 |
"etag": 'W/"a31a6d7d-cb18-40a5-b16d-9f4a36c1b18a"', |
| 68 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/pip2001", |
| 69 |
"location": "eastus2", |
| 70 |
"name": "pip2001", |
| 71 |
"properties": { |
| 72 |
"idleTimeoutInMinutes": 4, |
| 73 |
"provisioningState": "Succeeded", |
| 74 |
"publicIPAllocationMethod": "Dynamic", |
| 75 |
"resourceGuid": "29de82f4-a7da-440e-bd3d-9cabb79af95a" |
| 76 |
}, |
| 77 |
"type": "Microsoft.Network/publicIPAddresses" |
| 78 |
}] |
| 79 |
publicipaddresses: |
| 80 |
description: |
| 81 |
- List of publicipaddress. |
| 82 |
- Contains the detail which matches azure_rm_publicipaddress parameters. |
| 83 |
- Returned when the format parameter set to curated. |
| 84 |
returned: always |
| 85 |
type: complex |
| 86 |
contains: |
| 87 |
id: |
| 88 |
description: |
| 89 |
- Resource ID. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: /subscriptions/xxx---xxxxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/publicIPAddresses/pipb57dc95224 |
| 93 |
name: |
| 94 |
description: |
| 95 |
- Name of the public IP address. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: pipb57dc95224 |
| 99 |
type: |
| 100 |
description: |
| 101 |
- Resource type. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: "Microsoft.Network/publicIPAddresses" |
| 105 |
location: |
| 106 |
description: |
| 107 |
- Resource location. |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
sample: eastus |
| 111 |
tags: |
| 112 |
description: |
| 113 |
- Resource tags. |
| 114 |
returned: always |
| 115 |
type: dict |
| 116 |
sample: { |
| 117 |
"delete": "on-exit", |
| 118 |
"testing": "testing" |
| 119 |
} |
| 120 |
allocation_method: |
| 121 |
description: |
| 122 |
- The public IP allocation method. |
| 123 |
- Possible values are C(static) and C(dynamic). |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: static |
| 127 |
version: |
| 128 |
description: |
| 129 |
- The public IP address version. |
| 130 |
- Possible values are C(ipv4) and C(ipv6). |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: ipv4 |
| 134 |
dns_settings: |
| 135 |
description: |
| 136 |
- The FQDN of the DNS record associated with the public IP address. |
| 137 |
returned: always |
| 138 |
type: dict |
| 139 |
sample: { |
| 140 |
"domain_name_label": "ansible-b57dc95985712e45eb8b9c2e", |
| 141 |
"fqdn": "ansible-b57dc95985712e45eb8b9c2e.eastus.cloudapp.azure.com", |
| 142 |
"reverse_fqdn": "ansible-b57dc95985712e45eb8b9c2f.eastus.cloudapp.azure.com" |
| 143 |
} |
| 144 |
ip_tags: |
| 145 |
description: |
| 146 |
- The list of tags associated with the public IP address. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
sample: [ |
| 150 |
{ |
| 151 |
"type": "FirstPartyUsage", |
| 152 |
"value": "Storage" |
| 153 |
} |
| 154 |
] |
| 155 |
ip_address: |
| 156 |
description: |
| 157 |
- The Public IP Prefix this Public IP Address should be allocated from. |
| 158 |
returned: always |
| 159 |
type: str |
| 160 |
sample: 40.121.144.14 |
| 161 |
idle_timeout: |
| 162 |
description: |
| 163 |
- The idle timeout of the public IP address. |
| 164 |
returned: always |
| 165 |
type: int |
| 166 |
sample: 4 |
| 167 |
provisioning_state: |
| 168 |
description: |
| 169 |
- The provisioning state of the PublicIP resource. |
| 170 |
- Possible values is C(Succeeded). |
| 171 |
returned: always |
| 172 |
type: str |
| 173 |
sample: Succeeded |
| 174 |
public_ip_prefix: |
| 175 |
description: |
| 176 |
- The Public IP Prefix this Public IP Address should be allocated from. |
| 177 |
type: dict |
| 178 |
returned: when-used |
| 179 |
sample: {"id": "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/publicIPPrefixes/prefix01"} |
| 180 |
etag: |
| 181 |
description: |
| 182 |
- A unique read-only string that changes whenever the resource is updated. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
sample: "W/'1905ee13-7623-45b1-bc6b-4a12b2fb9d15'" |
| 186 |
sku: |
| 187 |
description: |
| 188 |
- The public IP address SKU. |
| 189 |
returned: always |
| 190 |
type: str |
| 191 |
sample: Standard |
| 192 |
zones: |
| 193 |
description: |
| 194 |
- A list of availability zones denoting the IP allocated for the resource needs to come from. |
| 195 |
returned: always |
| 196 |
type: list |
| 197 |
sample: ['1', '2'] |
| 198 |
''' |
| 199 |
try: |
| 200 |
from azure.core.exceptions import ResourceNotFoundError |
| 201 |
except Exception: |
| 202 |
|
| 203 |
pass |
| 204 |
|
| 205 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 206 |
|
| 207 |
AZURE_OBJECT_CLASS = 'PublicIp' |
| 208 |
|
| 209 |
|
| 210 |
class AzureRMPublicIPInfo(AzureRMModuleBase): |
| 211 |
|
| 212 |
def __init__(self): |
| 213 |
|
| 214 |
self.module_arg_spec = dict( |
| 215 |
name=dict(type='str'), |
| 216 |
resource_group=dict(type='str'), |
| 217 |
tags=dict(type='list', elements='str') |
| 218 |
) |
| 219 |
|
| 220 |
self.results = dict( |
| 221 |
changed=False, |
| 222 |
) |
| 223 |
|
| 224 |
self.name = None |
| 225 |
self.resource_group = None |
| 226 |
self.tags = None |
| 227 |
|
| 228 |
super(AzureRMPublicIPInfo, self).__init__(self.module_arg_spec, |
| 229 |
supports_check_mode=True, |
| 230 |
supports_tags=False, |
| 231 |
facts_module=True) |
| 232 |
|
| 233 |
def exec_module(self, **kwargs): |
| 234 |
|
| 235 |
for key in self.module_arg_spec: |
| 236 |
setattr(self, key, kwargs[key]) |
| 237 |
|
| 238 |
result = [] |
| 239 |
if self.name and not self.resource_group: |
| 240 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 241 |
|
| 242 |
if self.name: |
| 243 |
result = self.get_item() |
| 244 |
elif self.resource_group: |
| 245 |
result = self.list_resource_group() |
| 246 |
else: |
| 247 |
result = self.list_all() |
| 248 |
|
| 249 |
raw = self.filter(result) |
| 250 |
|
| 251 |
self.results['publicipaddresses'] = self.format(raw) |
| 252 |
|
| 253 |
return self.results |
| 254 |
|
| 255 |
def format(self, raw): |
| 256 |
return [self.pip_to_dict(item) for item in raw] |
| 257 |
|
| 258 |
def serialize(self, raw): |
| 259 |
results = [] |
| 260 |
for item in raw: |
| 261 |
pip = self.serialize_obj(item, AZURE_OBJECT_CLASS) |
| 262 |
pip['name'] = item.name |
| 263 |
pip['type'] = item.type |
| 264 |
results.append(pip) |
| 265 |
return results |
| 266 |
|
| 267 |
def filter(self, response): |
| 268 |
return [item for item in response if self.has_tags(item.tags, self.tags)] |
| 269 |
|
| 270 |
|
| 271 |
def pip_to_dict(self, pip): |
| 272 |
result = dict( |
| 273 |
id=pip.id, |
| 274 |
name=pip.name, |
| 275 |
type=pip.type, |
| 276 |
location=pip.location, |
| 277 |
tags=pip.tags, |
| 278 |
allocation_method=pip.public_ip_allocation_method.lower(), |
| 279 |
version=pip.public_ip_address_version.lower(), |
| 280 |
dns_settings=dict(), |
| 281 |
ip_tags=dict(), |
| 282 |
ip_address=pip.ip_address, |
| 283 |
idle_timeout=pip.idle_timeout_in_minutes, |
| 284 |
provisioning_state=pip.provisioning_state, |
| 285 |
etag=pip.etag, |
| 286 |
sku=pip.sku.name, |
| 287 |
zones=pip.zones, |
| 288 |
public_ip_prefix=None |
| 289 |
) |
| 290 |
if pip.dns_settings: |
| 291 |
result['dns_settings']['domain_name_label'] = pip.dns_settings.domain_name_label |
| 292 |
result['dns_settings']['fqdn'] = pip.dns_settings.fqdn |
| 293 |
result['dns_settings']['reverse_fqdn'] = pip.dns_settings.reverse_fqdn |
| 294 |
if pip.ip_tags: |
| 295 |
result['ip_tags'] = [dict(type=x.ip_tag_type, value=x.tag) for x in pip.ip_tags] |
| 296 |
if pip.public_ip_prefix: |
| 297 |
result['public_ip_prefix'] = dict(id=pip.public_ip_prefix.id) |
| 298 |
return result |
| 299 |
|
| 300 |
def get_item(self): |
| 301 |
self.log('Get properties for {0}'.format(self.name)) |
| 302 |
item = None |
| 303 |
try: |
| 304 |
item = self.network_client.public_ip_addresses.get(self.resource_group, self.name) |
| 305 |
except ResourceNotFoundError: |
| 306 |
pass |
| 307 |
return [item] if item else [] |
| 308 |
|
| 309 |
def list_resource_group(self): |
| 310 |
self.log('List items in resource groups') |
| 311 |
try: |
| 312 |
response = self.network_client.public_ip_addresses.list(self.resource_group) |
| 313 |
except ResourceNotFoundError as exc: |
| 314 |
self.fail("Error listing items in resource groups {0} - {1}".format(self.resource_group, str(exc))) |
| 315 |
return response |
| 316 |
|
| 317 |
def list_all(self): |
| 318 |
self.log('List all items') |
| 319 |
try: |
| 320 |
response = self.network_client.public_ip_addresses.list_all() |
| 321 |
except ResourceNotFoundError as exc: |
| 322 |
self.fail("Error listing all items - {0}".format(str(exc))) |
| 323 |
return response |
| 324 |
|
| 325 |
|
| 326 |
def main(): |
| 327 |
AzureRMPublicIPInfo() |
| 328 |
|
| 329 |
|
| 330 |
if __name__ == '__main__': |
| 331 |
main() |