| 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_virtualmachinescalesetinstance |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure virtual machine scale set instance |
| 16 |
description: |
| 17 |
- Manage Azure virtual machine scale set instance. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
vmss_name: |
| 26 |
description: |
| 27 |
- The name of the VM scale set. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
instance_id: |
| 31 |
description: |
| 32 |
- The instance ID of the virtual machine. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
latest_model: |
| 36 |
type: bool |
| 37 |
description: |
| 38 |
- Set to C(yes) to upgrade to the latest model. |
| 39 |
power_state: |
| 40 |
description: |
| 41 |
- Use this option to change power state of the instance. |
| 42 |
type: str |
| 43 |
choices: |
| 44 |
- 'running' |
| 45 |
- 'stopped' |
| 46 |
- 'deallocated' |
| 47 |
protect_from_scale_in: |
| 48 |
type: bool |
| 49 |
description: |
| 50 |
- turn on/off instance protection from scale in |
| 51 |
protect_from_scale_set_actions: |
| 52 |
type: bool |
| 53 |
description: |
| 54 |
- tun on/off instance protection from scale set actions |
| 55 |
state: |
| 56 |
description: |
| 57 |
- State of the VMSS instance. Use C(present) to update an instance and C(absent) to delete an instance. |
| 58 |
default: present |
| 59 |
type: str |
| 60 |
choices: |
| 61 |
- absent |
| 62 |
- present |
| 63 |
|
| 64 |
extends_documentation_fragment: |
| 65 |
- azure.azcollection.azure |
| 66 |
|
| 67 |
author: |
| 68 |
- Zim Kalinowski (@zikalino) |
| 69 |
|
| 70 |
''' |
| 71 |
|
| 72 |
EXAMPLES = ''' |
| 73 |
- name: Upgrade instance to the latest image |
| 74 |
azure_rm_virtualmachinescalesetinstance: |
| 75 |
resource_group: myResourceGroup |
| 76 |
vmss_name: myVMSS |
| 77 |
instance_id: "2" |
| 78 |
latest_model: true |
| 79 |
|
| 80 |
- name: Turn on protect from scale in |
| 81 |
azure_rm_virtualmachinescalesetinstance: |
| 82 |
resource_group: myResourceGroup |
| 83 |
vmss_name: myVMSS |
| 84 |
instance_id: "2" |
| 85 |
protect_from_scale_in: true |
| 86 |
''' |
| 87 |
|
| 88 |
RETURN = ''' |
| 89 |
instances: |
| 90 |
description: |
| 91 |
- A list of instances. |
| 92 |
returned: always |
| 93 |
type: complex |
| 94 |
contains: |
| 95 |
id: |
| 96 |
description: |
| 97 |
- Instance resource ID. |
| 98 |
returned: always |
| 99 |
type: str |
| 100 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/TestGroup/providers/Microsoft.Compute/scalesets/myscaleset/vms/myvm |
| 101 |
''' |
| 102 |
|
| 103 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 104 |
|
| 105 |
try: |
| 106 |
from azure.core.exceptions import ResourceNotFoundError |
| 107 |
from azure.mgmt.compute import ComputeManagementClient |
| 108 |
except ImportError: |
| 109 |
|
| 110 |
pass |
| 111 |
|
| 112 |
|
| 113 |
class AzureRMVirtualMachineScaleSetInstance(AzureRMModuleBase): |
| 114 |
def __init__(self): |
| 115 |
|
| 116 |
self.module_arg_spec = dict( |
| 117 |
resource_group=dict( |
| 118 |
type='str', |
| 119 |
required=True |
| 120 |
), |
| 121 |
vmss_name=dict( |
| 122 |
type='str', |
| 123 |
required=True |
| 124 |
), |
| 125 |
instance_id=dict( |
| 126 |
type='str', |
| 127 |
required=True |
| 128 |
), |
| 129 |
latest_model=dict( |
| 130 |
type='bool' |
| 131 |
), |
| 132 |
power_state=dict( |
| 133 |
type='str', |
| 134 |
choices=['running', 'stopped', 'deallocated'] |
| 135 |
), |
| 136 |
protect_from_scale_in=dict( |
| 137 |
type='bool' |
| 138 |
), |
| 139 |
protect_from_scale_set_actions=dict( |
| 140 |
type='bool' |
| 141 |
), |
| 142 |
state=dict( |
| 143 |
type='str', |
| 144 |
default='present', |
| 145 |
choices=['present', 'absent'] |
| 146 |
) |
| 147 |
) |
| 148 |
|
| 149 |
self.results = dict( |
| 150 |
changed=False |
| 151 |
) |
| 152 |
self.mgmt_client = None |
| 153 |
self.resource_group = None |
| 154 |
self.vmss_name = None |
| 155 |
self.instance_id = None |
| 156 |
self.latest_model = None |
| 157 |
self.power_state = None |
| 158 |
self.state = None |
| 159 |
self.protect_from_scale_in = None |
| 160 |
self.protect_from_scale_set_actions = None |
| 161 |
super(AzureRMVirtualMachineScaleSetInstance, self).__init__(self.module_arg_spec, supports_tags=False) |
| 162 |
|
| 163 |
def exec_module(self, **kwargs): |
| 164 |
for key in self.module_arg_spec: |
| 165 |
setattr(self, key, kwargs[key]) |
| 166 |
self.mgmt_client = self.get_mgmt_svc_client(ComputeManagementClient, |
| 167 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 168 |
api_version='2024-07-01') |
| 169 |
|
| 170 |
instances = self.get() |
| 171 |
|
| 172 |
if self.state == 'absent': |
| 173 |
for item in instances: |
| 174 |
if not self.check_mode: |
| 175 |
self.delete(item['instance_id']) |
| 176 |
self.results['changed'] = True |
| 177 |
self.results['instances'] = [] |
| 178 |
else: |
| 179 |
if self.latest_model is not None: |
| 180 |
for item in instances: |
| 181 |
if not item.get('latest_model', None): |
| 182 |
if not self.check_mode: |
| 183 |
self.apply_latest_model([item['instance_id']]) |
| 184 |
item['latest_model'] = True |
| 185 |
self.results['changed'] = True |
| 186 |
|
| 187 |
if self.power_state is not None: |
| 188 |
for item in instances: |
| 189 |
if self.power_state == 'stopped' and item['power_state'] not in ['stopped', 'stopping']: |
| 190 |
if not self.check_mode: |
| 191 |
self.stop(item['instance_id']) |
| 192 |
self.results['changed'] = True |
| 193 |
elif self.power_state == 'deallocated' and item['power_state'] not in ['deallocated']: |
| 194 |
if not self.check_mode: |
| 195 |
self.deallocate(item['instance_id']) |
| 196 |
self.results['changed'] = True |
| 197 |
elif self.power_state == 'running' and item['power_state'] not in ['running']: |
| 198 |
if not self.check_mode: |
| 199 |
self.start(item['instance_id']) |
| 200 |
self.results['changed'] = True |
| 201 |
if self.protect_from_scale_in is not None or self.protect_from_scale_set_actions is not None: |
| 202 |
for item in instances: |
| 203 |
protection_policy = item['protection_policy'] |
| 204 |
if protection_policy is None or self.protect_from_scale_in != protection_policy['protect_from_scale_in'] or \ |
| 205 |
self.protect_from_scale_set_actions != protection_policy['protect_from_scale_set_actions']: |
| 206 |
if not self.check_mode: |
| 207 |
self.update_protection_policy(self.instance_id, self.protect_from_scale_in, self.protect_from_scale_set_actions) |
| 208 |
self.results['changed'] = True |
| 209 |
|
| 210 |
self.results['instances'] = [{'id': item['id']} for item in instances] |
| 211 |
return self.results |
| 212 |
|
| 213 |
def get(self): |
| 214 |
response = None |
| 215 |
results = [] |
| 216 |
try: |
| 217 |
response = self.mgmt_client.virtual_machine_scale_set_vms.get(resource_group_name=self.resource_group, |
| 218 |
vm_scale_set_name=self.vmss_name, |
| 219 |
instance_id=self.instance_id) |
| 220 |
self.log("Response : {0}".format(response)) |
| 221 |
except ResourceNotFoundError as e: |
| 222 |
self.log('Could not get facts for Virtual Machine Scale Set VM.') |
| 223 |
|
| 224 |
if response: |
| 225 |
results.append(self.format_response(response)) |
| 226 |
|
| 227 |
return results |
| 228 |
|
| 229 |
def apply_latest_model(self, instance_id): |
| 230 |
try: |
| 231 |
poller = self.compute_client.virtual_machine_scale_sets.begin_update_instances(resource_group_name=self.resource_group, |
| 232 |
vm_scale_set_name=self.vmss_name, |
| 233 |
vm_instance_i_ds={'instance_ids': instance_id}) |
| 234 |
self.get_poller_result(poller) |
| 235 |
except Exception as exc: |
| 236 |
self.log("Error applying latest model {0} - {1}".format(self.vmss_name, str(exc))) |
| 237 |
self.fail("Error applying latest model {0} - {1}".format(self.vmss_name, str(exc))) |
| 238 |
|
| 239 |
def delete(self, instance_id): |
| 240 |
try: |
| 241 |
self.mgmt_client.virtual_machine_scale_set_vms.begin_delete(resource_group_name=self.resource_group, |
| 242 |
vm_scale_set_name=self.vmss_name, |
| 243 |
instance_id=instance_id) |
| 244 |
except Exception as e: |
| 245 |
self.log('Could not delete instance of Virtual Machine Scale Set VM.') |
| 246 |
self.fail('Could not delete instance of Virtual Machine Scale Set VM.') |
| 247 |
|
| 248 |
def start(self, instance_id): |
| 249 |
try: |
| 250 |
self.mgmt_client.virtual_machine_scale_set_vms.begin_start(resource_group_name=self.resource_group, |
| 251 |
vm_scale_set_name=self.vmss_name, |
| 252 |
instance_id=instance_id) |
| 253 |
except Exception as e: |
| 254 |
self.log('Could not start instance of Virtual Machine Scale Set VM.') |
| 255 |
self.fail('Could not start instance of Virtual Machine Scale Set VM.') |
| 256 |
|
| 257 |
def stop(self, instance_id): |
| 258 |
try: |
| 259 |
self.mgmt_client.virtual_machine_scale_set_vms.begin_power_off(resource_group_name=self.resource_group, |
| 260 |
vm_scale_set_name=self.vmss_name, |
| 261 |
instance_id=instance_id) |
| 262 |
except Exception as e: |
| 263 |
self.log('Could not stop instance of Virtual Machine Scale Set VM.') |
| 264 |
self.fail('Could not stop instance of Virtual Machine Scale Set VM.') |
| 265 |
|
| 266 |
def deallocate(self, instance_id): |
| 267 |
try: |
| 268 |
self.mgmt_client.virtual_machine_scale_set_vms.begin_deallocate(resource_group_name=self.resource_group, |
| 269 |
vm_scale_set_name=self.vmss_name, |
| 270 |
instance_id=instance_id) |
| 271 |
except Exception as e: |
| 272 |
self.log('Could not deallocate instance of Virtual Machine Scale Set VM.') |
| 273 |
self.fail('Could not deallocate instance of Virtual Machine Scale Set VM.') |
| 274 |
|
| 275 |
def update_protection_policy(self, instance_id, protect_from_scale_in, protect_from_scale_set_actions): |
| 276 |
try: |
| 277 |
d = {} |
| 278 |
if protect_from_scale_in is not None: |
| 279 |
d['protect_from_scale_in'] = protect_from_scale_in |
| 280 |
if protect_from_scale_set_actions is not None: |
| 281 |
d['protect_from_scale_set_actions'] = protect_from_scale_set_actions |
| 282 |
protection_policy = self.compute_models.VirtualMachineScaleSetVMProtectionPolicy(**d) |
| 283 |
instance = self.mgmt_client.virtual_machine_scale_set_vms.get(resource_group_name=self.resource_group, |
| 284 |
vm_scale_set_name=self.vmss_name, |
| 285 |
instance_id=instance_id) |
| 286 |
instance.protection_policy = protection_policy |
| 287 |
poller = self.mgmt_client.virtual_machine_scale_set_vms.begin_update(resource_group_name=self.resource_group, |
| 288 |
vm_scale_set_name=self.vmss_name, |
| 289 |
instance_id=instance_id, |
| 290 |
parameters=instance) |
| 291 |
self.get_poller_result(poller) |
| 292 |
except Exception as e: |
| 293 |
self.log('Could not update instance protection policy.') |
| 294 |
self.fail('Could not update instance protection policy.') |
| 295 |
|
| 296 |
def format_response(self, item): |
| 297 |
d = item.as_dict() |
| 298 |
instance = None |
| 299 |
power_state = '' |
| 300 |
if d.get('provisioning_state', None) is not None: |
| 301 |
iv = self.mgmt_client.virtual_machine_scale_set_vms.get_instance_view(resource_group_name=self.resource_group, |
| 302 |
vm_scale_set_name=self.vmss_name, |
| 303 |
instance_id=d.get('instance_id', None)).as_dict() |
| 304 |
for index in range(len(iv['statuses'])): |
| 305 |
code = iv['statuses'][index]['code'].split('/') |
| 306 |
if code[0] == 'PowerState': |
| 307 |
power_state = code[1] |
| 308 |
break |
| 309 |
else: |
| 310 |
try: |
| 311 |
instance = self.compute_client.virtual_machines.instance_view(self.resource_group, d.get('instance_id', None)).as_dict() |
| 312 |
vm_instance = self.compute_client.virtual_machines.get(self.resource_group, d.get('instance_id', None)).as_dict() |
| 313 |
except Exception as exc: |
| 314 |
self.fail("Getting Flexible VMSS instance instance failed, name {0} instance view - {1}".format(d.get('instance_id'), str(exc))) |
| 315 |
|
| 316 |
for index in range(len(instance['statuses'])): |
| 317 |
code = instance['statuses'][index]['code'].split('/') |
| 318 |
if code[0] == 'PowerState': |
| 319 |
power_state = code[1] |
| 320 |
elif code[0] == 'OSState' and code[1] == 'generalized': |
| 321 |
power_state = 'generalized' |
| 322 |
break |
| 323 |
elif code[0] == 'ProvisioningState' and code[1] == 'failed': |
| 324 |
power_state = '' |
| 325 |
break |
| 326 |
dd = { |
| 327 |
'id': d.get('id'), |
| 328 |
'tags': d.get('tags'), |
| 329 |
'instance_id': d.get('instance_id'), |
| 330 |
'latest_model': d.get('latest_model_applied'), |
| 331 |
'power_state': power_state, |
| 332 |
'protection_policy': d.get('protection_policy') |
| 333 |
} |
| 334 |
if d.get('provisioning_state') is None: |
| 335 |
dd['tags'] = vm_instance.get('tags', None) |
| 336 |
return dd |
| 337 |
|
| 338 |
|
| 339 |
def main(): |
| 340 |
AzureRMVirtualMachineScaleSetInstance() |
| 341 |
|
| 342 |
|
| 343 |
if __name__ == '__main__': |
| 344 |
main() |