Terraform - 将类型对象作为参数传递给 Azure 模板部署 [英] Terraform - Passing type Object as a parameter to Azure Template Deployment

查看:40
本文介绍了Terraform - 将类型对象作为参数传递给 Azure 模板部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算通过为 Terraform 提供 Azure ARM 模板来使用 Terraform 配置 Azure AD 域服务,这是因为 Terrafrom 不支持在本机配置 Azure AD 域服务.

I am tying to to provision Azure AD Domain Service using Terraform by giving Terraform the Azure ARM template, this is because Terrafrom does not support provisioning Azure AD Domain Service natively.

我已经导出了 ARM 模板及其参数,其中一个参数称为 "notificationSettings",它是一种 Object 类型,如下所示:

I have exported the ARM Template and it's parameters, one of the parameters is called "notificationSettings" which is a type Object and looks like below :

    "notificationSettings": {
        "value": {
            "notifyGlobalAdmins": "Enabled",
            "notifyDcAdmins": "Enabled",
            "additionalRecipients": []
        }
    }

其他参数都是strings,我可以毫无问题地传递它们,例如:

Other parameters are all strings and I can pass them without any issue, for example:

"apiVersion" = "2017-06-01"

我尝试将此对象传递给如下参数:

I have tried passing this object to parameters like below :

"notificationSettings" = [{
                "notifyGlobalAdmins" = "Enabled"
            "notifyDcAdmins" ="Enabled"
            "additionalRecipients" = []
}]

但是,当我执行 terrafrom apply 时,terrafrom 会抱怨并说:

However, when I execute terrafrom apply, terrafrom complains and say:

属性参数"的不适当值:元素notificationSettings":需要字符串.

Inappropriate value for attribute "parameters": element "notificationSettings": string required.

如何将对象类型的参数传递给模板主体?

How do I pass parameters type of Object to template body?

我还尝试使用如下 parameters_body 选项将整个 ARM json 参数作为文件提供给 terrafrom:

I have also tried giving the entire ARM json parameter as a file to terrafrom by using parameters_body option like below :

parameters_body = "${file("${path.module}/temp/params.json")}"

但是,我在执行 terrafrom 脚本时收到以下错误:

however, I am getting the followig error when executing the terrafrom script:

请求内容无效,无法反序列化:'错误转换价值"https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#"输入'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'.路径 'properties.parameters.$schema',第 1 行,位置 2952.'.

The request content was invalid and could not be deserialized: 'Error converting value "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 2952.'.

下面是 params.json 文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "apiVersion": {
            "value": "2017-06-01"
        },
        "sku": {
            "value": "Standard"
        "location": {
            "value": "westus"
        },
        "notificationSettings": {
            "value": {
                "notifyGlobalAdmins": "Enabled",
                "notifyDcAdmins": "Enabled",
                "additionalRecipients": []
            }
        },
        "subnetName": {
            "value": "xxxx"
        },
        "vnetName": {
            "value": "xxxx"
        },
        "vnetAddressPrefixes": {
            "value": [
                "10.0.1.0/24"
            ]
        },
        "subnetAddressPrefix": {
            "value": "10.0.1.0/24"
        },
        "nsgName": {
            "value": "xxxxx"
        }
    }
}

推荐答案

有一种方法可以将任意数据结构从 Terraform 传递到 ARM.

There is a way to pass arbitrary data structures from Terraform to ARM.

有两种方法可以将数据传递到 azure_template_deployment 提供程序中的 ARM 模板

There are two ways to pass data to the ARM template within the azure_template_deployment provider

  • 使用 parameters 块,它仅限于字符串参数
  • 使用 parameters_body 块,它几乎是任意的 JSON.
  • use the parameters block, which is limited to string parameters only
  • use the parameters_body block, which is pretty much arbitrary JSON.

我发现使用参数块的最简单方法是使用我需要的结构创建一个局部变量,然后对其调用 jsonencode.我还喜欢将 ARM 模板保存在一个单独的文件中,并通过 file() 调用将其拉入,从而降低 terraform 的复杂性.

I find the easiest way to use the parameters block is to create a local variable with the structure I require, then call jsonencode on it. I also like to keep the ARM template in a separate file and pull it in via a file() call, reducing the complexity of the terraform.

locals {
  location = "string"
  members = [
    "array",
    "of",
    "members"
  ]
  enabled = true
  tags = {
    "key" = "value",
    "simple" = "store"
  }

  # this is the format required by ARM templates
  parameters_body = {
    location = {
      value = "${local.location}"
    },
    properties = {
      value = {
        users = {
          members = "${local.members}"
        }
        boolparameter = "${local.enabled}"
      }
    }
    tags = {
      value = "${module.global.tags}"
    }
  }
}

resource "azurerm_template_deployment" "sample" {
  name = "sample"
  resource_group_name = "rg"
  deployment_mode = "Incremental"
  template_body = "${file("${path.module}/arm/sample_arm.json")}"
  parameter_body = "${jsonencode(local.parameters_body)}"
}

我发现的唯一警告是 bool 参数作为字符串传递,因此在 ARM 参数部分将它们声明为字符串,然后使用 ARM 函数转换为 bool

The only caveat I've found is that the bool parameters pass as a string, so declare them as a string in the ARM parameters section, then use a ARM function to convert to bool

"parameters: {
  "boolParameter": {
     "type": "string"
  }
},
"variables": {
  "boolVariable": "[bool(parameters('boolParameter'))]"
},
"resources": [
  ...
  "boolArm": "[variables('boolVariable')]",
  ...
]

这篇关于Terraform - 将类型对象作为参数传递给 Azure 模板部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆