Quick Deploy vm ESXi with Terraform

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

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

Quite often, you have to deploy / recreate virtual machines to test this or that application. Because of laziness, I thought about automating the process. My searches led me to a wonderful hashicorp product , terraform .

I think many people know what Terraform is, and who does not know, this is an application for managing any cloud, infrastructure or service using the concept of IasC ( Infrastructure as code ).

As a virtualization environment, I use ESXi. Pretty simple, convenient and reliable.
I foresee a question.
Why terraform if you can use vCenter Server?
You can of course, but. Firstly, this is an additional license, secondly, this product is very resource-intensive and just does not fit on my home server, and thirdly, the ability to upgrade skills.

The server is the Intel NUC platform:

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.)

And so, first things first.

For now, let's configure esxi, namely, open the VNC port in the firewall settings.

By default, the file is write protected. We carry out the following manipulations:

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

add the following block to the end of the file:

<service id="1000">
  <id>packer-vnc</id>
  <rule id="0000">
    <direction>inbound</direction>
    <protocol>tcp</protocol>
    <porttype>dst</porttype>
    <port>
      <begin>5900</begin>
      <end>6000</end>
    </port>
  </rule>
  <enabled>true</enabled>
  <required>true</required>
</service>

Exit, save. We change the rights back and restart the service:

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

In this case, VNC is needed to connect to the virtual machine and specify the path to the kickstart file.

Actually before rebooting the host. After that, this manipulation will have to be repeated.

Further I will carry out all the work in a virtual machine on the same server.

Characteristics:

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

Next we need packer , also a product of HashiCorp.

He is needed for the automatic assembly 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

An error may occur at the packer version step , since a package with the same name may be in RedHat-based.

which -a packer
/usr/sbin/packer

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

Now we need the ovftool download link . Download, put on the server and install:

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.

Moving on.

On a gita, I prepared everything I needed.

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

In the iso folder you need to put the distribution of the operating system. In my case it is centos 7.

It is also necessary to edit the centos-7-base.json file :

variables:     
iso_urls:  
iso_checksum:    

After all the changes, run the assembly:

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

If everything is configured and specified correctly, then you will see a picture of the automatic installation of the operating system.
packer-centos7-x86_64 output will be in this color.

==> packer-centos7-x86_64: Retrieving ISO
    packer-centos7-x86_64: Using file in-place: file:///root/create-and-deploy-esxi/iso/CentOS-7-x86_64-Minimal-1908.iso
==> packer-centos7-x86_64: Remote cache was verified skipping remote upload...
==> packer-centos7-x86_64: Creating required virtual machine disks
==> packer-centos7-x86_64: Building and writing VMX file
==> packer-centos7-x86_64: Starting HTTP server on port 8494
==> packer-centos7-x86_64: Registering remote VM...
==> packer-centos7-x86_64: Starting virtual machine...
    packer-centos7-x86_64: The VM will be run headless, without a GUI. If you want to
    packer-centos7-x86_64: view the screen of the VM, connect via VNC with the password "" to
    packer-centos7-x86_64: vnc://10.10.10.10:5900
==> packer-centos7-x86_64: Waiting 7s for boot...
==> packer-centos7-x86_64: Connecting to VM via VNC (10.10.10.10:5900)
==> packer-centos7-x86_64: Typing the boot command over VNC...
==> packer-centos7-x86_64: Waiting for SSH to become available...



This process takes 7-8 minutes.

After successful completion, the ova file will be located in the folder output-packer-centos7-x86_64 .

Install 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
terraform version
Terraform v0.12.24

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

Put 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 collect 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 at the finish line. Let's go roll out our image.

Go to the folder:

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

First of all, edit the variables.tf file . You must specify a connection to the ESXi server.

The network_config.cfg file contains the network settings of the future virtual machine. We change to our needs and run a one-liner:

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

Well, in the main.tf file , change the path to the ova file to your own, if different.

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.

The finish:

terraform apply

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

Options for using all of this are limited only by imagination.

I just wanted to share best practices and show the main points when working with these products.

Thank you for attention!

PS: I will be glad to constructive criticism.

All Articles