Terraform 使用带有偏移量的地形中的计数

Terraform 使用带有偏移量的地形中的计数,terraform,Terraform,我想使用Terraform创建一些资源,并使用var.tf文件中定义的偏移量将count.index包含到这些资源中,如: vm-app-04 vm-app-05 vm-app-06 vm-app-04-nic-01 vm-app-05-nic-01 vm-app-06-nic-01 每当我尝试添加${var.count\u offset}时,都会得到错误:无效字符或错误:无效表达式。我有没有办法创造这种行为 resource "azurerm_network_interface" "nic"

我想使用Terraform创建一些资源,并使用var.tf文件中定义的偏移量将count.index包含到这些资源中,如:

vm-app-04

vm-app-05

vm-app-06

vm-app-04-nic-01

vm-app-05-nic-01

vm-app-06-nic-01

每当我尝试添加${var.count\u offset}时,都会得到错误:无效字符或错误:无效表达式。我有没有办法创造这种行为

resource "azurerm_network_interface" "nic" {
  count                     = "${var.nb_instances}"
  name                      = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}-nic-01"
  location                  = "${module.global_variables.location}"
  resource_group_name       = "${module.global_variables.resource_group_name}"

  ip_configuration {
    name                          = "ipconfig01"
    subnet_id                     = "${var.subnet_id}"
    private_ip_address_allocation = "dynamic"
  }
  tags = {
    environment = "${var.environment}}"
  }
}


resource "azurerm_dns_a_record" "add_A_record" {
  count               = "${var.nb_instances}"
  name                = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}"
  zone_name           = "${module.global_variables.dns_zone}"
  resource_group_name = "${module.global_variables.resource_group_name}"
  ttl                 = 300
  records             = ["${element(azurerm_network_interface.nic.*.private_ip_address, count.index + 1 + ${var.count_offset})}"]
  tags = {
    environment = "${var.environment}"
  }
}
这是我得到的输出:

Error: Invalid character

  on ../../../modules/computation/cluster-module/vm.tf line 72, in resource "azurerm_network_interface" "nic":
  72:   name                      = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}-nic-01"

This character is not used within the language.


Error: Invalid expression

  on ../../../modules/computation/cluster-module/vm.tf line 72, in resource "azurerm_network_interface" "nic":
  72:   name                      = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}-nic-01"

Expected the start of an expression, but found an invalid expression token.


Error: Invalid character

  on ../../../modules/computation/cluster-module/vm.tf line 128, in resource "azurerm_dns_a_record" "add_A_record":
 128:   name                = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}"

This character is not used within the language.


Error: Missing argument separator

  on ../../../modules/computation/cluster-module/vm.tf line 128, in resource "azurerm_dns_a_record" "add_A_record":
 128:   name                = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}"

A comma is required to separate each function argument from the next.


Error: Invalid character

  on ../../../modules/computation/cluster-module/vm.tf line 132, in resource "azurerm_dns_a_record" "add_A_record":
 132:   records             = ["${element(azurerm_network_interface.nic.*.private_ip_address, count.index + 1 + ${var.count_offset})}"]

This character is not used within the language.


Error: Missing argument separator

  on ../../../modules/computation/cluster-module/vm.tf line 132, in resource "azurerm_dns_a_record" "add_A_record":
 132:   records             = ["${element(azurerm_network_interface.nic.*.private_ip_address, count.index + 1 + ${var.count_offset})}"]

A comma is required to separate each function argument from the next.

您不应该嵌套${},因为这只是用来告诉Terraform插入它的内部,这样您就可以在var.count\u偏移量周围放置${}

更改后,您的代码应该是:

resource "azurerm_network_interface" "nic" {
  count                     = "${var.nb_instances}"
  name                      = "${var.vm_hostname}-${format("%02d", count.index + 1 + var.count_offset)}-nic-01"
  location                  = "${module.global_variables.location}"
  resource_group_name       = "${module.global_variables.resource_group_name}"

  ip_configuration {
    name                          = "ipconfig01"
    subnet_id                     = "${var.subnet_id}"
    private_ip_address_allocation = "dynamic"
  }
  tags = {
    environment = "${var.environment}}"
  }
}


resource "azurerm_dns_a_record" "add_A_record" {
  count               = "${var.nb_instances}"
  name                = "${var.vm_hostname}-${format("%02d", count.index + 1 + ${var.count_offset})}"
  zone_name           = "${module.global_variables.dns_zone}"
  resource_group_name = "${module.global_variables.resource_group_name}"
  ttl                 = 300
  records             = ["${element(azurerm_network_interface.nic.*.private_ip_address, count.index + 1 + var.count_offset)}"]
  tags = {
    environment = "${var.environment}"
  }
}

您是否也可以包含错误代码?现在您只是显示没有偏移量的代码。对不起,我现在修复了我的帖子。您不需要var.count\u偏移量周围的${}。它解决了问题,非常感谢!