将计划块添加为条件 if 语句
Add the plan block as a conditional if statement
我正在使用 Terraform 从 Azure 市场部署 Linux 个虚拟机。
我在 linux 模块中设置了一个计划块,并在我的 main.tf 中声明了变量。
当 运行 terraform plan/apply 它工作并构建 VM
我想知道如何修改我的模块和 main.tf 以将计划块添加为条件 if 语句。
我想使用此模块构建非 azure 市场和 azure 市场虚拟机。
我不希望单独的模块来执行此操作。
module "vm-ansiblecontroller" {
resource_group_name = module.rg-ansiblecontroller.resource_group_name
location = local.location
linux_machine_name = "linux-test1"
tags = var.tags
nic_id = [module.vm-ansiblecontroller.nic_id]
subnet_id = module.subnet-networkcore.subnet_id
virtual_machine_size = "Standard_D2"
admin_username = "jpadmin"
admin_ssh_public_key = file("~/.ssh/id_rsa.pub")
source_image_publisher = "procomputers"
source_image_offer = "rocky-lnx-8-latest"
source_image_sku = "rocky-linux-8-latest"
source_image_version = "8.5.20220222"
plan_name = "rocky-linux-8-latest"
plan_product = "rocky-lnx-8-latest"
plan_publisher = "procomputers"
operating_system_disk_cache = "ReadWrite"
operating_system_disk_type = "Standard_LRS"
ip_configuration_name = "internal"
private_ip_address_allocation = "Dynamic"
public_ip_allocation_method = "Static"
public_ip_sku = "Standard"
depends_on = [
module.rg-networkcore,
module.vnet-networkcore,
module.subnet-networkcore
]
}
module "vm-jpdev" {
resource_group_name = module.rg-jpdev-vm.resource_group_name
location = local.location
linux_machine_name = "linux-test2"
tags = var.tags
nic_id = [module.vm-jpdev.nic_id]
subnet_id = module.subnet-networkcore.subnet_id
virtual_machine_size = "Standard_D2"
admin_username = "jpadmin"
admin_ssh_public_key = file("~/.ssh/id_rsa.pub")
source_image_publisher = "Canonical"
source_image_offer = "UbuntuServer"
source_image_sku = "16.04-LTS"
source_image_version = "latest"
operating_system_disk_cache = "ReadWrite"
operating_system_disk_type = "Standard_LRS"
ip_configuration_name = "internal"
private_ip_address_allocation = "Dynamic"
public_ip_allocation_method = "Static"
public_ip_sku = "Standard"
}
modules/virtualmachine/linux/variables.tf
# VM Name
variable "linux_machine_name" {
description = "Linux Virtual Machine Name - If left blank generated from metadata module"
type = string
default = ""
}
variable "resource_group_name" {
description = "Resource group name"
type = string
}
variable "location" {
description = "Azure region"
type = string
}
variable "tags" {
description = "tags to be applied to resources"
type = map(string)
}
# VM Size
variable "virtual_machine_size" {
description = "Instance size to be provisioned"
type = string
}
variable "admin_username" {
description = "names to be applied to resources"
type = string
}
variable "admin_ssh_public_key" {
description = "(Linux) Public SSH Key - Generated if left blank"
type = string
default = ""
sensitive = true
}
# Operating System
variable "source_image_publisher" {
description = "Operating System Publisher"
type = string
}
variable "source_image_offer" {
description = "Operating System Name"
type = string
}
variable "source_image_sku" {
description = "Operating System SKU"
type = string
}
variable "source_image_version" {
description = "Operating System Version"
type = string
default = "latest"
}
# Plan Block Variables
variable "plan_name" {
description = "Plan Name"
type = string
}
variable "plan_product" {
description = "Plan Product"
type = string
}
variable "plan_publisher" {
description = "Plan Publisher"
type = string
}
# Operating System Disk
variable "operating_system_disk_cache" {
description = "Type of caching to use on the OS disk - Options: None, ReadOnly or ReadWrite"
type = string
default = "ReadWrite"
}
variable "operating_system_disk_type" {
description = "Type of storage account to use with the OS disk - Options: Standard_LRS, StandardSSD_LRS or Premium_LRS"
type = string
default = "StandardSSD_LRS"
}
variable "ip_configuration_name" {
description = "ip configuration name"
type = string
default = ""
}
# Networking
variable "nic_id" {
type = list(string)
description = "ID of the nic"
}
variable "subnet_id" {
type = string
description = "ID of the subnet"
}
variable "private_ip_address_allocation" {
type = string
description = "Private ip allocation method"
}
variable "public_ip_allocation_method" {
type = string
description = "Public ip allocation method"
}
variable "public_ip_sku" {
description = "SKU to be used with this public IP - Basic or Standard"
type = string
default = "Standard"
}
modules/virtualmachine/variables.tf
# Interface id
output "nic_id" {
description = "ids of the vm nics provisoned."
value = azurerm_network_interface.nic-linux.id
}
你可以使用 dynamic blocks:
resource "azurerm_linux_virtual_machine" "vm-linux" {
#....
dynamic "plan" {
for_each = var.plan_product == "rocky-lnx-8-latest" ? [1] : []
content {
name = var.plan_name
product = var.plan_product
publisher = var.plan_publisher
}
}
# ...
}
我正在使用 Terraform 从 Azure 市场部署 Linux 个虚拟机。
我在 linux 模块中设置了一个计划块,并在我的 main.tf 中声明了变量。
当 运行 terraform plan/apply 它工作并构建 VM
我想知道如何修改我的模块和 main.tf 以将计划块添加为条件 if 语句。
我想使用此模块构建非 azure 市场和 azure 市场虚拟机。
我不希望单独的模块来执行此操作。
module "vm-ansiblecontroller" {
resource_group_name = module.rg-ansiblecontroller.resource_group_name
location = local.location
linux_machine_name = "linux-test1"
tags = var.tags
nic_id = [module.vm-ansiblecontroller.nic_id]
subnet_id = module.subnet-networkcore.subnet_id
virtual_machine_size = "Standard_D2"
admin_username = "jpadmin"
admin_ssh_public_key = file("~/.ssh/id_rsa.pub")
source_image_publisher = "procomputers"
source_image_offer = "rocky-lnx-8-latest"
source_image_sku = "rocky-linux-8-latest"
source_image_version = "8.5.20220222"
plan_name = "rocky-linux-8-latest"
plan_product = "rocky-lnx-8-latest"
plan_publisher = "procomputers"
operating_system_disk_cache = "ReadWrite"
operating_system_disk_type = "Standard_LRS"
ip_configuration_name = "internal"
private_ip_address_allocation = "Dynamic"
public_ip_allocation_method = "Static"
public_ip_sku = "Standard"
depends_on = [
module.rg-networkcore,
module.vnet-networkcore,
module.subnet-networkcore
]
}
module "vm-jpdev" {
resource_group_name = module.rg-jpdev-vm.resource_group_name
location = local.location
linux_machine_name = "linux-test2"
tags = var.tags
nic_id = [module.vm-jpdev.nic_id]
subnet_id = module.subnet-networkcore.subnet_id
virtual_machine_size = "Standard_D2"
admin_username = "jpadmin"
admin_ssh_public_key = file("~/.ssh/id_rsa.pub")
source_image_publisher = "Canonical"
source_image_offer = "UbuntuServer"
source_image_sku = "16.04-LTS"
source_image_version = "latest"
operating_system_disk_cache = "ReadWrite"
operating_system_disk_type = "Standard_LRS"
ip_configuration_name = "internal"
private_ip_address_allocation = "Dynamic"
public_ip_allocation_method = "Static"
public_ip_sku = "Standard"
}
modules/virtualmachine/linux/variables.tf
# VM Name
variable "linux_machine_name" {
description = "Linux Virtual Machine Name - If left blank generated from metadata module"
type = string
default = ""
}
variable "resource_group_name" {
description = "Resource group name"
type = string
}
variable "location" {
description = "Azure region"
type = string
}
variable "tags" {
description = "tags to be applied to resources"
type = map(string)
}
# VM Size
variable "virtual_machine_size" {
description = "Instance size to be provisioned"
type = string
}
variable "admin_username" {
description = "names to be applied to resources"
type = string
}
variable "admin_ssh_public_key" {
description = "(Linux) Public SSH Key - Generated if left blank"
type = string
default = ""
sensitive = true
}
# Operating System
variable "source_image_publisher" {
description = "Operating System Publisher"
type = string
}
variable "source_image_offer" {
description = "Operating System Name"
type = string
}
variable "source_image_sku" {
description = "Operating System SKU"
type = string
}
variable "source_image_version" {
description = "Operating System Version"
type = string
default = "latest"
}
# Plan Block Variables
variable "plan_name" {
description = "Plan Name"
type = string
}
variable "plan_product" {
description = "Plan Product"
type = string
}
variable "plan_publisher" {
description = "Plan Publisher"
type = string
}
# Operating System Disk
variable "operating_system_disk_cache" {
description = "Type of caching to use on the OS disk - Options: None, ReadOnly or ReadWrite"
type = string
default = "ReadWrite"
}
variable "operating_system_disk_type" {
description = "Type of storage account to use with the OS disk - Options: Standard_LRS, StandardSSD_LRS or Premium_LRS"
type = string
default = "StandardSSD_LRS"
}
variable "ip_configuration_name" {
description = "ip configuration name"
type = string
default = ""
}
# Networking
variable "nic_id" {
type = list(string)
description = "ID of the nic"
}
variable "subnet_id" {
type = string
description = "ID of the subnet"
}
variable "private_ip_address_allocation" {
type = string
description = "Private ip allocation method"
}
variable "public_ip_allocation_method" {
type = string
description = "Public ip allocation method"
}
variable "public_ip_sku" {
description = "SKU to be used with this public IP - Basic or Standard"
type = string
default = "Standard"
}
modules/virtualmachine/variables.tf
# Interface id
output "nic_id" {
description = "ids of the vm nics provisoned."
value = azurerm_network_interface.nic-linux.id
}
你可以使用 dynamic blocks:
resource "azurerm_linux_virtual_machine" "vm-linux" {
#....
dynamic "plan" {
for_each = var.plan_product == "rocky-lnx-8-latest" ? [1] : []
content {
name = var.plan_name
product = var.plan_product
publisher = var.plan_publisher
}
}
# ...
}