答案 0 :(得分:2)
首先应创建VM,然后使用Azure自定义脚本扩展来执行VM内的脚本。
compute_client = ComputeManagementClient(credentials, subscription_id)
###Your python code to create VM
.......
compute_client.virtual_machines.create_or_update( 'DEV-Central', computer_name, param_dict )
##Using Azure Custom Script to execute script inside VM
GROUP_NAME = 'shuicli'
vmname = 'shui'
ext_type_name = 'CustomScriptForLinux'
ext_name = 'shuitest'
params_create = {
'location': 'eastus',
'publisher': 'Microsoft.OSTCExtensions',
'virtual_machine_extension_type': ext_type_name,
'type_handler_version': '1.5',
'auto_upgrade_minor_version': True,
'settings': {
'fileUris': ["https://shuilinuxdiag336.blob.core.windows.net/customscriptfiles/test.sh"],
'commandToExecute': "sh test.sh"
},
'protected_settings' : {
'storageAccountName': 'shuilinuxdiag336',
'storageAccountKey': '<your storage account key>'
},
}
ext_poller = compute_client.virtual_machine_extensions.create_or_update(
GROUP_NAME,
vmname,
ext_name,
params_create,
)
ext = ext_poller.result()