python - 如何通过Python SDK创建Azure网络安全组

标签 python azure

我正在使用 Azure Python SDK 部署 Azure VM。我可以通过 Azure 门户创建具有网络安全组的 VM,没有任何问题。但是,我无法使用如下 API 创建网络安全组:

async_nsg_create=network_client.network_security_groups.begin_create_or_update(
    GROUP_NAME,
    NSG_NAME,
    nsg_parameters
)

它总是提示我“无权执行操作‘Microsoft.Network/networkSecurityGroups/write’”。 但是,我可以通过 Azure 门户创建网络安全组,方法是单击“创建资源”或在资源组中添加新源。我怀疑我可能必须通过 ResourceManagementClient 创建 NSG,但我在 API 文档中找不到任何有用的信息:https://learn.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.resourcemanagementclient?view=azure-python#models-api-version--2020-06-01--

我查了这个问题的解决方案:enter link description here ,但在步骤: resource_client.providers.register('Microsoft.Compute') 处失败,并提示:“无权执行操作 'Microsoft.Compute/register/action'”

最佳答案

该错误表示您的客户端没有执行操作的权限,您需要将其添加为资源组/订阅中的 RBAC 角色。

However, I can create a Network Security Group via the Azure portal by clicking "create a resource" or add new source in Resource Group.

在门户中,如果您使用代码here,则您正在使用登录门户的帐户。 ,它使用服务主体的凭据,这是不同的。

<小时/>

这是一个适合我的完整示例,您可以按照以下步骤操作。

1. Register an application with Azure AD and create a service principal .

2. Get values for signing increate a new application secret .

3.导航到资源组或订阅 -> 访问控制 (IAM) -> 添加 -> 将 AD 应用程序的服务主体添加为 RBAC 角色例如贡献者,详细信息如下this .

4.然后使用下面的代码。

from azure.identity import ClientSecretCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_06_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2020_06_01.models import SecurityRule

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"
subscription_id = "<subscription-id>"

credential = ClientSecretCredential(tenant_id, client_id, client_secret)
network_client = NetworkManagementClient(credential, subscription_id)

resource_group_name = "<group-name>"
nsg_name = "testnsg"

nsg_params = NetworkSecurityGroup(id= "testnsg", location="UK South", tags={ "name" : "testnsg" })
nsg = network_client.network_security_groups.begin_create_or_update(resource_group_name, "testnsg", parameters=nsg_params)
print(nsg.result().as_dict())

enter image description here

5.检查门户:

enter image description here

更新:

如果您想使用用户帐户,只需使用AzureCliCredential即可。

1.安装Azure CLI ,然后在本地终端中使用 az login 登录您的帐户,例如powershell。

2.登录后,更改如下代码并运行。

from azure.identity import ClientSecretCredential
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.v2020_06_01.models import NetworkSecurityGroup
from azure.mgmt.network.v2020_06_01.models import SecurityRule


subscription_id = "<subscription-id>"

credential = AzureCliCredential()
network_client = NetworkManagementClient(credential, subscription_id)

resource_group_name = "<group-name>"
nsg_name = "testnsg"

nsg_params = NetworkSecurityGroup(id= "testnsg", location="UK South", tags={ "name" : "testnsg" })
nsg = network_client.network_security_groups.begin_create_or_update(resource_group_name, "testnsg", parameters=nsg_params)
print(nsg.result().as_dict())

关于python - 如何通过Python SDK创建Azure网络安全组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64189108/

相关文章:

c# - 移动 Azure 服务 C# .NET 后端补丁未更新

azure - 如何在Azure数据工厂中传递不记名 token API

azure - 访问 Power BI API 时收到 401 Unauthorized

python - 请求模块的 PyInstaller SSL 错误 - 缺少模块 ssl (_ssl)

在 ubuntu 中安装 Python pip

python - 包的层次结构

Python - 多个if语句被忽略

azure - 如何使用 AzureCLI 按日期删除 Azure Blob 存储中的文件?

c# - 如何使用 C# 连接到 Azure 存储表

python - 从一个数据帧到另一个数据帧查找缺失值的正确方法