Commit 072cf1ef authored by zhehang deng's avatar zhehang deng
Browse files

Merge branch 'dev' into 'master'

初始化仓库

See merge request iac-module/cloudiac-vsphere-modules/cloudiac-vsphere-vm!1
No related merge requests found
Showing with 408 additions and 0 deletions
+408 -0
<!-- BEGIN_TF_DOCS -->
## Requirements
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.2.4 |
| <a name="requirement_vsphere"></a> [vsphere](#requirement\_vsphere) | >= 1.0.0 |
## Providers
| Name | Version |
|------|---------|
| <a name="provider_vsphere"></a> [vsphere](#provider\_vsphere) | >= 1.0.0 |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [vsphere_virtual_machine.vm](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/resources/virtual_machine) | resource |
| [vsphere_compute_cluster.cluster](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/data-sources/compute_cluster) | data source |
| [vsphere_datacenter.dc](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/data-sources/datacenter) | data source |
| [vsphere_datastore.datastore](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/data-sources/datastore) | data source |
| [vsphere_network.network](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/data-sources/network) | data source |
| [vsphere_virtual_machine.template](https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs/data-sources/virtual_machine) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_cluster"></a> [cluster](#input\_cluster) | VC 集群名称 | `string` | n/a | yes |
| <a name="input_cpu"></a> [cpu](#input\_cpu) | CPU | `string` | n/a | yes |
| <a name="input_data_disk_label"></a> [data\_disk\_label](#input\_data\_disk\_label) | Storage data disk labels | `list(any)` | `[]` | no |
| <a name="input_data_disk_scsi_controller"></a> [data\_disk\_scsi\_controller](#input\_data\_disk\_scsi\_controller) | scsi\_controller number for the data disk, should be equal to number of defined data disk | `list(any)` | `[]` | no |
| <a name="input_data_disk_size_gb"></a> [data\_disk\_size\_gb](#input\_data\_disk\_size\_gb) | List of storage data disk size | `list(any)` | `[]` | no |
| <a name="input_datastore"></a> [datastore](#input\_datastore) | VC 存储名称 | `string` | n/a | yes |
| <a name="input_dns_server"></a> [dns\_server](#input\_dns\_server) | n/a | `list(string)` | n/a | yes |
| <a name="input_domain"></a> [domain](#input\_domain) | default VM domain for linux guest customization. | `string` | `""` | no |
| <a name="input_eagerly_scrub"></a> [eagerly\_scrub](#input\_eagerly\_scrub) | iif set to true ,the disk space is zeroed out on VM creation.This will delay the creation of the disk or virtual machine.Cannot be set to true when thin\_provisioned is true. | `list(any)` | `null` | no |
| <a name="input_gateway"></a> [gateway](#input\_gateway) | 网关 | `string` | n/a | yes |
| <a name="input_hostname"></a> [hostname](#input\_hostname) | 系统主机名称 | `string` | n/a | yes |
| <a name="input_hw_clock_utc"></a> [hw\_clock\_utc](#input\_hw\_clock\_utc) | Tells the operating system that the hardware clock is set to UTC. | `bool` | `true` | no |
| <a name="input_idc_code"></a> [idc\_code](#input\_idc\_code) | VC 数据中心 | `string` | n/a | yes |
| <a name="input_ip"></a> [ip](#input\_ip) | n/a | `string` | n/a | yes |
| <a name="input_is_windows"></a> [is\_windows](#input\_is\_windows) | create windows vm?(default: false) | `bool` | `false` | no |
| <a name="input_memory"></a> [memory](#input\_memory) | 内存 | `string` | n/a | yes |
| <a name="input_netmask"></a> [netmask](#input\_netmask) | 掩码 | `string` | n/a | yes |
| <a name="input_network"></a> [network](#input\_network) | VC 网络名称 | `string` | n/a | yes |
| <a name="input_scsi_controller"></a> [scsi\_controller](#input\_scsi\_controller) | scsi\_controller number for the main OS disk | `number` | `0` | no |
| <a name="input_template"></a> [template](#input\_template) | 模版名称 | `string` | n/a | yes |
| <a name="input_thin_provisioned"></a> [thin\_provisioned](#input\_thin\_provisioned) | If true,this disk is thin provisioned,with space for the file being allocated on an as-needed basis. | `list(any)` | `null` | no |
| <a name="input_vm_annotation"></a> [vm\_annotation](#input\_vm\_annotation) | 主机备注 | `string` | n/a | yes |
| <a name="input_vm_folder_path"></a> [vm\_folder\_path](#input\_vm\_folder\_path) | the folder to put this virtual machine in | `string` | `""` | no |
| <a name="input_vm_name"></a> [vm\_name](#input\_vm\_name) | 虚拟机名称 | `string` | n/a | yes |
| <a name="input_wait_for_guest_ip_timeout"></a> [wait\_for\_guest\_ip\_timeout](#input\_wait\_for\_guest\_ip\_timeout) | wait for guest ip timeout | `number` | `0` | no |
| <a name="input_wait_for_guest_net_timeout"></a> [wait\_for\_guest\_net\_timeout](#input\_wait\_for\_guest\_net\_timeout) | wait for guest net timeout | `number` | `0` | no |
| <a name="input_win_options"></a> [win\_options](#input\_win\_options) | n/a | `map(any)` | `{}` | no |
## Outputs
| Name | Description |
|------|-------------|
| <a name="output_cpu"></a> [cpu](#output\_cpu) | n/a |
| <a name="output_default_ip_address"></a> [default\_ip\_address](#output\_default\_ip\_address) | n/a |
| <a name="output_description"></a> [description](#output\_description) | n/a |
| <a name="output_guest_id"></a> [guest\_id](#output\_guest\_id) | n/a |
| <a name="output_guest_ip_address"></a> [guest\_ip\_address](#output\_guest\_ip\_address) | n/a |
| <a name="output_hostname"></a> [hostname](#output\_hostname) | n/a |
| <a name="output_id"></a> [id](#output\_id) | n/a |
| <a name="output_is_windows"></a> [is\_windows](#output\_is\_windows) | n/a |
| <a name="output_memory"></a> [memory](#output\_memory) | n/a |
<!-- END_TF_DOCS -->
\ No newline at end of file
main.tf 0 → 100644
data "vsphere_datacenter" "dc" {
name = var.idc_code
}
data "vsphere_datastore" "datastore" {
name = var.datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_compute_cluster" "cluster" {
name = var.cluster
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = var.network
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_virtual_machine" "template" {
name = var.template
datacenter_id = data.vsphere_datacenter.dc.id
}
locals {
template_disk_count = length(data.vsphere_virtual_machine.template.disks)
}
resource "vsphere_virtual_machine" "vm" {
folder = var.vm_folder_path
name = var.vm_name
annotation = var.vm_annotation
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
wait_for_guest_net_timeout = var.wait_for_guest_net_timeout
wait_for_guest_ip_timeout = var.wait_for_guest_ip_timeout
num_cpus = var.cpu
num_cores_per_socket = var.cpu
memory = var.memory
cpu_hot_add_enabled = true
memory_hot_add_enabled = true
cpu_hot_remove_enabled = true
firmware = data.vsphere_virtual_machine.template.firmware
scsi_type = data.vsphere_virtual_machine.template.scsi_type
guest_id = data.vsphere_virtual_machine.template.guest_id
network_interface {
network_id = data.vsphere_network.network.id
}
// 系统盘
dynamic "disk" {
for_each = data.vsphere_virtual_machine.template.disks
iterator = template_disks
content {
label = format("disk%d", template_disks.key)
size = data.vsphere_virtual_machine.template.disks[template_disks.key].size
unit_number = var.scsi_controller != null ? var.scsi_controller * 15 + template_disks.key : template_disks.key
}
}
// 数据盘
dynamic "disk" {
for_each = var.data_disk_size_gb
iterator = terraform_disks
content {
label = length(var.data_disk_label) > 0 ? var.data_disk_label[terraform_disks.key] : format("disk%d", (terraform_disks.key + local.template_disk_count))
size = var.data_disk_size_gb[terraform_disks.key]
unit_number = length(var.data_disk_scsi_controller) > 0 ? (
var.data_disk_scsi_controller[terraform_disks.key] * 15 + terraform_disks.key + (
var.scsi_controller == var.data_disk_scsi_controller[terraform_disks.key] ? local.template_disk_count : 0
)
) : terraform_disks.key + local.template_disk_count
thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[terraform_disks.key] : null
eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[terraform_disks.key] : null
}
}
clone {
timeout = 240
template_uuid = data.vsphere_virtual_machine.template.id
customize {
dynamic "linux_options" {
for_each = var.is_windows ? [] : [1]
content {
host_name = length(var.hostname) > 0 ? var.hostname : var.vm_name
domain = var.domain
hw_clock_utc = var.hw_clock_utc
}
}
dynamic "windows_options" {
for_each = var.is_windows ? [1] : []
content {
computer_name = length(var.hostname) > 0 ? var.hostname : var.vm_name
admin_password = var.win_options.sys_password
auto_logon = true
auto_logon_count = 1
}
}
network_interface {
ipv4_address = var.ip
ipv4_netmask = var.netmask
}
ipv4_gateway = var.gateway
dns_server_list = var.dns_server
}
}
}
ouputs.tf 0 → 100644
output "id" {
value = vsphere_virtual_machine.vm.id
}
output "default_ip_address" {
value = vsphere_virtual_machine.vm.default_ip_address
}
output "guest_ip_address" {
value = vsphere_virtual_machine.vm.guest_ip_addresses
}
output "cpu" {
value = vsphere_virtual_machine.vm.num_cpus
}
output "memory" {
value = vsphere_virtual_machine.vm.memory
}
output "hostname" {
value = var.is_windows ? vsphere_virtual_machine.vm.clone[0].customize[0].windows_options[0].computer_name : vsphere_virtual_machine.vm.clone[0].customize[0].linux_options[0].host_name
}
output "description" {
value = vsphere_virtual_machine.vm.annotation
}
output "is_windows" {
value = var.is_windows
}
output "guest_id" {
value = vsphere_virtual_machine.vm.guest_id
}
variable "idc_code" {
description = "VC 数据中心"
type = string
}
variable "datastore" {
description = "VC 存储名称"
type = string
}
variable "cluster" {
description = "VC 集群名称"
type = string
}
variable "network" {
description = "VC 网络名称"
type = string
}
variable "template" {
description = "模版名称"
type = string
}
variable "vm_name" {
description = "虚拟机名称"
type = string
}
variable "vm_annotation" {
description = "主机备注"
type = string
}
variable "cpu" {
description = "CPU"
type = string
}
variable "memory" {
description = "内存"
type = string
}
variable "ip" {
description = ""
type = string
}
variable "hostname" {
description = "系统主机名称"
type = string
}
variable "netmask" {
description = "掩码"
type = string
}
variable "gateway" {
description = "网关"
type = string
}
variable "dns_server" {
description = ""
type = list(string)
}
variable "domain" {
description = "default VM domain for linux guest customization."
type = string
default = ""
}
variable "scsi_controller" {
description = "scsi_controller number for the main OS disk"
type = number
default = 0
}
variable "data_disk_scsi_controller" {
description = "scsi_controller number for the data disk, should be equal to number of defined data disk"
type = list(any)
default = []
}
variable "data_disk_label" {
description = "Storage data disk labels"
type = list(any)
default = []
}
variable "data_disk_size_gb" {
description = "List of storage data disk size"
type = list(any)
default = []
}
variable "thin_provisioned" {
description = "If true,this disk is thin provisioned,with space for the file being allocated on an as-needed basis."
type = list(any)
default = null
}
variable "eagerly_scrub" {
description = "iif set to true ,the disk space is zeroed out on VM creation.This will delay the creation of the disk or virtual machine.Cannot be set to true when thin_provisioned is true."
type = list(any)
default = null
}
#Linux Customization Variables
variable "hw_clock_utc" {
description = "Tells the operating system that the hardware clock is set to UTC."
type = bool
default = true
}
// windows 机器使用
variable "is_windows" {
description = "create windows vm?(default: false)"
type = bool
default = false
}
variable "win_options" {
type = map(any)
default = {}
}
variable "vm_folder_path" {
description = "the folder to put this virtual machine in"
type = string
default = ""
}
# variable "sys_password" {
# description = "sys_password"
# type = string
# default = null
# }
# variable "new_sys_password" {
# description = "sys_password"
# type = string
# default = ""
# }
# variable "app_code" {
# type = string
# default = ""
# }
# variable "os_management_level" {
# description = ""
# type = string
# default = ""
# }
# 超时参数
variable "wait_for_guest_ip_timeout" {
description = "wait for guest ip timeout"
type = number
default = 0
}
variable "wait_for_guest_net_timeout" {
description = "wait for guest net timeout"
type = number
default = 0
}
terraform {
required_version = ">= 1.2.4"
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = ">= 1.0.0"
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment