【问题标题】:Terraform - join/Concatanate variables with stringTerraform - 使用字符串连接/连接变量
【发布时间】:2023-04-06 21:45:01
【问题描述】:

变量.tf

variable "env_name" {
  default = "some_client"
}

variable "azure_instance_names" {
  default = [
    "AD01",
    "AD01",
  ]
}

我',尝试为azure_instance_names 变量(在本例中为 2)中指定的多个实例创建公共 IP,我在命名此资源时遇到问题,我想通过加入 env_name 和 @987654324 来创建名称@ 多变的。必须是一个单词,以-分隔,所以name应该在env_name-azure_instance_names中例如:

期望的输出

name=some_client-AD01 some_client-AD02

实际输出:

name=some_client AD01 some_client AD02

main.tf

resource "azurerm_public_ip" "datasourceip" {    
  count               = "${length(var.azure_instance_names)}"
  name                = "${join("-", list(format("%s %s", var.env_name, element(var.azure_instance_names, count.index))))}"
  location            = "${azurerm_resource_group.res_group.location}"
  resource_group_name = "${azurerm_resource_group.res_group.name}"
  allocation_method   = "Static"
}

在 terraform apply 我得到:

+ azurerm_public_ip.datasourceip[1]
      id:                      <computed>
      allocation_method:       "Static"
      fqdn:                    <computed>
      idle_timeout_in_minutes: "4"
      ip_address:              <computed>
      ip_version:              "IPv4"
      location:                "westeurope"
      name:                    "some_client AD01"
      resource_group_name:     "myrg1"
      sku:                     "Basic"
      tags.%:                  <computed>

由于 Azure 不接受超过一个单词 i' 的资源名称,因此尝试将“-”加入 var.env_name, var.azure_instance_names,因此资源名称应为 some_client-AD01

虽然我指定了连接函数 i',但仍然出现同样的错误:

azurerm_public_ip.datasourceip.1:创建/更新公共 IP“some_client AD01”(资源组“myrg1”)时出错:network.PublicIPAddressesClient#CreateOrUpdate:发送请求失败:StatusCode=400 -- 原始错误:Code="InvalidResourceName" Message="资源名称 some_client LBA-P-EU2B-AD01 无效。名称最长可包含 80 个字符。必须以单词字符开头,并且必须以单词字符或以 '' 结尾。名称可能包含单词字符或 '.'、'-'、''。"详情=[]

【问题讨论】:

    标签: terraform


    【解决方案1】:

    一路只用插值:

    name = "${var.env_name}-${var.azure_instance_names[count.index]}"
    

    我还发现${var.foo[i]}${element(var.foo, i)} 更容易阅读。

    【讨论】:

    • 请告诉我当节点名称不是列表时如何使用计数索引。像 kafkanode1 和 th3 一样,1 是计数器,也被定义为变量。 name = "${var.env_name}-${var.azure_instance_names[var.count.index+1]}"
    【解决方案2】:

    愚蠢的我!!,解决方案很简单:

    我只需要以这种方式连接var.env_name${format("%s",element(var.azure_instance_names, count.index)),而不是将两个变量都放入连接函数中:

    name =  "${var.env_name}-${format("%s",element(var.azure_instance_names, count.index))}"
    
    
    
    azurerm_public_ip.datasourceip[1]: Creating...
      allocation_method:       "" => "Static"
      fqdn:                    "" => "<computed>"
      idle_timeout_in_minutes: "" => "4"
      ip_address:              "" => "<computed>"
      ip_version:              "" => "IPv4"
      location:                "" => "westeurope"
      name:                    "" => "some_client-AD01"
      resource_group_name:     "" => "myrg1"
      sku:                     "" => "Basic"
      tags.%:                  "" => "<computed>"
    azurerm_public_ip.datasourceip[0]: Creating...
      allocation_method:       "" => "Static"
      fqdn:                    "" => "<computed>"
      idle_timeout_in_minutes: "" => "4"
      ip_address:              "" => "<computed>"
      ip_version:              "" => "IPv4"
      location:                "" => "westeurope"
      name:                    "" => "some_client-AD02"
      resource_group_name:     "" => "myrg1"
      sku:                     "" => "Basic"
      tags.%:                  "" => "<computed>"
    azurerm_public_ip.datasourceip[1]: Creation complete after 5s (ID: /subscriptions/2a325f88-e3dd-48c2-bf85-...PAddresses/some_client-AD01)
    azurerm_public_ip.datasourceip[0]: Creation complete after 5s (ID: /subscriptions/2a325f88-e3dd-48c2-bf85-...PAddresses/some_client-AD02)
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 2018-06-23
      • 1970-01-01
      相关资源
      最近更新 更多