| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_bastionhost_info |
| 15 |
|
| 16 |
version_added: "1.13.0" |
| 17 |
|
| 18 |
short_description: Get Azure bastion host info |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for Azure bastion host info. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of the bastion host. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- The name of the resource group. |
| 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 |
- xuzhang3 (@xuzhang3) |
| 43 |
- Fred-sun (@Fred-sun) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get bastion host info by name |
| 48 |
azure_rm_bastionhost_info: |
| 49 |
name: bastion-name |
| 50 |
resource_group: myResourceGroup |
| 51 |
|
| 52 |
- name: Get all bastion host by resource group |
| 53 |
azure_rm_bastionhost_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
|
| 56 |
- name: Get all bastion hoste by subscription filter by tags |
| 57 |
azure_rm_bastionhost_info: |
| 58 |
tags: |
| 59 |
- key1 |
| 60 |
- abc |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = ''' |
| 64 |
bastion_host: |
| 65 |
description: |
| 66 |
- List of Azure bastion host info. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
id: |
| 71 |
description: |
| 72 |
- Resource ID of the Azure bastion host. |
| 73 |
sample: "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/bastionHosts/testbastion" |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
name: |
| 77 |
description: |
| 78 |
- Name of the Azure bastion host. |
| 79 |
returned: always |
| 80 |
type: str |
| 81 |
sample: linkservice |
| 82 |
location: |
| 83 |
description: |
| 84 |
- Resource location. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: eastus |
| 88 |
etag: |
| 89 |
description: |
| 90 |
- A unique read-only string that changes whenever the resource is updated. |
| 91 |
type: str |
| 92 |
returned: always |
| 93 |
sample: "fb0e3a90-6afa-4a01-9171-9c84d144a0f3" |
| 94 |
type: |
| 95 |
description: |
| 96 |
- The resource type. |
| 97 |
type: str |
| 98 |
returned: always |
| 99 |
sample: Microsoft.Network/bastionHosts |
| 100 |
tags: |
| 101 |
description: |
| 102 |
- The resource tags. |
| 103 |
type: list |
| 104 |
returned: always |
| 105 |
sample: { 'key1': 'value1' } |
| 106 |
provisioning_state: |
| 107 |
description: |
| 108 |
- The provisioning state of the bastion host resource. |
| 109 |
type: str |
| 110 |
returned: always |
| 111 |
sample: Succeeded |
| 112 |
scale_units: |
| 113 |
description: |
| 114 |
- The scale units for the Bastion Host resource. |
| 115 |
type: int |
| 116 |
returned: always |
| 117 |
sample: 2 |
| 118 |
enable_tunneling: |
| 119 |
description: |
| 120 |
- Enable/Disable Tunneling feature of the Bastion Host resource. |
| 121 |
type: bool |
| 122 |
returned: always |
| 123 |
sample: False |
| 124 |
enable_shareable_link: |
| 125 |
description: |
| 126 |
- Enable/Disable Shareable Link of the Bastion Host resource. |
| 127 |
type: bool |
| 128 |
returned: always |
| 129 |
sample: False |
| 130 |
enable_ip_connect: |
| 131 |
description: |
| 132 |
- Enable/Disable IP Connect feature of the Bastion Host resource. |
| 133 |
type: bool |
| 134 |
returned: always |
| 135 |
sample: False |
| 136 |
enable_file_copy: |
| 137 |
description: |
| 138 |
- Enable/Disable File Copy feature of the Bastion Host resource. |
| 139 |
type: bool |
| 140 |
returned: always |
| 141 |
sample: False |
| 142 |
dns_name: |
| 143 |
description: |
| 144 |
- FQDN for the endpoint on which bastion host is accessible. |
| 145 |
type: str |
| 146 |
returned: always |
| 147 |
sample: bst-0ca1e1b6-9969-4167-be54-5972e1395c25.bastion.azure.com |
| 148 |
disable_copy_paste: |
| 149 |
description: |
| 150 |
- Enable/Disable Copy/Paste feature of the Bastion Host resource. |
| 151 |
type: bool |
| 152 |
returned: always |
| 153 |
sample: False |
| 154 |
sku: |
| 155 |
description: |
| 156 |
- The sku of this Bastion Host. |
| 157 |
type: complex |
| 158 |
returned: always |
| 159 |
contains: |
| 160 |
name: |
| 161 |
description: |
| 162 |
- The name of this Bastion Host. |
| 163 |
type: str |
| 164 |
returned: always |
| 165 |
sample: Standard |
| 166 |
ip_configurations: |
| 167 |
description: |
| 168 |
- An array of bastion host IP configurations. |
| 169 |
type: complex |
| 170 |
returned: always |
| 171 |
contains: |
| 172 |
name: |
| 173 |
description: |
| 174 |
- Name of the resource that is unique within a resource group. |
| 175 |
- This name can be used to access the resource. |
| 176 |
type: str |
| 177 |
returned: always |
| 178 |
sample: IpConf |
| 179 |
private_ip_allocation_method: |
| 180 |
description: |
| 181 |
- Private IP allocation method. |
| 182 |
type: str |
| 183 |
returned: always |
| 184 |
sample: Static |
| 185 |
public_ip_address: |
| 186 |
description: |
| 187 |
- Reference of the PublicIP resource. |
| 188 |
type: complex |
| 189 |
returned: always |
| 190 |
contains: |
| 191 |
id: |
| 192 |
description: |
| 193 |
- The ID of the public IP address. |
| 194 |
returned: always |
| 195 |
type: str |
| 196 |
sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/publicIPAddresses/Myip" |
| 197 |
subnet: |
| 198 |
description: |
| 199 |
- The reference to the subnet resource. |
| 200 |
returned: always |
| 201 |
type: complex |
| 202 |
contains: |
| 203 |
id: |
| 204 |
description: |
| 205 |
- The ID of the subnet.. |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.Network/virtualNetworks/vnet/subnets/AzureBastionSubnet" |
| 209 |
''' |
| 210 |
|
| 211 |
try: |
| 212 |
from azure.core.exceptions import ResourceNotFoundError |
| 213 |
except Exception: |
| 214 |
|
| 215 |
pass |
| 216 |
|
| 217 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 218 |
|
| 219 |
|
| 220 |
class AzureRMBastionHostInfo(AzureRMModuleBase): |
| 221 |
|
| 222 |
def __init__(self): |
| 223 |
|
| 224 |
self.module_arg_spec = dict( |
| 225 |
name=dict(type="str"), |
| 226 |
resource_group=dict(type="str"), |
| 227 |
tags=dict(type='list', elements='str') |
| 228 |
) |
| 229 |
|
| 230 |
self.name = None |
| 231 |
self.tags = None |
| 232 |
self.resource_group = None |
| 233 |
self.results = dict( |
| 234 |
changed=False, |
| 235 |
) |
| 236 |
|
| 237 |
super(AzureRMBastionHostInfo, self).__init__(self.module_arg_spec, |
| 238 |
supports_check_mode=True, |
| 239 |
supports_tags=False, |
| 240 |
facts_module=True) |
| 241 |
|
| 242 |
def exec_module(self, **kwargs): |
| 243 |
for key in list(self.module_arg_spec.keys()): |
| 244 |
setattr(self, key, kwargs[key]) |
| 245 |
|
| 246 |
if self.name is not None and self.resource_group is not None: |
| 247 |
result = self.get_item() |
| 248 |
elif self.resource_group is not None: |
| 249 |
result = self.list_resourcegroup() |
| 250 |
else: |
| 251 |
result = self.list_by_subscription() |
| 252 |
|
| 253 |
self.results["bastion_host"] = [item for item in result if item and self.has_tags(item['tags'], self.tags)] |
| 254 |
return self.results |
| 255 |
|
| 256 |
def get_item(self): |
| 257 |
self.log("Get properties for {0} in {1}".format(self.name, self.resource_group)) |
| 258 |
|
| 259 |
try: |
| 260 |
response = self.network_client.bastion_hosts.get(self.resource_group, self.name) |
| 261 |
return [self.bastion_to_dict(response)] |
| 262 |
except ResourceNotFoundError: |
| 263 |
self.log("Could not get info for {0} in {1}".format(self.name, self.resource_group)) |
| 264 |
|
| 265 |
return [] |
| 266 |
|
| 267 |
def list_resourcegroup(self): |
| 268 |
result = [] |
| 269 |
self.log("List all in {0}".format(self.resource_group)) |
| 270 |
try: |
| 271 |
response = self.network_client.bastion_hosts.list_by_resource_group(self.resource_group) |
| 272 |
while True: |
| 273 |
result.append(response.next()) |
| 274 |
except StopIteration: |
| 275 |
pass |
| 276 |
except Exception: |
| 277 |
pass |
| 278 |
return [self.bastion_to_dict(item) for item in result] |
| 279 |
|
| 280 |
def list_by_subscription(self): |
| 281 |
result = [] |
| 282 |
self.log("List all in by subscription") |
| 283 |
try: |
| 284 |
response = self.network_client.bastion_hosts.list() |
| 285 |
while True: |
| 286 |
result.append(response.next()) |
| 287 |
except StopIteration: |
| 288 |
pass |
| 289 |
except Exception: |
| 290 |
pass |
| 291 |
return [self.bastion_to_dict(item) for item in result] |
| 292 |
|
| 293 |
def bastion_to_dict(self, bastion_info): |
| 294 |
bastion = bastion_info.as_dict() |
| 295 |
result = dict( |
| 296 |
id=bastion.get("id"), |
| 297 |
name=bastion.get('name'), |
| 298 |
type=bastion.get('type'), |
| 299 |
etag=bastion.get('etag'), |
| 300 |
location=bastion.get('location'), |
| 301 |
tags=bastion.get('tags'), |
| 302 |
sku=dict(), |
| 303 |
ip_configurations=list(), |
| 304 |
dns_name=bastion.get('dns_name'), |
| 305 |
provisioning_state=bastion.get('provisioning_state'), |
| 306 |
scale_units=bastion.get('scale_units'), |
| 307 |
disable_copy_paste=bastion.get('disable_copy_paste'), |
| 308 |
enable_file_copy=bastion.get('enable_file_copy'), |
| 309 |
enable_ip_connect=bastion.get('enable_ip_connect'), |
| 310 |
enable_shareable_link=bastion.get('enable_tunneling'), |
| 311 |
enable_tunneling=bastion.get('enable_tunneling') |
| 312 |
) |
| 313 |
|
| 314 |
if bastion.get('sku'): |
| 315 |
result['sku']['name'] = bastion['sku']['name'] |
| 316 |
|
| 317 |
if bastion.get('ip_configurations'): |
| 318 |
for items in bastion['ip_configurations']: |
| 319 |
result['ip_configurations'].append( |
| 320 |
{ |
| 321 |
"name": items['name'], |
| 322 |
"subnet": dict(id=items['subnet']['id']), |
| 323 |
"public_ip_address": dict(id=items['public_ip_address']['id']), |
| 324 |
"private_ip_allocation_method": items['private_ip_allocation_method'], |
| 325 |
} |
| 326 |
) |
| 327 |
return result |
| 328 |
|
| 329 |
|
| 330 |
def main(): |
| 331 |
AzureRMBastionHostInfo() |
| 332 |
|
| 333 |
|
| 334 |
if __name__ == "__main__": |
| 335 |
main() |