New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for generalizing VMs #49704
Support for generalizing VMs #49704
Changes from 8 commits
a556a3c
4ffaa92
19839da
747b8a6
aa74143
10c3786
ab72c63
c68af22
c57c23a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,12 @@ | |
- Toggle that controls if the machine is allocated/deallocated, only useful with state='present'. | ||
default: True | ||
type: bool | ||
generalized: | ||
description: | ||
- Use with state 'present' to generalize the machine. Set to true to generalize the machine. | ||
- Please note that this operation is irreversible. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we need stronger message to user, that once generalized, the vm is not usable. |
||
type: bool | ||
version_added: "2.8" | ||
restarted: | ||
description: | ||
- Use with state 'present' to restart a running VM. | ||
|
@@ -496,7 +502,7 @@ | |
|
||
RETURN = ''' | ||
powerstate: | ||
description: Indicates if the state is running, stopped, deallocated | ||
description: Indicates if the state is running, stopped, deallocated, generalized | ||
returned: always | ||
type: string | ||
example: running | ||
|
@@ -666,6 +672,7 @@ | |
try: | ||
from msrestazure.azure_exceptions import CloudError | ||
from msrestazure.tools import parse_resource_id | ||
from msrest.polling import LROPoller | ||
except ImportError: | ||
# This is handled in azure_rm_common | ||
pass | ||
|
@@ -727,6 +734,7 @@ def __init__(self): | |
allocated=dict(type='bool', default=True), | ||
restarted=dict(type='bool', default=False), | ||
started=dict(type='bool', default=True), | ||
generalized=dict(type='bool', default=False), | ||
data_disks=dict(type='list'), | ||
plan=dict(type='dict'), | ||
accept_terms=dict(type='bool', default=False) | ||
|
@@ -765,6 +773,7 @@ def __init__(self): | |
self.allocated = None | ||
self.restarted = None | ||
self.started = None | ||
self.generalized = None | ||
self.differences = None | ||
self.data_disks = None | ||
self.plan = None | ||
|
@@ -954,6 +963,10 @@ def exec_module(self, **kwargs): | |
self.log("CHANGED: virtual machine {0} running and requested state 'stopped'".format(self.name)) | ||
changed = True | ||
powerstate_change = 'poweroff' | ||
elif self.generalized and vm_dict['powerstate'] != 'generalized': | ||
self.log("CHANGED: virtual machine {0} requested to be 'generalized'".format(self.name)) | ||
changed = True | ||
powerstate_change = 'generalized' | ||
|
||
self.differences = differences | ||
|
||
|
@@ -1298,6 +1311,9 @@ def exec_module(self, **kwargs): | |
|
||
elif powerstate_change == 'deallocated': | ||
self.deallocate_vm() | ||
elif powerstate_change == 'generalized': | ||
self.power_off_vm() | ||
self.generalize_vm() | ||
|
||
self.results['ansible_facts']['azure_vm'] = self.serialize_vm(self.get_vm()) | ||
|
||
|
@@ -1343,6 +1359,9 @@ def serialize_vm(self, vm): | |
if vm.instance_view: | ||
result['powerstate'] = next((s.code.replace('PowerState/', '') | ||
for s in vm.instance_view.statuses if s.code.startswith('PowerState')), None) | ||
for s in vm.instance_view.statuses: | ||
if s.code == "OSState/generalized": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lower case to avoid case sensitive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, won't hurt |
||
result['powerstate'] = 'generalized' | ||
|
||
# Expand network interfaces to include config properties | ||
for interface in vm.network_profile.network_interfaces: | ||
|
@@ -1414,6 +1433,17 @@ def deallocate_vm(self): | |
self.fail("Error deallocating virtual machine {0} - {1}".format(self.name, str(exc))) | ||
return True | ||
|
||
def generalize_vm(self): | ||
self.results['actions'].append("Generalize virtual machine {0}".format(self.name)) | ||
self.log("Generalize virtual machine {0}".format(self.name)) | ||
try: | ||
response = self.compute_client.virtual_machines.generalize(self.resource_group, self.name) | ||
if isinstance(response, LROPoller): | ||
self.get_poller_result(response) | ||
except Exception as exc: | ||
self.fail("Error generalizing virtual machine {0} - {1}".format(self.name, str(exc))) | ||
return True | ||
|
||
def delete_vm(self, vm): | ||
vhd_uris = [] | ||
managed_disk_ids = [] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why 'd'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it follows other options, which are "started", "stopped".... etc....