| 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_networkinterface_info |
| 15 |
|
| 16 |
version_added: "0.1.2" |
| 17 |
|
| 18 |
short_description: Get network interface facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for a specific network interface or all network interfaces within a resource group. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Only show results for a specific network interface. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- Name of the resource group containing the network interface(s). Required when searching by name. |
| 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 |
|
| 47 |
EXAMPLES = ''' |
| 48 |
- name: Get facts for one network interface |
| 49 |
azure_rm_networkinterface_info: |
| 50 |
resource_group: myResourceGroup |
| 51 |
name: nic001 |
| 52 |
|
| 53 |
- name: Get network interfaces within a resource group |
| 54 |
azure_rm_networkinterface_info: |
| 55 |
resource_group: myResourceGroup |
| 56 |
|
| 57 |
- name: Get network interfaces by tag |
| 58 |
azure_rm_networkinterface_info: |
| 59 |
resource_group: myResourceGroup |
| 60 |
tags: |
| 61 |
- testing |
| 62 |
- foo:bar |
| 63 |
''' |
| 64 |
|
| 65 |
RETURN = ''' |
| 66 |
networkinterfaces: |
| 67 |
description: |
| 68 |
- List of network interface dicts. Each dict contains parameters can be passed to M(azure.azcollection.azure_rm_networkinterface) module. |
| 69 |
type: complex |
| 70 |
returned: always |
| 71 |
contains: |
| 72 |
id: |
| 73 |
description: |
| 74 |
- Id of the network interface. |
| 75 |
type: str |
| 76 |
returned: always |
| 77 |
sample: "/subscriptions/xxxx-xxxxx/resourceGroups/testRG/providers/Microsoft.Network/networkInterfaces/nic01" |
| 78 |
resource_group: |
| 79 |
description: |
| 80 |
- Name of a resource group where the network interface exists. |
| 81 |
type: str |
| 82 |
returned: always |
| 83 |
sample: testRG |
| 84 |
name: |
| 85 |
description: |
| 86 |
- Name of the network interface. |
| 87 |
type: str |
| 88 |
returned: always |
| 89 |
sample: nic01 |
| 90 |
location: |
| 91 |
description: |
| 92 |
- Azure location. |
| 93 |
type: str |
| 94 |
returned: always |
| 95 |
sample: eastus |
| 96 |
virtual_network: |
| 97 |
description: |
| 98 |
- An existing virtual network with which the network interface will be associated. |
| 99 |
- It is a dict which contains I(name) and I(resource_group) of the virtual network. |
| 100 |
type: complex |
| 101 |
returned: always |
| 102 |
contains: |
| 103 |
name: |
| 104 |
description: |
| 105 |
- The name of the virtual network relate network interface. |
| 106 |
type: str |
| 107 |
returned: always |
| 108 |
sample: vnetnic01 |
| 109 |
resource_gorup: |
| 110 |
description: |
| 111 |
- Resource groups name that exist on the virtual network. |
| 112 |
type: str |
| 113 |
returned: always |
| 114 |
sample: testRG |
| 115 |
subscription_id: |
| 116 |
description: |
| 117 |
- Virtual network Subscription ID. |
| 118 |
type: str |
| 119 |
returned: always |
| 120 |
sample: xxxxxxx-xxxxxxxxxxxxx |
| 121 |
subnet_id: |
| 122 |
description: |
| 123 |
- The subnet's ID. |
| 124 |
type: str |
| 125 |
returned: always |
| 126 |
sample: "/subscriptions/xxx-xxxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/nic01/subnets/sub01" |
| 127 |
subnet: |
| 128 |
description: |
| 129 |
- Name of an existing subnet within the specified virtual network. |
| 130 |
type: str |
| 131 |
returned: always |
| 132 |
sample: sub01 |
| 133 |
tags: |
| 134 |
description: |
| 135 |
- Tags of the network interface. |
| 136 |
type: dict |
| 137 |
returned: always |
| 138 |
sample: {key1: value1, key2: value2} |
| 139 |
ip_configurations: |
| 140 |
description: |
| 141 |
- List of IP configurations, if contains multiple configurations. |
| 142 |
type: complex |
| 143 |
returned: always |
| 144 |
contains: |
| 145 |
name: |
| 146 |
description: |
| 147 |
- Name of the IP configuration. |
| 148 |
type: str |
| 149 |
returned: always |
| 150 |
sample: default |
| 151 |
private_ip_address: |
| 152 |
description: |
| 153 |
- Private IP address for the IP configuration. |
| 154 |
type: str |
| 155 |
returned: always |
| 156 |
sample: 10.10.0.4 |
| 157 |
private_ip_allocation_method: |
| 158 |
description: |
| 159 |
- Private IP allocation method. |
| 160 |
type: str |
| 161 |
returned: always |
| 162 |
sample: Dynamic |
| 163 |
public_ip_address: |
| 164 |
description: |
| 165 |
- Name of the public IP address. None for disable IP address. |
| 166 |
type: str |
| 167 |
returned: always |
| 168 |
sample: null |
| 169 |
public_ip_allocation_method: |
| 170 |
description: |
| 171 |
- Public IP allocation method. |
| 172 |
type: str |
| 173 |
returned: always |
| 174 |
sample: null |
| 175 |
load_balancer_backend_address_pools: |
| 176 |
description: |
| 177 |
- List of existing load-balancer backend address pools associated with the network interface. |
| 178 |
type: list |
| 179 |
returned: always |
| 180 |
sample: null |
| 181 |
application_gateway_backend_address_pools: |
| 182 |
description: |
| 183 |
- List of existing application gateway backend address pools associated with the network interface. |
| 184 |
version_added: "1.10.0" |
| 185 |
type: list |
| 186 |
returned: always |
| 187 |
sample: null |
| 188 |
primary: |
| 189 |
description: |
| 190 |
- Whether the IP configuration is the primary one in the list. |
| 191 |
type: bool |
| 192 |
returned: always |
| 193 |
sample: true |
| 194 |
application_security_groups: |
| 195 |
description: |
| 196 |
- List of Application security groups. |
| 197 |
type: list |
| 198 |
returned: always |
| 199 |
sample: ['/subscriptions/<subsid>/resourceGroups/<rg>/providers/Microsoft.Network/applicationSecurityGroups/myASG'] |
| 200 |
enable_accelerated_networking: |
| 201 |
description: |
| 202 |
- Specifies whether the network interface should be created with the accelerated networking feature or not. |
| 203 |
type: bool |
| 204 |
returned: always |
| 205 |
sample: false |
| 206 |
create_with_security_group: |
| 207 |
description: |
| 208 |
- Specifies whether a default security group should be be created with the NIC. Only applies when creating a new NIC. |
| 209 |
type: bool |
| 210 |
returned: always |
| 211 |
sample: false |
| 212 |
security_group: |
| 213 |
description: |
| 214 |
- A security group resource ID with which to associate the network interface. |
| 215 |
type: str |
| 216 |
returned: always |
| 217 |
sample: null |
| 218 |
enable_ip_forwarding: |
| 219 |
description: |
| 220 |
- Whether to enable IP forwarding |
| 221 |
type: bool |
| 222 |
returned: always |
| 223 |
sample: false |
| 224 |
dns_servers: |
| 225 |
description: |
| 226 |
- Which DNS servers should the NIC lookup. |
| 227 |
- List of IP addresses. |
| 228 |
type: list |
| 229 |
returned: always |
| 230 |
sample: [] |
| 231 |
mac_address: |
| 232 |
description: |
| 233 |
- The MAC address of the network interface. |
| 234 |
type: str |
| 235 |
returned: always |
| 236 |
sample: null |
| 237 |
provisioning_state: |
| 238 |
description: |
| 239 |
- The provisioning state of the network interface. |
| 240 |
type: str |
| 241 |
returned: always |
| 242 |
sample: Succeeded |
| 243 |
dns_settings: |
| 244 |
description: |
| 245 |
- The DNS settings in network interface. |
| 246 |
type: complex |
| 247 |
returned: always |
| 248 |
contains: |
| 249 |
dns_servers: |
| 250 |
description: |
| 251 |
- List of DNS servers IP addresses. |
| 252 |
type: list |
| 253 |
returned: always |
| 254 |
sample: [] |
| 255 |
applied_dns_servers: |
| 256 |
description: |
| 257 |
- If the VM that uses this NIC is part of an Availability Set, then this list will have the union of all DNS servers |
| 258 |
from all NICs that are part of the Availability Set. This property is what is configured on each of those VMs. |
| 259 |
type: list |
| 260 |
returned: always |
| 261 |
sample: [] |
| 262 |
internal_dns_name_label: |
| 263 |
description: |
| 264 |
- Relative DNS name for this NIC used for internal communications between VMs in the same virtual network. |
| 265 |
type: str |
| 266 |
returned: always |
| 267 |
sample: null |
| 268 |
internal_fqdn: |
| 269 |
description: |
| 270 |
- Fully qualified DNS name supporting internal communications between VMs in the same virtual network. |
| 271 |
type: str |
| 272 |
returned: always |
| 273 |
sample: null |
| 274 |
''' |
| 275 |
try: |
| 276 |
from azure.core.exceptions import ResourceNotFoundError |
| 277 |
except Exception: |
| 278 |
|
| 279 |
pass |
| 280 |
|
| 281 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, azure_id_to_dict |
| 282 |
|
| 283 |
|
| 284 |
AZURE_OBJECT_CLASS = 'NetworkInterface' |
| 285 |
|
| 286 |
|
| 287 |
def nic_to_dict(nic): |
| 288 |
ip_configurations = [ |
| 289 |
dict( |
| 290 |
name=config.name, |
| 291 |
private_ip_address=config.private_ip_address, |
| 292 |
private_ip_allocation_method=config.private_ip_allocation_method, |
| 293 |
primary=config.primary if config.primary else False, |
| 294 |
load_balancer_backend_address_pools=([item.id for item in config.load_balancer_backend_address_pools] |
| 295 |
if config.load_balancer_backend_address_pools else None), |
| 296 |
application_gateway_backend_address_pools=([item.id for item in config.application_gateway_backend_address_pools] |
| 297 |
if config.application_gateway_backend_address_pools else None), |
| 298 |
public_ip_address=config.public_ip_address.id if config.public_ip_address else None, |
| 299 |
public_ip_allocation_method=config.public_ip_address.public_ip_allocation_method if config.public_ip_address else None, |
| 300 |
application_security_groups=([asg.id for asg in config.application_security_groups] |
| 301 |
if config.application_security_groups else None) |
| 302 |
) for config in nic.ip_configurations |
| 303 |
] |
| 304 |
config = nic.ip_configurations[0] if len(nic.ip_configurations) > 0 else None |
| 305 |
subnet_dict = azure_id_to_dict(config.subnet.id) if config and config.subnet else None |
| 306 |
subnet = subnet_dict.get('subnets') if subnet_dict else None |
| 307 |
virtual_network = dict( |
| 308 |
resource_group=subnet_dict.get('resourceGroups'), |
| 309 |
subnet_id=config.subnet.id if config and config.subnet else None, |
| 310 |
subscription_id=subnet_dict.get('subscriptions'), |
| 311 |
name=subnet_dict.get('virtualNetworks')) if subnet_dict else None |
| 312 |
return dict( |
| 313 |
id=nic.id, |
| 314 |
resource_group=azure_id_to_dict(nic.id).get('resourceGroups'), |
| 315 |
name=nic.name, |
| 316 |
subnet=subnet, |
| 317 |
virtual_network=virtual_network, |
| 318 |
location=nic.location, |
| 319 |
tags=nic.tags, |
| 320 |
security_group=nic.network_security_group.id if nic.network_security_group else None, |
| 321 |
dns_settings=dict( |
| 322 |
dns_servers=nic.dns_settings.dns_servers, |
| 323 |
applied_dns_servers=nic.dns_settings.applied_dns_servers, |
| 324 |
internal_dns_name_label=nic.dns_settings.internal_dns_name_label, |
| 325 |
internal_fqdn=nic.dns_settings.internal_fqdn |
| 326 |
), |
| 327 |
ip_configurations=ip_configurations, |
| 328 |
mac_address=nic.mac_address, |
| 329 |
enable_ip_forwarding=nic.enable_ip_forwarding, |
| 330 |
provisioning_state=nic.provisioning_state, |
| 331 |
enable_accelerated_networking=nic.enable_accelerated_networking, |
| 332 |
dns_servers=nic.dns_settings.dns_servers, |
| 333 |
) |
| 334 |
|
| 335 |
|
| 336 |
class AzureRMNetworkInterfaceInfo(AzureRMModuleBase): |
| 337 |
|
| 338 |
def __init__(self): |
| 339 |
|
| 340 |
self.module_arg_spec = dict( |
| 341 |
name=dict(type='str'), |
| 342 |
resource_group=dict(type='str'), |
| 343 |
tags=dict(type='list', elements='str') |
| 344 |
) |
| 345 |
|
| 346 |
self.results = dict( |
| 347 |
changed=False, |
| 348 |
) |
| 349 |
|
| 350 |
self.name = None |
| 351 |
self.resource_group = None |
| 352 |
self.tags = None |
| 353 |
|
| 354 |
super(AzureRMNetworkInterfaceInfo, self).__init__(self.module_arg_spec, |
| 355 |
supports_check_mode=True, |
| 356 |
supports_tags=False, |
| 357 |
facts_module=True |
| 358 |
) |
| 359 |
|
| 360 |
def exec_module(self, **kwargs): |
| 361 |
|
| 362 |
for key in self.module_arg_spec: |
| 363 |
setattr(self, key, kwargs[key]) |
| 364 |
|
| 365 |
if self.name and not self.resource_group: |
| 366 |
self.fail("Parameter error: resource group required when filtering by name.") |
| 367 |
|
| 368 |
results = [] |
| 369 |
|
| 370 |
if self.name: |
| 371 |
results = self.get_item() |
| 372 |
elif self.resource_group: |
| 373 |
results = self.list_resource_group() |
| 374 |
else: |
| 375 |
results = self.list_all() |
| 376 |
|
| 377 |
self.results['networkinterfaces'] = self.to_dict_list(results) |
| 378 |
return self.results |
| 379 |
|
| 380 |
def get_item(self): |
| 381 |
self.log('Get properties for {0}'.format(self.name)) |
| 382 |
item = None |
| 383 |
try: |
| 384 |
item = self.network_client.network_interfaces.get(self.resource_group, self.name) |
| 385 |
except ResourceNotFoundError: |
| 386 |
pass |
| 387 |
|
| 388 |
return [item] if item and self.has_tags(item.tags, self.tags) else [] |
| 389 |
|
| 390 |
def list_resource_group(self): |
| 391 |
self.log('List for resource group') |
| 392 |
try: |
| 393 |
response = self.network_client.network_interfaces.list(self.resource_group) |
| 394 |
return [item for item in response if self.has_tags(item.tags, self.tags)] |
| 395 |
except ResourceNotFoundError as exc: |
| 396 |
self.fail("Error listing by resource group {0} - {1}".format(self.resource_group, str(exc))) |
| 397 |
|
| 398 |
def list_all(self): |
| 399 |
self.log('List all') |
| 400 |
try: |
| 401 |
response = self.network_client.network_interfaces.list_all() |
| 402 |
return [item for item in response if self.has_tags(item.tags, self.tags)] |
| 403 |
except ResourceNotFoundError as exc: |
| 404 |
self.fail("Error listing all - {0}".format(str(exc))) |
| 405 |
|
| 406 |
def to_dict_list(self, raws): |
| 407 |
return [nic_to_dict(item) for item in raws] if raws else [] |
| 408 |
|
| 409 |
|
| 410 |
def main(): |
| 411 |
AzureRMNetworkInterfaceInfo() |
| 412 |
|
| 413 |
|
| 414 |
if __name__ == '__main__': |
| 415 |
main() |