gpt4 book ai didi

python - 我们可以使用 python sdkcompute_client.virtual_machines.begin_create_or_update 将扩展添加到它创建的 Azure VM 中吗?

转载 作者:行者123 更新时间:2023-12-03 06:41:24 27 4
gpt4 key购买 nike

早上好,

我需要将 AADLoginForLinux 扩展添加到我使用 python sdkcompute_client.virtual_machines.begin_create_or_update 调用启动的虚拟机中。

我发现我也许可以进行休息调用来添加扩展,但我想知道是否可以通过 sdk 调用来完成?有人有添加这样的扩展的示例/示例吗?

最佳答案

我尝试在我的环境中重现相同的结果并得到以下结果:

我使用以下代码创建了 Azure 虚拟机:

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"] = "XXXXXXXX"
resource_client = ResourceManagementClient(credential, subscription_id)
RESOURCE_GROUP_NAME = "Imran"
LOCATION = "eastus"
rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME,
{
"location": LOCATION
}
)

VNET_NAME = "testvnet"
SUBNET_NAME = "subnet1"
IP_NAME = "IP"
IP_CONFIG_NAME = "ipconfig"
NIC_NAME = "testnic"
network_client = NetworkManagementClient(credential, subscription_id)
poller = network_client.virtual_networks.begin_create_or_update(RESOURCE_GROUP_NAME,
VNET_NAME,
{
"location": LOCATION,
"address_space": {
"address_prefixes": ["10.0.0.0/16"]
}
}
)
vnet_result = poller.result()
poller = network_client.subnets.begin_create_or_update(RESOURCE_GROUP_NAME,
VNET_NAME, SUBNET_NAME,
{ "address_prefix": "10.0.0.0/24" }
)
subnet_result = poller.result(
print(f"Provisioned virtual subnet {subnet_result.name} with address prefix {subnet_result.address_prefix}")
poller = network_client.public_ip_addresses.begin_create_or_update(RESOURCE_GROUP_NAME,
IP_NAME,
{
"location": LOCATION,
"sku": { "name": "Standard" },
"public_ip_allocation_method": "Static",
"public_ip_address_version" : "IPV4"
}
)
ip_address_result = poller.result()
poller = network_client.network_interfaces.begin_create_or_update(RESOURCE_GROUP_NAME,
NIC_NAME,
{
"location": LOCATION,
"ip_configurations": [ {
"name": testconfig,
"subnet": { "id": subnet_result.id },
"public_ip_address": {"id": ip_address_result.id }
}]
}
)
nic_result = poller.result()
compute_client = ComputeManagementClient(credential, subscription_id)
VM_NAME = "linuxvm"
USERNAME = "****"
PASSWORD = "****"
poller = compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
{
"location": LOCATION,
"storage_profile": {
"image_reference": {
"publisher": 'Canonical',
"offer": "UbuntuServer",
"sku": "16.04.0-LTS",
"version": "latest"
}
},
"hardware_profile": {
"vm_size": "Standard_DS1_v2"
},
"os_profile": {
"computer_name": VM_NAME,
"admin_username": USERNAME,
"admin_password": PASSWORD
},
"network_profile": {
"network_interfaces": [{
"id": nic_result.id,
}]
}
}
)
vm_result = poller.result()
print(f"Provisioned virtual machine {vm_result.name}")

Azure 虚拟机已成功创建,如下所示:

enter image description here

要在创建 Azure 虚拟机时添加扩展,请使用 VirtualMachineExtensionsOperations 类,如下所示:

VirtualMachineExtensionsOperations(*args, **kwargs) 

begin_create_or_update
(resource_group_name: str,
vm_name: str, vm_extension_name: str, extension_parameters: _models.VirtualMachineExtension, *,
content_type: str = "'application/json'", **kwargs: Any) ->
LROPoller[_models.VirtualMachineExtension]

关于python - 我们可以使用 python sdkcompute_client.virtual_machines.begin_create_or_update 将扩展添加到它创建的 Azure VM 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74477495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com