Quick Deploy vm ESXi ka Terraform

Lumelang bohle, lebitso la ka ke Ivan 'me ke mookameli oa tsamaiso ea joala (OPS).

Ke kopa ho u joetsa hore na ke tsamaisa mechini e fumanehang joang ho ESXi ntle le vCenter ke sebelisa Terraform.

Hangata, o tlameha ho kenya / ho etsa mechini e ncha ho leka sena kapa ts'ebeliso eo. Ka lebaka la botsoa, ​​ke ile ka nahana ka ho iketsetsa mokhoa ona. Patlo ea ka e ile ea nkisa sehlahisoa se setle sa k'hamphani hashicorp, terraform.

Ke nahana hore batho ba bangata ba tseba hore na Terraform ke eng, mme ke mang ea sa tsebeng, ena ke kopo ea ho laola leru leha e le lefe, lisebelisoa tsa motheo kapa tšebeletso ho sebelisa mohopolo oa IasC (Infrastructure as code).

Ke sebelisa ESXi e le tikoloho ea ka ea virtualization. E bonolo, e loketse ebile e ka tšeptjoa.
Ke lebeletse potso.

Ke hobane'ng ha terraform ha u ka sebelisa vCenter Server?

Ehlile u ka khona, empa. Taba ea pele, ena ke laesense e eketsehileng, ea bobeli, sehlahisoa sena se na le lisebelisoa tse ngata ebile ha se lumellane le seva sa ka sa lapeng, 'me ea boraro, bokhoni ba ho ntlafatsa tsebo.

Sethala sa Intel NUC se sebetsa joalo ka seva:

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

'Me ka hona, lintho tsa pele pele.

Hajoale, ha re theheng esxi, e leng, bula boema-kepe ba VNC ho li-firewall.

Ka ho sa feleng, faele e sirelelitsoe ho ngola. Re etsa manipulations tse latelang:

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

Kenya boloko bo latelang qetellong ea faele:

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

Tsoa, boloka. Fetola litokelo hape 'me u qale tšebeletso hape:

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

Ehlile ho fihlela moamoheli a qala hape. Ka mor'a moo, ts'ebetso ena e tla tlameha ho phetoa.

Ho feta moo, ke tla etsa mosebetsi oohle ka mochini o sebetsang ho seva se le seng.

Litlhaloso tsa lihlahisoa:

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

Ka mor'a moo, re hloka thoto, hape ke sehlahisoa sa HashiCorp.

Hoa hlokahala ho kopanya setšoantšo sa "khauta". eo re tla e sebelisa nakong e tlang.

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

Tseleng mofuta oa sephutheloana phoso e ka 'na ea etsahala, kaha RedHat-based e ka ba le sephutheloana se nang le lebitso le tšoanang.

which -a packer
/usr/sbin/packer

Bakeng sa tharollo, o ka etsa symlink, kapa sebelisa tsela e feletseng /usr/bin/packer.

Hona joale re hloka ovftool kopanya li-link. Khoasolla, kenya seva ebe u kenya:

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.

Re tsoela pele.

Ka gith ke lokiselitse tsohle tseo u li hlokang.

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

Ho foldara kholo o hloka ho beha kit ea ho aba ea sistimi e sebetsang. Tabeng ea ka, sena ke centos 7.

U boetse u hloka ho fetola faele. centos-7-base.json:

variables: указать свои данные для подключения
iso_urls: указать актуальный
iso_checksum: чексумма вашего образа 

Ka mor'a liphetoho tsohle, tsamaisa kopano:

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

Haeba ntho e 'ngoe le e' ngoe e hlophisitsoe 'me e hlalositsoe ka nepo, joale u tla bona setšoantšo sa ho kenya mochine o sebetsang. Ts'ebetso ena e nka metsotso e 7-8.

Kamora ho phethoa ka katleho foldareng tlhahiso-packer-centos7-x86_64 faele ea ova e tla fumaneha.

Kenya 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

Kaha Terraform ha e na mofani oa ESXi, o hloka ho e haha.

Re tsamaea:

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

Ka mor'a moo, re bokella mofani oa thepa:

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

Re moleng oa ho qetela. Ha re e'o hlahisa setšoantšo sa rona.

Ha re ee ho sephutheli:

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

Pele ho tsohle, fetola faele mefuta-futa.tf. U hloka ho hlakisa khokahano ho seva ea ESXi.

Ka faele network_config.cfg e na le li-setting tsa marang-rang tsa mochini o tlang oa virtual. Fetola litlhoko tsa hau 'me u tsamaise line-line:

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

Hantle, ka faele ka sehloohong.tf fetola tsela ea faele ea ova hore e be ea hau, haeba e fapane.

Nako ya nnete.

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.

Qetella:

terraform apply

Haeba ntho e 'ngoe le e' ngoe e etsoa ka nepo, ka mor'a metsotso e 2-3 mochine o mocha o tla sebelisoa ho tloha setšoantšong se entsoeng pele.

Ts'ebeliso ea tsena tsohle e lekantsoe feela ke monahano oa hau.

Ke ne ke batla feela ho arolelana mekhoa e metle le ho bontša lintlha tsa sehlooho ha ke sebetsa le lihlahisoa tsena.

Ke leboha tlhokomelo!

PS: Ke tla thabela ho khalemeloa ka mokhoa o hahang.

Source: www.habr.com

Eketsa ka tlhaloso