| 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_loadbalancer_info |
| 15 |
|
| 16 |
version_added: "0.1.2" |
| 17 |
|
| 18 |
short_description: Get load balancer facts |
| 19 |
|
| 20 |
description: |
| 21 |
- Get facts for a specific load balancer or all load balancers. |
| 22 |
|
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Limit results to a specific resource group. |
| 27 |
type: str |
| 28 |
resource_group: |
| 29 |
description: |
| 30 |
- The resource group to search for the desired load balancer. |
| 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 |
- Thomas Stringer (@trstringer) |
| 43 |
''' |
| 44 |
|
| 45 |
EXAMPLES = ''' |
| 46 |
- name: Get facts for one load balancer |
| 47 |
azure_rm_loadbalancer_info: |
| 48 |
name: Testing |
| 49 |
resource_group: myResourceGroup |
| 50 |
|
| 51 |
- name: Get facts for all load balancers |
| 52 |
azure_rm_loadbalancer_info: |
| 53 |
|
| 54 |
- name: Get facts for all load balancers in a specific resource group |
| 55 |
azure_rm_loadbalancer_info: |
| 56 |
resource_group: myResourceGroup |
| 57 |
|
| 58 |
- name: Get facts by tags |
| 59 |
azure_rm_loadbalancer_info: |
| 60 |
tags: |
| 61 |
- testing |
| 62 |
''' |
| 63 |
|
| 64 |
RETURN = ''' |
| 65 |
loadbalancers: |
| 66 |
description: |
| 67 |
- Gets a list of load balancers. |
| 68 |
returned: always |
| 69 |
type: list |
| 70 |
elements: dict |
| 71 |
sample: [ |
| 72 |
{ |
| 73 |
"etag": "1c83ade9-9dee-4027-860a-d5fabacc184f", |
| 74 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/providers/ |
| 75 |
Microsoft.Network/loadBalancers/testloadbalancer1", |
| 76 |
"location": "centralindia", |
| 77 |
"name": "testloadbalancer1", |
| 78 |
"properties": { |
| 79 |
"backendAddressPools": [], |
| 80 |
"frontendIPConfigurations": [ |
| 81 |
{ |
| 82 |
"etag": "1c83ade9-9dee-4027-860a-d5fabacc184f", |
| 83 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/ |
| 84 |
providers/Microsoft.Network/loadBalancers/testloadbalancer1/ |
| 85 |
frontendIPConfigurations/frontendipconf0", |
| 86 |
"name": "frontendipconf0", |
| 87 |
"properties": { |
| 88 |
"privateIPAllocationMethod": "Dynamic", |
| 89 |
"provisioningState": "Succeeded", |
| 90 |
"publicIPAddress": { |
| 91 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ |
| 92 |
myAzureResourceGroup/providers/Microsoft.Network/publicIPAddresses/testpip" |
| 93 |
} |
| 94 |
}, |
| 95 |
"zones": ["1", "2", "3"], |
| 96 |
"type": "Microsoft.Network/loadBalancers/frontendIPConfigurations" |
| 97 |
} |
| 98 |
], |
| 99 |
"inboundNatPools": [], |
| 100 |
"inboundNatRules": [], |
| 101 |
"loadBalancingRules": [], |
| 102 |
"outboundRules": [], |
| 103 |
"probes": [], |
| 104 |
"provisioningState": "Succeeded", |
| 105 |
"resourceGuid": "0b31ab3e-7c55-438d-92a0-5acdc99b5277" |
| 106 |
}, |
| 107 |
"sku": { |
| 108 |
"name": "Standard" |
| 109 |
}, |
| 110 |
"type": "Microsoft.Network/loadBalancers" |
| 111 |
} |
| 112 |
] |
| 113 |
''' |
| 114 |
|
| 115 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 116 |
|
| 117 |
try: |
| 118 |
from azure.core.exceptions import ResourceNotFoundError |
| 119 |
except Exception: |
| 120 |
|
| 121 |
pass |
| 122 |
|
| 123 |
AZURE_OBJECT_CLASS = 'LoadBalancer' |
| 124 |
|
| 125 |
|
| 126 |
class AzureRMLoadBalancerInfo(AzureRMModuleBase): |
| 127 |
"""Utility class to get load balancer facts""" |
| 128 |
|
| 129 |
def __init__(self): |
| 130 |
|
| 131 |
self.module_args = dict( |
| 132 |
name=dict(type='str'), |
| 133 |
resource_group=dict(type='str'), |
| 134 |
tags=dict(type='list', elements='str') |
| 135 |
) |
| 136 |
|
| 137 |
self.results = dict( |
| 138 |
changed=False, |
| 139 |
loadbalancers=[] |
| 140 |
) |
| 141 |
|
| 142 |
self.name = None |
| 143 |
self.resource_group = None |
| 144 |
self.tags = None |
| 145 |
|
| 146 |
super(AzureRMLoadBalancerInfo, self).__init__( |
| 147 |
derived_arg_spec=self.module_args, |
| 148 |
supports_check_mode=True, |
| 149 |
supports_tags=False, |
| 150 |
facts_module=True |
| 151 |
) |
| 152 |
|
| 153 |
def exec_module(self, **kwargs): |
| 154 |
|
| 155 |
for key in self.module_args: |
| 156 |
setattr(self, key, kwargs[key]) |
| 157 |
|
| 158 |
self.results['loadbalancers'] = ( |
| 159 |
self.get_item() if self.name |
| 160 |
else self.list_items() |
| 161 |
) |
| 162 |
|
| 163 |
return self.results |
| 164 |
|
| 165 |
def get_item(self): |
| 166 |
"""Get a single load balancer""" |
| 167 |
|
| 168 |
self.log('Get properties for {0}'.format(self.name)) |
| 169 |
|
| 170 |
item = None |
| 171 |
result = [] |
| 172 |
|
| 173 |
try: |
| 174 |
item = self.network_client.load_balancers.get(self.resource_group, self.name) |
| 175 |
except ResourceNotFoundError: |
| 176 |
pass |
| 177 |
|
| 178 |
if item and self.has_tags(item.tags, self.tags): |
| 179 |
result = [self.serialize_obj(item, AZURE_OBJECT_CLASS)] |
| 180 |
|
| 181 |
return result |
| 182 |
|
| 183 |
def list_items(self): |
| 184 |
"""Get all load balancers""" |
| 185 |
|
| 186 |
self.log('List all load balancers') |
| 187 |
|
| 188 |
if self.resource_group: |
| 189 |
try: |
| 190 |
response = self.network_client.load_balancers.list(self.resource_group) |
| 191 |
except ResourceNotFoundError as exc: |
| 192 |
self.fail('Failed to list items in resource group {0} - {1}'.format(self.resource_group, str(exc))) |
| 193 |
else: |
| 194 |
try: |
| 195 |
response = self.network_client.load_balancers.list_all() |
| 196 |
except ResourceNotFoundError as exc: |
| 197 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 198 |
|
| 199 |
results = [] |
| 200 |
for item in response: |
| 201 |
if self.has_tags(item.tags, self.tags): |
| 202 |
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS)) |
| 203 |
|
| 204 |
return results |
| 205 |
|
| 206 |
|
| 207 |
def main(): |
| 208 |
"""Main module execution code path""" |
| 209 |
|
| 210 |
AzureRMLoadBalancerInfo() |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == '__main__': |
| 214 |
main() |