【问题标题】:How to pass parameters to ARM template using azure python sdk?如何使用 azure python sdk 将参数传递给 ARM 模板?
【发布时间】:2021-04-30 16:13:23
【问题描述】:

我将以下命令作为 bash 脚本运行。

az group create -n $1-rg -l eastus2

az deployment group create -g $1-rg -n $1-deploy \
  -f ./azure/sensor/trafficmirrorstack.json \
  -p @./azure/sensor/trafficmirrorstack.parameters.json \
  -p CustomerName=$1

az deployment group show -g $1-rg -n $1-deploy 

以下似乎应该可以工作:

  rg_name = f"{name}-rg"
    deploy_name = f"{name}-deploy"
    region = list(region_params.keys())[0]

    # add resource group
    rg_result = resource_client.resource_groups.create_or_update(
        rg_name, 
        {
            "location": region
        }
        )
    print(f"Provisioned resource group {rg_result.name} in the {rg_result.location} region")
    with open("./sensor/trafficmirrorstack.json") as template_file:
        print("!!!!1")
        print(f"{0}".format(template_file.read()))
        #print(f"{0}".format(template_file.read()))
        print("!!!!2")
        template = f"{0}".format(template_file.read())

    parameters = {"CustomerName": { "value": name}}
    deployment_params = { 
        "mode": "Incremental",
        "template": template,
        "parameters": parameters
    }
    # Create deployment
    deployment_create_result = resource_client.deployments.begin_create_or_update(
        rg_name,
        deploy_name,
        {"properties": deployment_params},
        # deployment_params,
    )
    deployment_create_result = deployment_create_result.result()

但是你怎么做相当于“-p @./azure/sensor/trafficmirrorstack.parameters.json -p CustomerName=$1”

提前致谢

【问题讨论】:

  • 据我了解,你想覆盖参数文件中的一对键值对。对?如果有,请参考docs.microsoft.com/en-us/python/api/azure-mgmt-resource/…。我们不能同时使用 parametersLink 和参数。
  • 我的预期问题是我可以在 azure cli 中指定多个“参数”。在我上面的示例中,“-p @./azure/sensor/trafficmirrorstack.parameters.json”是一个文件,“-p CustomerName=$1”是名称值对。 “参数= {“客户名称”:{“值”:名称}}部署参数= {“模式”:“增量”,“模板”:模板,“参数”:参数}”。似乎只允许其中之一
  • 我的问题与这个 github 问题类似。 [github.com/Azure/azure-sdk-for-python/issues/478] 只添加了多个参数指定的附加复杂性。

标签: python azure azure-sdk


【解决方案1】:

当我们使用Azure python sdk部署arm模板时,我们可以使用 resource_client.deployments.begin_create_or_update方法提供参数文件URL或定义参数为json。我们不能同时使用这两种方法。更多详情请参考here

此外,在 azure cli 中,它会读取您提供的多个“参数”,然后在我们部署 arm 模板时将这些“参数”定义为 parameters 的一个 Jobject。更多详情请参考here

【讨论】:

  • 谢谢。我会将其标记为已解决,但我没有足够的声望点。太傻了,azure cli 允许但 sdk 不允许。
  • 该文件说您不能拥有 uri 和 file 但不能同时拥有两者。不是文件和值
  • @DuaneGuthrie 如果您想接受它作为解决方案,请参考meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多