Quick deployment of ESXi VMs using Terraform

Hello everyone, my name is Ivan and I am a systems administrator (OPS) who is an alcoholic.

I would like to share how I deploy virtual machines on ESXi without vCenter using Terraform.

Quite often, I need to deploy/recreate virtual machines to test various applications. Due to laziness, I thought about automating the process. My search led me to a wonderful product from the company hashicorp, terraform.

I think many know what Terraform is, and for those who don't, it’s an application for managing any cloud, infrastructure, or service using the IasC concept (Infrastructure as Code).

For my virtualization environment, I use ESXi. It's quite simple, convenient, and reliable.
I foresee the question.

Why Terraform if vCenter Server can be used?

Of course, but. First, it’s an additional license; second, this product is very resource-intensive and simply won't fit on my home server; and third, it allows me to enhance my skills.

The server platform is Intel NUC:

CPU: 2 CPUs x Intel(R) Core(TM) i3-4010U CPU @ 1.70GHz
RAM: 8Gb
HDD: 500Gb
ESXi version: ESXi-6.5.0-4564106-standard (VMware, Inc.)

So, let's go through everything step by step.

First, let’s set up ESXi, specifically opening the VNC port in the firewall settings.

By default, the file is write-protected. We perform the following actions:

chmod 644 /etc/vmware/firewall/service.xml
chmod +t /etc/vmware/firewall/service.xml
vi /etc/vmware/firewall/service.xml

At the end of the file, we add the following block:

packer-vnc
  
    inbound
    tcp
    dst
    
      5900
      6000
    
  
  true
  true

We exit, save, change the permissions back, and restart the service:

chmod 444 /etc/vmware/firewall/service.xml
esxcli network firewall refresh

This is valid until the host is rebooted. After that, this action will have to be repeated.

Next, I will perform all work in a virtual machine on this same server.

Specifications:

OS: Centos 7 x86_64 minimal
RAM: 1GB
HDD: 20GB
Selinux: disable
firewalld: disable

Next, we will need packer, which is also a product of HashiCorp.

It is needed for automatic building of the "golden" image, which we will use in the future.

yum install unzip git -y
curl -O https://releases.hashicorp.com/packer/1.5.5/packer_1.5.5_linux_amd64.zip
unzip packer_1.5.5_linux_amd64.zip -d /usr/bin && rm -rf packer_1.5.5_linux_amd64.zip
packer version
Packer v1.5.5

At this step packer version An error may occur because there might be a package with the same name in RedHat-based systems.

which -a packer
/usr/sbin/packer

To resolve this, you can create a symlink or use the absolute path. /usr/bin/packer.

Now we will need ovftool. download link. Download it, place it on the server, and install it:

chmod +x VMware-ovftool-4.4.0-15722219-lin.x86_64.bundle
./VMware-ovftool-4.4.0-15722219-lin.x86_64.bundle
Extracting VMware Installer...done.
You must accept the VMware OVF Tool component for Linux End User License Agreement to continue.  Press Enter to proceed.
VMWARE END USER LICENSE AGREEMENT
Do you agree? [yes/no]:yes
The product is ready to be installed.  Press Enter to begin installation or Ctrl-C to cancel. 
Installing VMware OVF Tool component for Linux 4.4.0
    Configuring...
[######################################################################] 100%
Installation was successful.

Let's move on.

I have prepared everything needed on GitHub.

git clone https://github.com/letnab/create-and-deploy-esxi.git && cd create-and-deploy-esxi

In the folder iso You need to place the operating system distribution. In my case, it’s CentOS 7.

You also need to edit the file centos-7-base.json:

variables: specify your connection details
iso_urls: provide the current one
iso_checksum: the checksum of your image 

After all the changes, we start the build:

/usr/bin/packer build centos-7-base.json

If everything is configured and specified correctly, you will see an automated operating system installation process. This process takes me 7-8 minutes.

After successful completion, the folder output-packer-centos7-x86_64 will contain the OVA file.

Installing Terraform:

curl -O https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip
unzip terraform_0.12.24_linux_amd64.zip -d /usr/bin/ && rm -rf terraform_0.12.24_linux_amd64.zip
tf version
Terraform v0.12.24

Since Terraform does not have a provider for ESXi, you need to build it.

Install Go:

cd /tmp
curl -O https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.14.2.linux-amd64.tar.gz && rm -rf go1.14.2.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version
go version go1.14.2 linux/amd64

Next, we build the provider:

go get -u -v golang.org/x/crypto/ssh
go get -u -v github.com/hashicorp/terraform
go get -u -v github.com/josenk/terraform-provider-esxi
export GOPATH="$HOME/go"
cd $GOPATH/src/github.com/josenk/terraform-provider-esxi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-w -extldflags "-static"' -o terraform-provider-esxi_`cat version`
cp terraform-provider-esxi_`cat version` /usr/bin

We are on the final stretch. Let’s deploy our image.

Navigate to the folder:

cd /root/create-and-deploy-esxi/centos7

First, we edit the file variables.tf. You need to specify the connection to the ESXi server.

In the file network_config.cfg will contain the network settings of the future virtual machine. Modify it according to your needs and run the one-liner:

sed -i -e '2d' -e '3i "network": "'$(gzip < network_config.cfg| base64 | tr -d 'n')'",' metadata.json

Also, in the file main.tf change the path to the OVA file to yours if it differs.

The moment of truth.

terraform init
Initializing the backend...

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.esxi: version = "~> 1.6"
* provider.template: version = "~> 2.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

terraform plan
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.template_file.Default: Refreshing state...
data.template_file.network_config: Refreshing state...

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

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # esxi_guest.Default will be created
  + resource "esxi_guest" "Default" {
      + boot_disk_size         = (known after apply)
      + disk_store             = "datastore1"
      + guest_name             = "centos7-test"
      + guest_shutdown_timeout = (known after apply)
      + guest_startup_timeout  = (known after apply)
      + guestinfo              = {
          + "metadata"          = "base64text"
          + "metadata.encoding" = "gzip+base64"
          + "userdata"          = "base64text"
          + "userdata.encoding" = "gzip+base64"
        }
      + guestos                = (known after apply)
      + id                     = (known after apply)
      + ip_address             = (known after apply)
      + memsize                = "1024"
      + notes                  = (known after apply)
      + numvcpus               = (known after apply)
      + ovf_properties_timer   = (known after apply)
      + ovf_source             = "\/root\/create-and-deploy-esxi\/output-packer-centos7-x86_64\/packer-centos7-x86_64.ova"
      + power                  = "on"
      + resource_pool_name     = (known after apply)
      + virthwver              = (known after apply)

      + network_interfaces {
          + mac_address     = (known after apply)
          + nic_type        = (known after apply)
          + virtual_network = "VM Network"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

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

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Finish:

terraform apply

If everything is done correctly, a new virtual machine will be deployed from the previously created image in 2-3 minutes.

The possibilities of using all this are limited only by imagination.

I just wanted to share my findings and show the key points when working with these products.

Thank you for your attention!

P.S.: I would appreciate constructive criticism.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster