| 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_sqlserver_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get SQL Server facts |
| 16 |
description: |
| 17 |
- Get facts of SQL Server. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal. |
| 23 |
type: str |
| 24 |
required: True |
| 25 |
server_name: |
| 26 |
description: |
| 27 |
- The name of the server. |
| 28 |
type: str |
| 29 |
|
| 30 |
extends_documentation_fragment: |
| 31 |
- azure.azcollection.azure |
| 32 |
- azure.azcollection.azure_tags |
| 33 |
|
| 34 |
author: |
| 35 |
- Zim Kalinowski (@zikalino) |
| 36 |
|
| 37 |
''' |
| 38 |
|
| 39 |
EXAMPLES = ''' |
| 40 |
- name: Get instance of SQL Server |
| 41 |
azure_rm_sqlserver_info: |
| 42 |
resource_group: myResourceGroup |
| 43 |
server_name: server_name |
| 44 |
|
| 45 |
- name: List instances of SQL Server |
| 46 |
azure_rm_sqlserver_info: |
| 47 |
resource_group: myResourceGroup |
| 48 |
''' |
| 49 |
|
| 50 |
RETURN = ''' |
| 51 |
servers: |
| 52 |
description: |
| 53 |
- A list of dict results where the key is the name of the SQL Server and the values are the facts for that SQL Server. |
| 54 |
returned: always |
| 55 |
type: complex |
| 56 |
contains: |
| 57 |
sqlserver_name: |
| 58 |
description: |
| 59 |
- The key is the name of the server that the values relate to. |
| 60 |
type: complex |
| 61 |
contains: |
| 62 |
id: |
| 63 |
description: |
| 64 |
- Resource ID. |
| 65 |
returned: always |
| 66 |
type: str |
| 67 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Sql/servers/sqlcrudtest-4645 |
| 68 |
name: |
| 69 |
description: |
| 70 |
- Resource name. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: sqlcrudtest-4645 |
| 74 |
type: |
| 75 |
description: |
| 76 |
- Resource type. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: Microsoft.Sql/servers |
| 80 |
location: |
| 81 |
description: |
| 82 |
- Resource location. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: japaneast |
| 86 |
kind: |
| 87 |
description: |
| 88 |
- Kind of sql server. This is metadata used for the Azure portal experience. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: v12.0 |
| 92 |
version: |
| 93 |
description: |
| 94 |
- The version of the server. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: 12.0 |
| 98 |
state: |
| 99 |
description: |
| 100 |
- The state of the server. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: Ready |
| 104 |
fully_qualified_domain_name: |
| 105 |
description: |
| 106 |
- The fully qualified domain name of the server. |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
sample: fully_qualified_domain_name |
| 110 |
minimal_tls_version: |
| 111 |
description: |
| 112 |
- The version TLS clients at which must connect. |
| 113 |
returned: always |
| 114 |
type: str |
| 115 |
sample: 1.2 |
| 116 |
version_added: "1.11.0" |
| 117 |
public_network_access: |
| 118 |
description: |
| 119 |
- Whether or not public endpoint access is allowed for the server. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
sample: Enabled |
| 123 |
version_added: "1.11.0" |
| 124 |
restrict_outbound_network_access: |
| 125 |
description: |
| 126 |
- Whether or not outbound network access is allowed for this server. |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
sample: Enabled |
| 130 |
version_added: "1.11.0" |
| 131 |
admin_username: |
| 132 |
description: |
| 133 |
- Username of the SQL administrator account for server. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
sample: sqladmin |
| 137 |
version_added: "1.11.0" |
| 138 |
administrators: |
| 139 |
description: |
| 140 |
- The Azure Active Directory identity of the server. |
| 141 |
returned: always |
| 142 |
type: dict |
| 143 |
version_added: "1.11.0" |
| 144 |
contains: |
| 145 |
administrator_type: |
| 146 |
description: |
| 147 |
- Type of the Azure AD administrator. |
| 148 |
type: str |
| 149 |
sample: ActiveDirectory |
| 150 |
azure_ad_only_authentication: |
| 151 |
description: |
| 152 |
- Azure AD only authentication enabled. |
| 153 |
type: bool |
| 154 |
sample: False |
| 155 |
login: |
| 156 |
description: |
| 157 |
- Login name of the Azure AD administrator. |
| 158 |
type: str |
| 159 |
sample: MyAzureAdGroup |
| 160 |
principal_type: |
| 161 |
description: |
| 162 |
- Principal Type of the Azure AD administrator. |
| 163 |
type: str |
| 164 |
sample: Group |
| 165 |
sid: |
| 166 |
description: |
| 167 |
- SID (object ID) of the Azure AD administrator. |
| 168 |
type: str |
| 169 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 170 |
tenant_id: |
| 171 |
description: |
| 172 |
- Tenant ID of the Azure AD administrator. |
| 173 |
type: str |
| 174 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 175 |
identity: |
| 176 |
description: |
| 177 |
- Identity for the Server. |
| 178 |
type: complex |
| 179 |
returned: when available |
| 180 |
contains: |
| 181 |
type: |
| 182 |
description: |
| 183 |
- Type of the managed identity |
| 184 |
returned: always |
| 185 |
sample: UserAssigned |
| 186 |
type: str |
| 187 |
user_assigned_identities: |
| 188 |
description: |
| 189 |
- User Assigned Managed Identities and its options |
| 190 |
returned: always |
| 191 |
type: complex |
| 192 |
contains: |
| 193 |
id: |
| 194 |
description: |
| 195 |
- Dict of the user assigned identities IDs associated to the Resource |
| 196 |
returned: always |
| 197 |
type: dict |
| 198 |
elements: dict |
| 199 |
''' |
| 200 |
|
| 201 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 202 |
|
| 203 |
try: |
| 204 |
from azure.core.exceptions import ResourceNotFoundError |
| 205 |
except ImportError: |
| 206 |
|
| 207 |
pass |
| 208 |
|
| 209 |
|
| 210 |
class AzureRMSqlServerInfo(AzureRMModuleBase): |
| 211 |
def __init__(self): |
| 212 |
|
| 213 |
self.module_arg_spec = dict( |
| 214 |
resource_group=dict( |
| 215 |
type='str', |
| 216 |
required=True |
| 217 |
), |
| 218 |
server_name=dict( |
| 219 |
type='str' |
| 220 |
) |
| 221 |
) |
| 222 |
|
| 223 |
self.results = dict( |
| 224 |
changed=False, |
| 225 |
) |
| 226 |
self.resource_group = None |
| 227 |
self.server_name = None |
| 228 |
super(AzureRMSqlServerInfo, self).__init__(self.module_arg_spec, supports_check_mode=True) |
| 229 |
|
| 230 |
def exec_module(self, **kwargs): |
| 231 |
|
| 232 |
for key in self.module_arg_spec: |
| 233 |
setattr(self, key, kwargs[key]) |
| 234 |
|
| 235 |
if (self.resource_group is not None and |
| 236 |
self.server_name is not None): |
| 237 |
self.results['servers'] = self.get() |
| 238 |
elif (self.resource_group is not None): |
| 239 |
self.results['servers'] = self.list_by_resource_group() |
| 240 |
return self.results |
| 241 |
|
| 242 |
def get(self): |
| 243 |
''' |
| 244 |
Gets facts of the specified SQL Server. |
| 245 |
|
| 246 |
:return: deserialized SQL Serverinstance state dictionary |
| 247 |
''' |
| 248 |
response = None |
| 249 |
results = {} |
| 250 |
try: |
| 251 |
response = self.sql_client.servers.get(resource_group_name=self.resource_group, |
| 252 |
server_name=self.server_name) |
| 253 |
self.log("Response : {0}".format(response)) |
| 254 |
except ResourceNotFoundError: |
| 255 |
self.log('Could not get facts for Servers.') |
| 256 |
|
| 257 |
if response is not None: |
| 258 |
results[response.name] = self.format_results(response.as_dict()) |
| 259 |
|
| 260 |
return results |
| 261 |
|
| 262 |
def list_by_resource_group(self): |
| 263 |
''' |
| 264 |
Gets facts of the specified SQL Server. |
| 265 |
|
| 266 |
:return: deserialized SQL Serverinstance state dictionary |
| 267 |
''' |
| 268 |
response = None |
| 269 |
results = {} |
| 270 |
try: |
| 271 |
response = self.sql_client.servers.list_by_resource_group(resource_group_name=self.resource_group) |
| 272 |
self.log("Response : {0}".format(response)) |
| 273 |
except ResourceNotFoundError: |
| 274 |
self.log('Could not get facts for Servers.') |
| 275 |
|
| 276 |
if response is not None: |
| 277 |
for item in response: |
| 278 |
results[item.name] = self.format_results(item.as_dict()) |
| 279 |
|
| 280 |
return results |
| 281 |
|
| 282 |
def format_results(self, response): |
| 283 |
administrators = response.get("administrators") |
| 284 |
return { |
| 285 |
"id": response.get("id"), |
| 286 |
"name": response.get("name"), |
| 287 |
"type": response.get("type"), |
| 288 |
"location": response.get("location"), |
| 289 |
"kind": response.get("kind"), |
| 290 |
"version": response.get("version"), |
| 291 |
"state": response.get("state"), |
| 292 |
"tags": response.get("tags", {}), |
| 293 |
"fully_qualified_domain_name": response.get("fully_qualified_domain_name"), |
| 294 |
"minimal_tls_version": response.get("minimal_tls_version"), |
| 295 |
"public_network_access": response.get("public_network_access"), |
| 296 |
"restrict_outbound_network_access": response.get("restrict_outbound_network_access"), |
| 297 |
"admin_username": response.get("administrator_login"), |
| 298 |
"administrators": None if not administrators else { |
| 299 |
"administrator_type": administrators.get("administrator_type"), |
| 300 |
"azure_ad_only_authentication": administrators.get("azure_ad_only_authentication"), |
| 301 |
"login": administrators.get("login"), |
| 302 |
"principal_type": administrators.get("principal_type"), |
| 303 |
"sid": administrators.get("sid"), |
| 304 |
"tenant_id": administrators.get("tenant_id"), |
| 305 |
}, |
| 306 |
"identity": response.get("identity"), |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
def main(): |
| 311 |
AzureRMSqlServerInfo() |
| 312 |
|
| 313 |
|
| 314 |
if __name__ == '__main__': |
| 315 |
main() |