Sign in
agent:

Enable Scheduled Patching for Azure VM

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task involves setting "patchMode" to "AutomaticByPlatform" and "bypassPlatformSafetyChecksOnUserSchedule" to True. This configures VMs for automatic updates on a customer managed schedule(a prerequisite to enable scheduled patching successfully).

import json def extract_vm_names_and_resource_groups(processed_vms): extracted_details = [] for vm in processed_vms: # Directly extract 'vm_name' and 'resource_group' from each dictionary vm_name = vm.get("vm_name") resource_group = vm.get("resource_group") extracted_details.append((vm_name, resource_group)) return extracted_details def construct_and_execute_patch_commands(subscription_id, resource_groups, vm_names, api_version, body_dict): if len(vm_names) != len(resource_groups): print("Error: The lengths of vm_names and resource_groups do not match.") return # Convert the Python dictionary to a JSON string body_json = json.dumps(body_dict, ensure_ascii=False).replace('True', 'true').replace('False', 'false') # Correctly replace Python True to JSON true for vm_name, resource_group in zip(vm_names, resource_groups): url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Compute/virtualMachines/{vm_name}?api-version={api_version}" # Construct the full command. Note: For actual execution, consider splitting and escaping correctly. command = f"""az rest --method patch --url '{url}' --body '{body_json}'""" print(f"Executing patch command for VM: {vm_name} in resource_group: {resource_group}") result = _exe(None, command) #print(result) print(f"Enabled Scheduled Patching(Customer Managed Schedules) for Azure VM: {vm_name}") # Extract VM names and resource groups extracted_vm_details = extract_vm_names_and_resource_groups(processed_vms) #print(extracted_vm_details) api_version = "2024-03-01" body_dict = { "properties": { "osProfile": { "windowsConfiguration": { "provisionVMAgent": True, "enableAutomaticUpdates": True, "patchSettings": { "patchMode": "AutomaticByPlatform", "automaticByPlatformSettings": { "bypassPlatformSafetyChecksOnUserSchedule": True } } } } } } # Unpack the extracted VM details into separate lists vm_names, resource_groups = zip(*extracted_vm_details) # Execute patch commands for extracted VMs construct_and_execute_patch_commands(subscription_id, list(resource_groups), list(vm_names), api_version, body_dict)
copied