Terraform - 将类型对象作为参数传递给 Azure 模板部署

Ben*_*min 6 terraform terraform-provider-azure

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

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

    "notificationSettings": {
        "value": {
            "notifyGlobalAdmins": "Enabled",
            "notifyDcAdmins": "Enabled",
            "additionalRecipients": []
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

"apiVersion" = "2017-06-01"
Run Code Online (Sandbox Code Playgroud)

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

"notificationSettings" = [{
                "notifyGlobalAdmins" = "Enabled"
            "notifyDcAdmins" ="Enabled"
            "additionalRecipients" = []
}]
Run Code Online (Sandbox Code Playgroud)

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

属性“参数”的值不合适:元素“notificationSettings”:需要字符串。

如何将 Object 的参数类型传递给template body

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

parameters_body = "${file("${path.module}/temp/params.json")}"
Run Code Online (Sandbox Code Playgroud)

但是,在执行 terrafrom 脚本时出现以下错误:

请求内容无效且无法反序列化:“将值“ https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json# ”转换为“Microsoft.WindowsAzure.ResourceStack ”时出错。 Frontdoor.Data.Definitions.DeploymentParameterDefinition'。路径 'properties.parameters.$schema',第 1 行,位置 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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Lei*_*eif 9

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

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

  • 使用parameters块,它仅限于字符串参数
  • 使用parameters_body块,它几乎是任意的 JSON。

我发现使用参数块的最简单方法是使用我需要的结构创建一个局部变量,然后调用jsonencode它。我还喜欢将 ARM 模板保存在一个单独的文件中,并通过file()调用将其拉入,从而降低 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)}"
}
Run Code Online (Sandbox Code Playgroud)

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

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