使用Python部署Azure ARM模板

2024-06-17 11:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找使用Python为存储帐户部署ARM。也就是说,我正在寻找与powershell的新azresourcegroupdeploymentcmdlet相当的Python版本

我一直在寻找一些链接,但我找不到任何

This link,有一些“deployer”类,但它非常特定于linux虚拟机。我认为应该有一个非常简单的方法。我找不到它


Tags: 方法版本链接linux部署link帐户this
1条回答
网友
1楼 · 发布于 2024-06-17 11:22:27

该示例将使用任何模板,只需稍加修改。具体地说,Deployment.deploy()方法是您要查找带有模板的资源组部署示例的地方

您需要更改的主要内容是属性。本例中的那些特定于Linux VM模板,因此您需要将它们更改为 存储模板,或者在模板中根本不使用参数的情况下删除它们

def deploy(self):
    """Deploy the template to a resource group."""
    self.client.resource_groups.create_or_update(
        self.resource_group,
        {
            'location': 'westus'
        }
    )

    template_path = os.path.join(os.path.dirname(
        __file__), 'templates', 'template.json')
    with open(template_path, 'r') as template_file_fd:
        template = json.load(template_file_fd)

    parameters = {                    #<   - Update this with your template's parameters
        'sshKeyData': self.pub_ssh_key,
        'vmName': 'azure-deployment-sample-vm',
        'dnsLabelPrefix': self.dns_label_prefix
    }
    parameters = {k: {'value': v} for k, v in parameters.items()}

    deployment_properties = {
        'mode': DeploymentMode.incremental,
        'template': template,
        'parameters': parameters
    }

    deployment_async_operation = self.client.deployments.create_or_update(
        self.resource_group,
        'azure-sample',
        deployment_properties
    )
    deployment_async_operation.wait()

相关问题 更多 >