找不到变量'azurerm_sql_server.sql_server.name'的资源'azurerm_sql_server.sql_server'

时间:2019-08-05 04:09:51

标签: sql-server azure-devops terraform terraform-provider-azure

我用var.sql_server_enable用启用/禁用选项创建了sql,但是我对输出有问题。如果var.sql_server_enable0,则azurerm_sql_server.sql_server.name将为null,并且出现以下错误。

resource "azurerm_sql_server" "sql_server" {
  count                        = "${var.sql_server_enable ? 1 : 0}"
  name                         = "${var.sql_server_name}"
  resource_group_name          = "${var.sql_server_resource_group_name}"
  location                     = "${var.sql_server_location}"
  version                      = "${var.sql_server_version}"
  administrator_login          = "${var.sql_server_admin_user}"
  administrator_login_password = "${var.sql_server_admin_pass}"
}
output "sql_server_name_output" {
  value = "${var.sql_server_enable == "1" ? azurerm_sql_server.sql_server.name : var.null }"
}
Error: Error running plan: 1 error occurred:
        * module.hello.module.azure_sql_server_hello_staging.output.sql_server_name_output: Resource 'azurerm_sql_server.sql_server' not found for variable 'azurerm_sql_server.sql_server.name'

2 个答案:

答案 0 :(得分:0)

您可以输出 value = "${var.sql_server_enable ? azurerm_sql_server.sql_server[0].name : var.null }"。当您进行terraform应用时,当您输入变量“ sql_server_enable”的值时,可以键入“ 1”代表真,输入“ 0”代表假。

这适用于Terraform v0.12.6 + provider.azurerm v1.32.1,如果需要,您可以upgrade it

variable "sql_server_enable" {
  type = bool 

}

variable "null" {
  default = "this is empty"
}


resource "azurerm_sql_server" "sql_server" {
  count                        = "${var.sql_server_enable  ? 1 : 0}"
  name                         = "mysqlserver123qaz"
  resource_group_name          = "${azurerm_resource_group.test.name}"
  location                     = "${azurerm_resource_group.test.location}"
  version                      = "12.0"
  administrator_login          = "mradministrator"
  administrator_login_password = "thisIsDog11"

}

output "sql_server_name_output" {
 value = "${var.sql_server_enable ? azurerm_sql_server.sql_server[0].name : var.null }"
 }

结果1 enter image description here 结果2 enter image description here 希望对您有帮助。

答案 1 :(得分:0)

感谢您的回答,但是在其他模块上使用了sql_server_name_output变量。它需要string值。然后,我使用var.null而不是null。我听从您的建议后,在下面又遇到另一个错误。您还有其他建议吗?

Error: Inconsistent conditional result types

  on ../modules/azure_sql_server/output.tf line 9, in output "sql_server_name_output":
   9:   value = "${var.sql_server_enable ? azurerm_sql_server.sql_server[*].name : var.null }"
    |----------------
    | azurerm_sql_server.sql_server is tuple with 1 element
    | var.null is "null"
    | var.sql_server_enable is true

下面需要另一个azurerm_sql_server.sql_server的资源。

resource "azurerm_sql_firewall_rule" "sql_firewall_rule" {
  count               = "${var.sql_fw_enable == "1" ? 1 : 0}"
  name                = "${var.sql_fw_rule_name}"
  resource_group_name = "${var.sql_fw_rule_resource_group_name}"
  server_name         = "${azurerm_sql_server.sql_server}"
  start_ip_address    = "${var.sql_fw_rule_start_ip}"
  end_ip_address      = "${var.sql_fw_rule_end_ip}"
}