| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = ''' |
| 9 |
--- |
| 10 |
module: azure_rm_dedicatedhost_info |
| 11 |
|
| 12 |
version_added: "3.8.0" |
| 13 |
|
| 14 |
short_description: Retrieves information abount dedicated host |
| 15 |
|
| 16 |
description: |
| 17 |
- Retrieves information about a dedicated host. |
| 18 |
- Lists all of the dedicated hosts in the specified dedicated host group. |
| 19 |
|
| 20 |
options: |
| 21 |
resource_group: |
| 22 |
description: |
| 23 |
- Name of the resource group. |
| 24 |
type: str |
| 25 |
required: True |
| 26 |
host_group_name: |
| 27 |
description: |
| 28 |
- The name of the dedicated host group. |
| 29 |
type: str |
| 30 |
required: True |
| 31 |
name: |
| 32 |
description: |
| 33 |
- Name of the dedicate host. |
| 34 |
type: str |
| 35 |
tags: |
| 36 |
description: |
| 37 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 38 |
type: list |
| 39 |
elements: str |
| 40 |
|
| 41 |
extends_documentation_fragment: |
| 42 |
- azure.azcollection.azure |
| 43 |
|
| 44 |
author: |
| 45 |
- magodo (@magodo) |
| 46 |
- xuzhang3 (@xuzhang3) |
| 47 |
- Fred Sun (@Fred-sun) |
| 48 |
''' |
| 49 |
|
| 50 |
EXAMPLES = ''' |
| 51 |
- name: List facts for one dedicate host |
| 52 |
azure_rm_dedicatedhost_info: |
| 53 |
resource_group: myAzureResourceGroup |
| 54 |
host_group_name: myhostgroup |
| 55 |
|
| 56 |
- name: Get facts for dedicate host |
| 57 |
azure_rm_dedicatedhost_info: |
| 58 |
resource_group: myAzureResourceGroup |
| 59 |
host_group_name: myhostgroup |
| 60 |
name: mydedicatehost |
| 61 |
''' |
| 62 |
|
| 63 |
RETURN = ''' |
| 64 |
dedicate_hosts: |
| 65 |
description: |
| 66 |
- Gets a list of dedicated host. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
host_group_name: |
| 71 |
description: |
| 72 |
- The name of the host group. |
| 73 |
type: str |
| 74 |
returned: always |
| 75 |
sample: myDedicatedHostGroup |
| 76 |
resource_group: |
| 77 |
description: |
| 78 |
- The name of the resource group. |
| 79 |
type: str |
| 80 |
returned: always |
| 81 |
sample: myResourceGroup |
| 82 |
name: |
| 83 |
description: |
| 84 |
- The dedicate hsot name. |
| 85 |
type: str |
| 86 |
returned: always |
| 87 |
sample: myHost |
| 88 |
id: |
| 89 |
description: |
| 90 |
- The dedicate host ID. |
| 91 |
type: str |
| 92 |
returned: always |
| 93 |
sample: "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/HostGroups/myDedicatedHostGroup/hosts/myHost" |
| 94 |
location: |
| 95 |
description: |
| 96 |
- Resource location. |
| 97 |
type: str |
| 98 |
returned: always |
| 99 |
sample: eastus |
| 100 |
tags: |
| 101 |
description: |
| 102 |
- Resource tags. |
| 103 |
type: dict |
| 104 |
returned: always |
| 105 |
sample: {'key1': 'value1'} |
| 106 |
sku: |
| 107 |
description: |
| 108 |
- SKU of the dedicated host for Hardware Generation and VM family. |
| 109 |
type: dict |
| 110 |
returned: always |
| 111 |
sample: {'name': 'DSv3-Type4'} |
| 112 |
auto_replace_on_failure: |
| 113 |
description: |
| 114 |
- Specifies whether the dedicated host should be replaced automatically in case of a failure. |
| 115 |
type: bool |
| 116 |
returned: always |
| 117 |
sample: true |
| 118 |
license_type: |
| 119 |
description: |
| 120 |
- Specifies the software license type that will be applied to the VMs deployed on the dedicated host. |
| 121 |
type: str |
| 122 |
returned: always |
| 123 |
sample: None |
| 124 |
provisioning_state: |
| 125 |
description: |
| 126 |
- The provisioning state, which only appears in the response. |
| 127 |
type: str |
| 128 |
returned: always |
| 129 |
sample: Succeeded |
| 130 |
platform_fault_domain: |
| 131 |
description: |
| 132 |
- Fault domain of the dedicated host within a dedicated host group. |
| 133 |
type: str |
| 134 |
returned: always |
| 135 |
sample: 1 |
| 136 |
virtual_machines: |
| 137 |
description: |
| 138 |
- A list of references to all virtual machines in the Dedicated Host. |
| 139 |
type: str |
| 140 |
returned: always |
| 141 |
sample: [{"id": "/subscriptions/subId/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/vm1"}] |
| 142 |
''' |
| 143 |
|
| 144 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 145 |
|
| 146 |
try: |
| 147 |
from azure.core.exceptions import ResourceNotFoundError |
| 148 |
except Exception: |
| 149 |
|
| 150 |
pass |
| 151 |
|
| 152 |
AZURE_OBJECT_CLASS = 'DedicateHostInfo' |
| 153 |
|
| 154 |
|
| 155 |
class AzureRMDedicateHostInfo(AzureRMModuleBase): |
| 156 |
|
| 157 |
def __init__(self): |
| 158 |
|
| 159 |
|
| 160 |
self.module_arg_spec = dict( |
| 161 |
name=dict(type='str'), |
| 162 |
host_group_name=dict(type='str', required=True), |
| 163 |
resource_group=dict(type='str', required=True), |
| 164 |
tags=dict(type='list', elements='str') |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
self.results = dict( |
| 169 |
changed=False |
| 170 |
) |
| 171 |
|
| 172 |
self.name = None |
| 173 |
self.resource_group = None |
| 174 |
self.host_group_name = None |
| 175 |
self.tags = None |
| 176 |
|
| 177 |
super(AzureRMDedicateHostInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 178 |
|
| 179 |
def exec_module(self, **kwargs): |
| 180 |
|
| 181 |
for key in self.module_arg_spec: |
| 182 |
setattr(self, key, kwargs[key]) |
| 183 |
|
| 184 |
results = [] |
| 185 |
if self.name is not None: |
| 186 |
|
| 187 |
results = self.get_item() |
| 188 |
else: |
| 189 |
|
| 190 |
results = self.list_by_host_group() |
| 191 |
|
| 192 |
self.results['dedicated_hosts'] = [self.host_to_dict(item) for item in results] if results else [] |
| 193 |
|
| 194 |
return self.results |
| 195 |
|
| 196 |
def get_item(self): |
| 197 |
self.log('Get host facts for {0}'.format(self.name)) |
| 198 |
results = [] |
| 199 |
|
| 200 |
try: |
| 201 |
item = self.compute_client.dedicated_hosts.get(self.resource_group, self.host_group_name, self.name) |
| 202 |
except ResourceNotFoundError: |
| 203 |
return [] |
| 204 |
|
| 205 |
|
| 206 |
if item and self.has_tags(item.tags, self.tags): |
| 207 |
results = [item] |
| 208 |
return results |
| 209 |
|
| 210 |
def list_by_host_group(self): |
| 211 |
self.log('List all host for host group - {0}'.format(self.host_group_name)) |
| 212 |
try: |
| 213 |
response = self.compute_client.dedicated_hosts.list_by_host_group(self.resource_group, self.host_group_name) |
| 214 |
except Exception: |
| 215 |
return [] |
| 216 |
|
| 217 |
results = [] |
| 218 |
for item in response: |
| 219 |
if self.has_tags(item.tags, self.tags): |
| 220 |
results.append(item) |
| 221 |
return results |
| 222 |
|
| 223 |
def host_to_dict(self, host): |
| 224 |
result = dict( |
| 225 |
resource_group=self.resource_group, |
| 226 |
host_group_name=self.host_group_name, |
| 227 |
id=host.id, |
| 228 |
name=host.name, |
| 229 |
location=host.location, |
| 230 |
tags=host.tags, |
| 231 |
sku=dict(), |
| 232 |
auto_replace_on_failure=host.auto_replace_on_failure, |
| 233 |
license_type=host.license_type, |
| 234 |
provisioning_state=host.provisioning_state, |
| 235 |
platform_fault_domain=host.platform_fault_domain, |
| 236 |
virtual_machines=[] |
| 237 |
) |
| 238 |
if host.virtual_machines: |
| 239 |
result['virtual_machines'] = [dict(id=item.id) for item in host.virtual_machines] |
| 240 |
|
| 241 |
if host.sku: |
| 242 |
result['sku']['name'] = host.sku.name |
| 243 |
return result |
| 244 |
|
| 245 |
|
| 246 |
def main(): |
| 247 |
AzureRMDedicateHostInfo() |
| 248 |
|
| 249 |
|
| 250 |
if __name__ == '__main__': |
| 251 |
main() |