如何在 Azure Python SDK 中创建 NIC 时指定网络安全组

How to Specify Network Security Group When Creating NIC In Azure Python SDK

现在,我正在创建一个 NIC 供 Azure 上的 VM 使用,但我需要 VM 允许端口 80。为此,我需要使用网络安全组,但我不知道如何在我为我的 VM 创建 NIC 时指定它。这是我当前的代码:nicParams = { 'location': LOCATION, 'ip_configurations': [{ 'name': VM_NAME + "-ipconfig", 'public_ip_address': publicIP, 'subnet': { 'id': subnetInfo.id } }], } nicCreationResult = network_client.network_interfaces.create_or_update(GROUP_NAME, VM_NAME + "-nic", nicParams)

我应该在nicParams中添加什么来指定网络安全组?

您需要获取您的 NSG 的 ID 并在您的第一级 vNIC 的 NIC 参数中引用它:

nicParams = {
    'location': LOCATION,
    'ip_configurations': [{
        'name': VM_NAME + "-ipconfig",
        'public_ip_address': publicIP,
        'subnet': {
            'id': subnetInfo.id
        }
    }],
    'network_security_group': {
        'id': '<NSG-ID-HERE>'
    }
}
nicCreationResult = network_client.network_interfaces.create_or_update(GROUP_NAME, VM_NAME + "-nic", nicParams)

如果 network_security_group 不起作用,请尝试 networkSecurityGroup。