从Terraform公共注册表模块覆盖默认值

时间:2019-11-29 03:50:16

标签: terraform terraform-provider-azure

使用下面的Terraform代码。 main.tf 文件如下

data "azurerm_resource_group" "tf-rg-external" {
  name = var.rg_name
}

data "azurerm_virtual_network" "tf-vn" {
  name                = var.vnet_name
  resource_group_name = var.rg_name
}

# Reference existing subnet
data "azurerm_subnet" "tf-sn" {
  name                 = var.subnet_name
  virtual_network_name = data.azurerm_virtual_network.tf-vn.name
  resource_group_name  = var.rg_name
}

module "sql_vm" {
  source                        = "Azure/compute/azurerm"
  location                      = data.azurerm_resource_group.tf-rg-external.location
  vnet_subnet_id                = data.azurerm_virtual_network.tf-vn.subnets
  resource_group_name           = data.azurerm_resource_group.tf-rg-external.name
  admin_password                = var.admin_password
  admin_username                = var.admin_username
  boot_diagnostics              = var.boot_diagnostics
  boot_diagnostics_sa_type      = var.boot_diagnostics_sa_type
  # data_disk                     = var.data_disk
  # data_disk_size_gb             = var.data_disk_size_gb
  # data_sa_type                  = var.data_sa_type
  delete_os_disk_on_termination = var.delete_os_disk_on_termination
  enable_accelerated_networking = var.enable_accelerated_networking
  # flag is_windows_image is required only when you use a custom image to spin up a VM
  # is_windows_image
  # flag vm_os_id is required only when you are using custom image
  # you need to provide id of your custom image
  # vm_os_id
  nb_instances                 = var.nb_instances
  #nb_public_ip                 = var.nb_public_ip
  public_ip_address_allocation = var.public_ip_address_allocation
  storage_account_type         = var.storage_account_type
  vm_hostname                  = var.vm_hostname
  vm_os_offer                  = var.vm_os_offer
  vm_os_publisher              = var.vm_os_publisher
  # vm_os_simple is to be used is you do not wish to specify offer, publisher and sku
  # vm_os_simple                  = UbuntuServer, WindowsServer, RHEL, openSUSE-Leap, CentOS, Debian, CoreOS and SLES
  vm_os_sku     = var.vm_os_sku
  vm_os_version = var.vm_os_version
  vm_size       = var.vm_size
}

我的 variables.tf 文件如下:

variable "public_ip_address_allocation" {
  type    = string
  default = "Dynamic"
}
variable "storage_account_type" {
  type    = string
  default = "Standard_LRS"
}
variable "vm_hostname" {
  type    = string
  default = "testvm"
}
variable "vm_os_offer" {
  type    = string
  default = "WindowsServer"
}
variable "vm_os_publisher" {
  type    = string
  default = "MicrosoftWindowsServer"
}
variable "vm_os_sku" {
  type    = string
  default = "2012-R2-Datacenter"
}
variable "vm_os_version" {
  type    = string
  default = "latest"
}
variable "vm_size" {
  type    = string
  default = "Standard_B1ms"
}
variable "admin_password" {
  type = string
  default = "Symphony12#$%"
}
variable "admin_username" {
  type = string
  default = "devopsadmin"
}
variable "boot_diagnostics" {
  type = bool
  default = "true"
}
variable "boot_diagnostics_sa_type" {
  type = string
  default = "Standard_LRS"
}
# variable "data_disk" {
#   type = bool
#   default ="false"
# }
# variable "data_disk_size_gb" {
#   type = number
#   default = 0
# }
# variable "data_sa_type" {
#   type = string
# }
variable "delete_os_disk_on_termination" {
  type = bool
  default = true
}
variable "enable_accelerated_networking" {
  type = bool
  default = false
}
variable "nb_instances" {
  type = number
  default = 2
}
# variable "nb_public_ip" {
#   type = bool
#   default = "1"
# }
# variable "location" {
#   type = string
# }
# variable "vnet_subnet_id" {
#   type = string
# }
variable "rg_name" {
  type = string
  default = "nxt-grp-prd-manage-rgp-au-se"
}
variable "subnet_name" {
  type = string
  default = "subnet_1"
}
variable "vnet_name" {
  type = string
  default = "virtual_network_1"
}

当我运行地形计划时,它在下面报告错误:

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.azurerm_virtual_network.tf-vn: Refreshing state...
data.azurerm_resource_group.tf-rg-external: Refreshing state...
data.azurerm_subnet.tf-sn: Refreshing state...

------------------------------------------------------------------------

Warning: "public_ip_address_allocation": [DEPRECATED] this property has been deprecated in favor of `allocation_method` to better match the api

  on .terraform/modules/sql_vm/Azure-terraform-azurerm-compute-fb014dd/main.tf line 248, in resource "azurerm_public_ip" "vm":
 248: resource "azurerm_public_ip" "vm" {



Warning: "public_ip_address_allocation": [DEPRECATED] this property has been deprecated in favor of `allocation_method` to better match the api

  on .terraform/modules/sql_vm/Azure-terraform-azurerm-compute-fb014dd/main.tf line 248, in resource "azurerm_public_ip" "vm":
 248: resource "azurerm_public_ip" "vm" {



Error: domain_name_label must contain only lowercase alphanumeric characters, numbers and hyphens. It must start with a letter and end only with a number or letter

  on .terraform/modules/sql_vm/Azure-terraform-azurerm-compute-fb014dd/main.tf line 248, in resource "azurerm_public_ip" "vm":
 248: resource "azurerm_public_ip" "vm" {

我希望能够覆盖默认模块中的内容,如下所示:

domain_name_label = "false"

不幸的是,当我在主文件中这样做时,出现如下错误:

Error: Unsupported argument

  on main.tf line 48, in module "sql_vm":
  48:   domain_name_label = "false"

An argument named "domain_name_label" is not expected here.

因此,无论如何都没有要覆盖的内容,因为这是一个开箱即用的配置,我很高兴看到它能正常工作。

1 个答案:

答案 0 :(得分:1)

  

我希望能够覆盖默认模块中的内容,如下所示

为此,tf模块应将其声明为变量。您的模块Azure/compute/azurerm没有为domain_name_label定义的变量。

退后一步,看看您的原始问题:查看此模块中的以下代码块:

https://github.com/Azure/terraform-azurerm-compute/blob/master/main.tf#L248

resource "azurerm_public_ip" "vm" {
  count                        = "${var.nb_public_ip}"
  name                         = "${var.vm_hostname}-${count.index}-publicIP"
  location                     = "${var.location}"
  resource_group_name          = "${azurerm_resource_group.vm.name}"
  public_ip_address_allocation = "${var.public_ip_address_allocation}"
  domain_name_label            = "${element(var.public_ip_dns, count.index)}"
  tags                         = "${var.tags}"
}

请在调用此模块时为public_ip_dns设置一个值。可以解决您的问题。

示例: (请注意,public_ip_dns = ["my-new-good-vm"]行):

module "sql_vm" {
  source                        = "Azure/compute/azurerm"
  location                      = data.azurerm_resource_group.tf-rg-external.location
  vnet_subnet_id                = data.azurerm_virtual_network.tf-vn.subnets
  resource_group_name           = data.azurerm_resource_group.tf-rg-external.name
  admin_password                = var.admin_password
  admin_username                = var.admin_username
  public_ip_dns                 = ["my-new-good-vm"]
  boot_diagnostics              = var.boot_diagnostics
  boot_diagnostics_sa_type      = var.boot_diagnostics_sa_type
  # data_disk                     = var.data_disk
  # data_disk_size_gb             = var.data_disk_size_gb
  # data_sa_type                  = var.data_sa_type
  delete_os_disk_on_termination = var.delete_os_disk_on_termination
  enable_accelerated_networking = var.enable_accelerated_networking
  # flag is_windows_image is required only when you use a custom image to spin up a VM
  # is_windows_image
  # flag vm_os_id is required only when you are using custom image
  # you need to provide id of your custom image
  # vm_os_id
  nb_instances                 = var.nb_instances
  #nb_public_ip                 = var.nb_public_ip
  public_ip_address_allocation = var.public_ip_address_allocation
  storage_account_type         = var.storage_account_type
  vm_hostname                  = var.vm_hostname
  vm_os_offer                  = var.vm_os_offer
  vm_os_publisher              = var.vm_os_publisher
  # vm_os_simple is to be used is you do not wish to specify offer, publisher and sku
  # vm_os_simple                  = UbuntuServer, WindowsServer, RHEL, openSUSE-Leap, CentOS, Debian, CoreOS and SLES
  vm_os_sku     = var.vm_os_sku
  vm_os_version = var.vm_os_version
  vm_size       = var.vm_size
}