From ad779d6ec001eeed32da52fdc565bfb8fd39aab6 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Fri, 30 Jun 2023 17:32:31 +0200 Subject: [PATCH 01/12] Fixes #993 allow to set boot diagnostics storage account to managed --- plugins/modules/azure_rm_virtualmachine.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 65a8471c7..0c13143fe 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -443,6 +443,7 @@ storage_account: description: - The name of an existing storage account to use for boot diagnostics. + - special string "managed" means the stroage account gets set to None, which means creates is a managed boot diagnostic storage account - If not specified, uses I(storage_account_name) defined one level up. - If storage account is not specified anywhere, and C(enabled) is C(true), a default storage account is created for boot diagnostics data. required: false @@ -1477,9 +1478,13 @@ def exec_module(self, **kwargs): current_boot_diagnostics['enabled'] = self.boot_diagnostics['enabled'] boot_diagnostics_changed = True - boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( - limited=not self.boot_diagnostics['enabled'], vm_dict=vm_dict) - boot_diagnostics_blob = boot_diagnostics_storage_account.primary_endpoints.blob if boot_diagnostics_storage_account else None + if self.boot_diagnostics['storage_account'] == 'managed': + boot_diagnostics_blob = None + else: + boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( + limited=not self.boot_diagnostics['enabled'], vm_dict=vm_dict) + boot_diagnostics_blob = boot_diagnostics_storage_account.primary_endpoints.blob if boot_diagnostics_storage_account else None + if current_boot_diagnostics.get('storageUri') != boot_diagnostics_blob: current_boot_diagnostics['storageUri'] = boot_diagnostics_blob boot_diagnostics_changed = True From b86fbd3e11dc26ec98b97c38d56bf7d2c7a30350 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Mon, 3 Jul 2023 11:47:27 +0200 Subject: [PATCH 02/12] Fix changing boot diagnostics if boot diagnostics are disabled --- plugins/modules/azure_rm_virtualmachine.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 0c13143fe..30ca11f3f 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1955,10 +1955,14 @@ def exec_module(self, **kwargs): ) if self.boot_diagnostics is not None: + storage_uri = None + # storageUri is undefined if boot diagnostics is disabled + if 'storageUri' in vm_dict['properties']['diagnosticsProfile']['bootDiagnostics']: + storage_uri = vm_dict['properties']['diagnosticsProfile']['bootDiagnostics']['storageUri'] vm_resource.diagnostics_profile = self.compute_models.DiagnosticsProfile( boot_diagnostics=self.compute_models.BootDiagnostics( enabled=vm_dict['properties']['diagnosticsProfile']['bootDiagnostics']['enabled'], - storage_uri=vm_dict['properties']['diagnosticsProfile']['bootDiagnostics']['storageUri'])) + storage_uri=storage_uri)) if vm_dict.get('tags'): vm_resource.tags = vm_dict['tags'] From fb8439f53d7667eb2bd1a70dadff1975730c826b Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Wed, 5 Jul 2023 11:49:33 +0200 Subject: [PATCH 03/12] fix blank space Co-authored-by: Fred-sun <37327967+Fred-sun@users.noreply.github.com> --- plugins/modules/azure_rm_virtualmachine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 30ca11f3f..58dae7e24 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1484,7 +1484,6 @@ def exec_module(self, **kwargs): boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( limited=not self.boot_diagnostics['enabled'], vm_dict=vm_dict) boot_diagnostics_blob = boot_diagnostics_storage_account.primary_endpoints.blob if boot_diagnostics_storage_account else None - if current_boot_diagnostics.get('storageUri') != boot_diagnostics_blob: current_boot_diagnostics['storageUri'] = boot_diagnostics_blob boot_diagnostics_changed = True From a0d74891c3cd6c525cc4849a7689d8152676ac3f Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Thu, 6 Jul 2023 13:40:43 +0200 Subject: [PATCH 04/12] Introduce new var type for boot diagnostics --- plugins/modules/azure_rm_virtualmachine.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 58dae7e24..8343177ea 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -440,15 +440,23 @@ - Flag indicating if boot diagnostics are enabled. required: true type: bool + type: + description: + - Should the storage account be managed by azure or a custom storage account + required: false + type: str + choices: + - managed storage_account: description: + - Only used if I(type) is not set - The name of an existing storage account to use for boot diagnostics. - - special string "managed" means the stroage account gets set to None, which means creates is a managed boot diagnostic storage account - If not specified, uses I(storage_account_name) defined one level up. - If storage account is not specified anywhere, and C(enabled) is C(true), a default storage account is created for boot diagnostics data. required: false resource_group: description: + - Only used if I(type) is not set - Resource group where the storage account is located. type: str linux_config: @@ -604,6 +612,7 @@ storage_blob: osdisk.vhd boot_diagnostics: enabled: yes + type: managed image: offer: 0001-com-ubuntu-server-focal publisher: canonical @@ -1478,7 +1487,7 @@ def exec_module(self, **kwargs): current_boot_diagnostics['enabled'] = self.boot_diagnostics['enabled'] boot_diagnostics_changed = True - if self.boot_diagnostics['storage_account'] == 'managed': + if self.boot_diagnostics['type'] == 'managed': boot_diagnostics_blob = None else: boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( From da4b86cda70a005a031278a085564a69bcba5249 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Thu, 6 Jul 2023 13:49:43 +0200 Subject: [PATCH 05/12] Fix type for default case of creating/preexisting storage account --- plugins/modules/azure_rm_virtualmachine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 8343177ea..d180c05ea 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1487,7 +1487,7 @@ def exec_module(self, **kwargs): current_boot_diagnostics['enabled'] = self.boot_diagnostics['enabled'] boot_diagnostics_changed = True - if self.boot_diagnostics['type'] == 'managed': + if 'type' in self.boot_diagnostics and self.boot_diagnostics['type'] == 'managed': boot_diagnostics_blob = None else: boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( From 7e31f0343dc9db5dec39ce3e2c5b2f8fc2a07dfb Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Fri, 7 Jul 2023 09:55:27 +0200 Subject: [PATCH 06/12] Fix managed boot diagnostics for vm creation -- include freds changes --- plugins/modules/azure_rm_virtualmachine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index d180c05ea..2affe7230 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1619,7 +1619,7 @@ def exec_module(self, **kwargs): promotion_code=self.plan.get('promotion_code')) # do this before creating vm_resource as it can modify tags - if self.boot_diagnostics_present and self.boot_diagnostics['enabled']: + if self.boot_diagnostics_present and self.boot_diagnostics['enabled'] and self.boot_diagnostics.get('type') != 'managed': boot_diag_storage_account = self.get_boot_diagnostics_storage_account() vm_resource = self.compute_models.VirtualMachine( @@ -1716,7 +1716,7 @@ def exec_module(self, **kwargs): ) if self.boot_diagnostics_present: - if self.boot_diagnostics['enabled']: + if self.boot_diagnostics['enabled'] and self.boot_diagnostics.get('type') != 'managed': storage_uri = boot_diag_storage_account.primary_endpoints.blob else: storage_uri = None From 8409f877aba784a42428d320409873632389444d Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Mon, 10 Jul 2023 13:15:45 +0200 Subject: [PATCH 07/12] Adress review, add mutually exclusive description --- plugins/modules/azure_rm_virtualmachine.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 2affe7230..086c969ce 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -443,21 +443,22 @@ type: description: - Should the storage account be managed by azure or a custom storage account + - It is mutually exclusive with suboption I(storage_account) required: false type: str choices: - managed storage_account: description: - - Only used if I(type) is not set - The name of an existing storage account to use for boot diagnostics. - If not specified, uses I(storage_account_name) defined one level up. - If storage account is not specified anywhere, and C(enabled) is C(true), a default storage account is created for boot diagnostics data. + - It is mutually exclusive with I(type) required: false resource_group: description: - - Only used if I(type) is not set - Resource group where the storage account is located. + - It is mutually exclusive with I(type) type: str linux_config: description: @@ -1121,7 +1122,10 @@ def __init__(self): ansible_facts=dict(azure_vm=None) ) + mutually_exclusive = [] # [ ('boot_diagnostics.type', 'boot_diagnostics.storage_account'), ('boot_diagnostics.type', 'boot_diagnostics.resource_group') ] TODO: Figure out how thos works on suboptions + super(AzureRMVirtualMachine, self).__init__(derived_arg_spec=self.module_arg_spec, + mutually_exclusive=mutually_exclusive, supports_check_mode=True) @property From d11c156e91783370a16fce178c1729693634f93b Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Tue, 11 Jul 2023 16:47:23 +0200 Subject: [PATCH 08/12] Fix argspec for mutually exvlusiveness inside a suboption thanks to @flowerysong for explaining it :) --- plugins/modules/azure_rm_virtualmachine.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 086c969ce..c4a0064f4 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1057,7 +1057,16 @@ def __init__(self): license_type=dict(type='str', choices=['Windows_Server', 'Windows_Client', 'RHEL_BYOS', 'SLES_BYOS']), vm_identity=dict(type='dict', options=managed_identity_spec), winrm=dict(type='list'), - boot_diagnostics=dict(type='dict'), + boot_diagnostics=dict( + type='dict', + options=dict( + enabled=dict(type='bool', default=False), + type=dict(type='str', choices=['managed']), + storage_account=dict(type='str'), + resource_group=dict(type='str'), + ), + mutually_exclusive=[('type', 'storage_account'), ('type', 'resource_group')], + ), ephemeral_os_disk=dict(type='bool'), windows_config=dict(type='dict', options=windows_configuration_spec), linux_config=dict(type='dict', options=linux_configuration_spec), @@ -1122,10 +1131,7 @@ def __init__(self): ansible_facts=dict(azure_vm=None) ) - mutually_exclusive = [] # [ ('boot_diagnostics.type', 'boot_diagnostics.storage_account'), ('boot_diagnostics.type', 'boot_diagnostics.resource_group') ] TODO: Figure out how thos works on suboptions - super(AzureRMVirtualMachine, self).__init__(derived_arg_spec=self.module_arg_spec, - mutually_exclusive=mutually_exclusive, supports_check_mode=True) @property From 2c993cc126758656dc41ae64f6ec44d311d0d9c8 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Wed, 12 Jul 2023 10:42:36 +0200 Subject: [PATCH 09/12] boot_diagnostics.enabled default is undefined --- plugins/modules/azure_rm_virtualmachine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index c4a0064f4..1dea7e7ad 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1060,7 +1060,7 @@ def __init__(self): boot_diagnostics=dict( type='dict', options=dict( - enabled=dict(type='bool', default=False), + enabled=dict(type='bool'), type=dict(type='str', choices=['managed']), storage_account=dict(type='str'), resource_group=dict(type='str'), From 239058d46e6e5c6159e3413ed4b635adb937a7d1 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Wed, 12 Jul 2023 10:58:59 +0200 Subject: [PATCH 10/12] Fix sanity tests --- plugins/modules/azure_rm_virtualmachine.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 1dea7e7ad..24f5e6fba 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -431,6 +431,7 @@ - The certificate store on the VM to which the certificate should be added. - The specified certificate store is implicitly in the LocalMachine account. boot_diagnostics: + type: dict description: - Manage boot diagnostics settings for a VM. - Boot diagnostics includes a serial console and remote console screenshots. @@ -438,12 +439,11 @@ enabled: description: - Flag indicating if boot diagnostics are enabled. - required: true type: bool type: description: - Should the storage account be managed by azure or a custom storage account - - It is mutually exclusive with suboption I(storage_account) + - It is mutually exclusive with suboption I(storage_account) and I(resource_group) required: false type: str choices: @@ -454,6 +454,7 @@ - If not specified, uses I(storage_account_name) defined one level up. - If storage account is not specified anywhere, and C(enabled) is C(true), a default storage account is created for boot diagnostics data. - It is mutually exclusive with I(type) + type: str required: false resource_group: description: @@ -2704,4 +2705,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file From eb0dc89e7de2a2cc56014fcf1e995d8aecd14293 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Thu, 13 Jul 2023 09:04:05 +0200 Subject: [PATCH 11/12] Adress freds review --- plugins/modules/azure_rm_virtualmachine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 24f5e6fba..8a60b6451 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -1137,7 +1137,7 @@ def __init__(self): @property def boot_diagnostics_present(self): - return self.boot_diagnostics is not None and 'enabled' in self.boot_diagnostics + return self.boot_diagnostics is not None and self.boot_diagnostics.get('enabled') is not None def get_boot_diagnostics_storage_account(self, limited=False, vm_dict=None): """ @@ -1162,8 +1162,8 @@ def get_boot_diagnostics_storage_account(self, limited=False, vm_dict=None): - if not there, None """ bsa = None - if 'storage_account' in self.boot_diagnostics: - if 'resource_group' in self.boot_diagnostics: + if self.boot_diagnostics is not None and self.boot_diagnostics.get('storage_account') is not None: + if self.boot_diagnostics.get('resource_group') is not None: bsa = self.get_storage_account(self.boot_diagnostics['resource_group'], self.boot_diagnostics['storage_account']) else: bsa = self.get_storage_account(self.resource_group, self.boot_diagnostics['storage_account']) @@ -1498,7 +1498,7 @@ def exec_module(self, **kwargs): current_boot_diagnostics['enabled'] = self.boot_diagnostics['enabled'] boot_diagnostics_changed = True - if 'type' in self.boot_diagnostics and self.boot_diagnostics['type'] == 'managed': + if self.boot_diagnostics.get('type') is not None and self.boot_diagnostics['type'] == 'managed': boot_diagnostics_blob = None else: boot_diagnostics_storage_account = self.get_boot_diagnostics_storage_account( From 9ded735bb45f6b5ed916f0f9c2918c10092d2afa Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Thu, 13 Jul 2023 09:08:27 +0200 Subject: [PATCH 12/12] Add single newline at the end --- plugins/modules/azure_rm_virtualmachine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/azure_rm_virtualmachine.py b/plugins/modules/azure_rm_virtualmachine.py index 8a60b6451..dd00d6c0a 100644 --- a/plugins/modules/azure_rm_virtualmachine.py +++ b/plugins/modules/azure_rm_virtualmachine.py @@ -2705,4 +2705,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main()