如何通过Python SDK创建Azure网络安全组
我正在使用Azure Python SDK部署Azure VM。我可以通过Azure门户创建具有网络安全组的VM,而不会出现任何问题。但是,我未能使用API创建网络安全组,如:如何通过Python SDK创建Azure网络安全组,python,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.Netwo
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文档中找不到任何有用的信息:
我检查了此问题中的解决方案:,但在步骤中失败:
resource\u client.providers.register('Microsoft.Compute')
,它抱怨:“没有权限执行操作‘Microsoft.Compute/register/action’”该错误表示您的客户端没有权限执行操作,您需要将其添加为资源组/订阅中的RBAC角色
但是,我可以通过Azure门户通过单击“创建资源”或在资源组中添加新源来创建网络安全组
在门户中,您使用的是登录在门户中的帐户,如果您使用的是代码,则它使用的是服务主体的凭据,这是不同的
这里有一个完整的样本作品供我参考,请按照以下步骤操作 一, 二,。和 3.导航到资源组或订阅->
访问控制(IAM)
->添加
->将广告应用程序的服务主体添加为RBAC角色,例如贡献者
,详细信息如下
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())
票据/代码正在使用哪些凭证?查看参考资料。感谢您的具体回复。我认为我当前的问题是:“订阅->访问控制(IAM)->添加->将广告应用程序的服务主体添加为RBAC”,我无法为我创建的应用程序分配权限。我明天会联系我的管理员。再次回到这个问题。我无法使我的服务负责人具有其他访问权限。我被告知使用我的登录帐户。您是否知道是否有任何方法可以通过使用登录帐户凭据来调用API?
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())