terraform 编排ECS

provider 与 resource

在 Terraform 的配置文件中,比较常见的配置类型有 provider 和 resource。

provider 在 Terraform 中负责管理资源的生命周期:创建、读取、更新、删除。比如访问 AWS 中的资源需要使用 AWS 的 provider,访问 Azure 中的资源需要使用 Azure 的 provider。

resource 是基础设施的一个组件。它可能是一些低级组件,例如物理服务器、虚拟机或容器。也可以是更高级别的组件,如电子邮件提供程序、DNS记录或数据库提供程序。

指定 provider

provider "azurerm" {
}

Terraform 执行命令时会读取工作目录中所有的 .tf, .tfvars 文件,所以我们不必把所有的东西都写在单个文件中去,应按职责分列在不同的文件中,例如:

文件名说明
provider.tfprovider配置
terraform.tfvars配置provider要用的变量
varable.tf通用变量
resource.tf资源定义
data.tf包文件定义
output.tf输出

 举个例子:

varable.tf

#===========================#
# VMware vCenter connection #
#===========================#

variable "vsphere-user" {
  type        = string
  description = "VMware vSphere user name"
}

variable "vsphere-password" {
  type        = string
  description = "VMware vSphere password"
}

variable "vsphere-vcenter" {
  type        = string
  description = "VMWare vCenter server FQDN / IP"
}

variable "vsphere-unverified-ssl" {
  type        = string
  description = "Is the VMware vCenter using a self signed certificate (true/false)"
}

variable "vsphere-datacenter" {
  type        = string
  description = "VMWare vSphere datacenter"
}

variable "vsphere-cluster" {
  type        = string
  description = "VMWare vSphere cluster"
  default     = ""
}

variable "vsphere-template-folder" {
  type        = string
  description = "Template folder"
  default = "Templates"
}

#================================#
# VMware vSphere virtual machine #
#================================#

variable "vm-count" {
  type        = string
  description = "Number of VM"
  default     =  1
}

variable "vm-name-prefix" {
  type        = string
  description = "Name of VM prefix"
  default     =  "tftest"
}

variable "vm-datastore" {
  type        = string
  description = "Datastore used for the vSphere virtual machines"
}

variable "vm-network" {
  type        = string
  description = "Network used for the vSphere virtual machines"
}

variable "vm-linked-clone" {
  type        = string
  description = "Use linked clone to create the vSphere virtual machine from the template (true/false). If you would like to use the linked clone feature, your template need to have one and only one snapshot"
  default     = "false"
}

variable "vm-cpu" {
  type        = string
  description = "Number of vCPU for the vSphere virtual machines"
  default     = "2"
}

variable "vm-ram" {
  type        = string
  description = "Amount of RAM for the vSphere virtual machines (example: 2048)"
}

variable "vm-disk-size" {
  type        = string
  description = "Amount of Disk for the vSphere virtual machines (example: 80)"
  default     = "80"
}


variable "vm-name" {
  type        = string
  description = "The name of the vSphere virtual machines and the hostname of the machine"
}

variable "vm-guest-id" {
  type        = string
  description = "The ID of virtual machines operating system"
}

variable "vm-template-name" {
  type        = string
  description = "The template to clone to create the VM"
}

variable "vm-domain" {
  type        = string
  description = "Linux virtual machine domain name for the machine. This, along with host_name, make up the FQDN of the virtual machine"
  default     = ""
}

variable "vm-folder" {
  type        = string
  description = "The VM folder"
}

variable "vm-resource-pool" {
  type        = string
  description = "The VM resource pool"
}


variable "vm-annotation" {
  type        = string
  description = "The VM notes"
}

variable "vm-application" {
  type        = string
  description = "The VM Custom Attributes"
}

variable "vm-owner" {
  type        = string
  description = "The VM Custom Attributes"
}

variable "vm-ip" {
  description = "Ip used for the vSpgere virtual machine"
}

variable "vm-netmask" {
  description = "Netmask used for the vSphere virtual machine (example: 24)"
}

variable "vm-gateway" {
  description = "Gateway for the vSphere virtual machine"
}

variable "vm-dns" {
  description = "DNS for the vSphere virtual machine"
}

variables.tf

# ======================== #
# VMware VMs configuration #
# ======================== #

vm-count = "1"   #定义虚拟机数量
vm-name = "ECS-st002"  #定义虚拟机基础名称
vm-template-name = "template 18.04 40"  #定义虚拟机模版名称
vsphere-template-folder = "虚拟机模板"  #定义虚拟机模版所在的文件夹
vm-cpu = 4   #定义虚拟机硬件配置 2 4 8 16
vm-ram = 4096 #定义虚拟机硬件配置 4096 8192  16384 32768
vm-disk-size = 100
vm-guest-id = "ubuntu64Guest" #centos7_64Guest
vm-resource-pool = "test"  #定义虚拟机部署到哪个资源池和文件夹,文件夹需要提前创建
vm-folder = "**"    #定义虚拟机部署到哪个资源池和文件夹,文件夹需要提前创建
vsphere-datacenter = "Datacenter-supOS"  #定义虚拟机目标数据中心和集群
vsphere-cluster = "**"  #定义虚拟机目标数据中心和集群
vm-datastore = "**"    #定于虚拟机使用存储
vm-network = "VP6.9_2102"      #定义虚拟机使用网络
vm-domain = "corp.local"    #定义Linux虚拟机的domain


vm-annotation = "Create by Terraform"   #定义虚拟机备注消息的第一行内容


# ============================ #
# VM Custom Notes              #
# ============================ #
vm-application = "备注(暂时不用)"
vm-owner = "备注(暂时不用)"

# ============================ #
# VMware vSphere configuration #
# ============================ #

# VMware vCenter IP/FQDN
vsphere-vcenter = "**"

# VMware vSphere username used to deploy the infrastructure
vsphere-user = "**"  #定义vCenter连接信息

# VMware vSphere password used to deploy the infrastructure
vsphere-password = "**"     #定义vCenter连接信息

# Skip the verification of the vCenter SSL certificate (true/false)
vsphere-unverified-ssl = "true"    #定义vCenter连接信息   跳过vCenter SSL证书的验证

vm-ip = "**"
vm-netmask = 24
vm-gateway = "***"
vm-dns = "****"

main.tf

#==================== #
# Deploying vSphere VM #
# ==================== #

# Connect to VMware vSphere vCenter
provider "vsphere" {
  user = var.vsphere-user
  password = var.vsphere-password
  vsphere_server = var.vsphere-vcenter

  # If you have a self-signed cert  自签名证书
  allow_unverified_ssl = var.vsphere-unverified-ssl
}

# Define VMware vSphere
data "vsphere_datacenter" "dc" {
  name = var.vsphere-datacenter
}

data "vsphere_datastore" "datastore" {
  name = var.vm-datastore
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_compute_cluster" "cluster" {
  name = var.vsphere-cluster
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_network" "network" {
  name = var.vm-network
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_virtual_machine" "template" {
  name = "/${var.vsphere-datacenter}/vm/${var.vsphere-template-folder}/${var.vm-template-name}"
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_resource_pool" "resource_pool" {
  name = var.vm-resource-pool
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

# =================== #
# get CST Time.       #
# =================== #

locals {
  time = "${formatdate("YYYY-MM-DD hh:mm",timeadd(timestamp(),"8h"))}"
  host_name = "${formatdate("YYYY-MM-DD-hh-mm",timeadd(timestamp(),"8h"))}"
}
#定义Linux自定义配置,这里采用DHCP和简单配置,可以参考文档进行自定义静态IP地址等
# Create VMs
resource "vsphere_virtual_machine" "vm" {
  count = var.vm-count
  name = var.vm-name
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id = data.vsphere_datastore.datastore.id
  folder = var.vm-folder
  annotation = "${var.vm-annotation}\nVM-CreateDate:${local.time}"
  num_cpus = var.vm-cpu
  memory = var.vm-ram
  guest_id = var.vm-guest-id
  network_interface {
    network_id = data.vsphere_network.network.id
  }
  disk {
    label = "${var.vm-name}.vmdk"
    size = var.vm-disk-size
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template.id
    linked_clone = false
    host_name1= var.vm-ip
    customize {
      #定制
      timeout = "20"
      linux_options {
        host_name = "ECS-${local.host_name}"
        domain = var.vm-domain
      }
      network_interface {

        ipv4_address = var.vm-ip
        ipv4_netmask = var.vm-netmask
      }
      ipv4_gateway = var.vm-gateway
      dns_server_list = [
        var.vm-dns]
    }
  }
}
# limit the terraform version
terraform {
  required_version = ">= 0.12.20"
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值