Using Packer to build Redhat/CentOS virtual machine on VMware vSphere

In this article, we will step by step introduce how to use ISO to build Redhat Enterprise Linux 7/CentOS 7 VM in VMware vSphere environment. Packer It is very useful for building basic images in the cloud and locally. Packer is an open source tool developed by HashiCorp development. HashiCorp has well-known tools such as Vagrant, Consul and Terraform. Today, cloning and deployment are the easiest way to build VMS. In some cases, the required version of the OS template may not exist, and you may need to build a VM in ad hoc. This is a very convenient place for Packer. If you want to create templates automatically, see this article.

Let's start coding the new infrastructure.

Download the following components:

 

On your laptop / desktop,

1. Create a new directory and copy all downloaded components. RHEL/CentOS ISO should remain a 0n VMware vSphere datastore.

Unixarea – Packer directory snapshot

 

2. Open notepad and paste the following contents. Edit all required values based on your infrastructure. Save this file as centos7 in the same directory_ build. json.

{
  "builders": [
    {
      "type": "vsphere-iso",

      "vcenter_server":      "192.168.2.212",
      "username":            "administrator@vsphere.local",
      "password":            "test@123",
      "insecure_connection": "true",
      "vm_name": "RHEL-Packer1",
      "notes": "Build via Packer",
      "datacenter": "STACK-BLR",
      "cluster": "UA-CLS",
      "host": "192.168.2.211",
      "datastore": "DATASTORE-BLR",
      "network": "VM Network",
      "resource_pool": "UA-ResPool",

      "guest_os_type": "centos7_64Guest",

      "ssh_username": "root",
      "ssh_password": "server",

      "CPUs":             1,
      "RAM":              1024,
      "RAM_reserve_all": false,

      "convert_to_template": false,

      "disk_controller_type":  "pvscsi",
      "disk_size":        25000,
      "disk_thin_provisioned": true,

      "network_card": "vmxnet3",

      "iso_paths": [
        "[DATASTORE-BLR] ISO/centos7_64.iso"
      ],
      "iso_checksum": "5b61d5b378502e9cba8ba26b6696c92a",
      "iso_checksum_type": "md5",
      "floppy_files": [
        "{{template_dir}}/ks.cfg"
      ],
      "boot_command": " inst.text inst.ks=hd:fd0:/ks.cfg "
          }
  ]
}

Except boot_command,networ_card,disk_ controller_ In addition to type and configuration type, you may need to update the values of almost all fields.

 

3. You need to prepare a traditional kickstart file to define package selection and other configurations. Download this for RHEL 7/ CentOS 7 kickstart file . Please modify and update the kickstart file at any time according to our needs.

  • Root credentials - root / server
  • User – admin/admin123

 

4. This is a snapshot of the contents of the directory.

Packer – RHEL – vSphere VM build

 

5. Open the command prompt - start - > CMD - > Enter. Navigate to the directory we created for this VM build.

Packer VM build – RHEL – CentOS

 

6. Use the following command to trigger the packer build job. At this stage, the VM job is waiting for the VM to start using IP.

C:\Users\lingeswaran.rangasam\Desktop\Redhat-Packer-Test>packer.exe build CentOS7_build.json
vsphere-iso output will be in this color.

==> vsphere-iso: Creating VM...
==> vsphere-iso: Customizing hardware...
==> vsphere-iso: Mount ISO images...
==> vsphere-iso: Creating floppy disk...
    vsphere-iso: Copying files flatly from floppy_files
    vsphere-iso: Copying file: C:\Users\lingeswaran.rangasam\Desktop\Redhat-Packer-Test/ks.cfg
    vsphere-iso: Done copying files from floppy_files
    vsphere-iso: Collecting paths from floppy_dirs
    vsphere-iso: Resulting paths from floppy_dirs : []
    vsphere-iso: Done copying paths from floppy_dirs
==> vsphere-iso: Uploading created floppy image
==> vsphere-iso: Adding generated Floppy...
==> vsphere-iso: Set boot order temporary...
==> vsphere-iso: Power on VM...
==> vsphere-iso: Waiting 10s for boot...
==> vsphere-iso: Typing boot command...
==> vsphere-iso: Waiting for IP...

 

7. If you navigate to the vCenter console, you can see that the virtual machine may have been created and powered on.

VMware VM – create – power on

 

8. Start the VM console and observe the automatic build.

Virtual machine startup – Packer – VMware vSphere

 

9. VM is using kickstart file to start automatic installation.

Automatic installation – Packer Build – RHEL

 

10. The VM uses LVM to automatically configure the required file system layout.

Auto configure LVM – Packer – RHEL

 

11. This is the result of the packer build job after successfully completing the VM build.

C:\Users\lingeswaran.rangasam\Desktop\Redhat-Packer-Test>packer.exe build CentOS7_build.json
vsphere-iso output will be in this color.

==> vsphere-iso: Creating VM...
==> vsphere-iso: Customizing hardware...
==> vsphere-iso: Mount ISO images...
==> vsphere-iso: Creating floppy disk...
    vsphere-iso: Copying files flatly from floppy_files
    vsphere-iso: Copying file: C:\Users\lingeswaran.rangasam\Desktop\Redhat-Packer-Test/ks.cfg
    vsphere-iso: Done copying files from floppy_files
    vsphere-iso: Collecting paths from floppy_dirs
    vsphere-iso: Resulting paths from floppy_dirs : []
    vsphere-iso: Done copying paths from floppy_dirs
==> vsphere-iso: Uploading created floppy image
==> vsphere-iso: Adding generated Floppy...
==> vsphere-iso: Set boot order temporary...
==> vsphere-iso: Power on VM...
==> vsphere-iso: Waiting 10s for boot...
==> vsphere-iso: Typing boot command...
==> vsphere-iso: Waiting for IP...
==> vsphere-iso: IP address: 192.168.2.7
==> vsphere-iso: Using ssh communicator to connect: 192.168.2.7
==> vsphere-iso: Waiting for SSH to become available...
==> vsphere-iso: Connected to SSH!
==> vsphere-iso: Shut down VM...
==> vsphere-iso: Deleting Floppy drives...
==> vsphere-iso: Deleting Floppy image...
==> vsphere-iso: Eject CD-ROM drives...
==> vsphere-iso: Clear boot order...
Build 'vsphere-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> vsphere-iso: RHEL-Packer1

C:\Users\lingeswaran.rangasam\Desktop\Redhat-Packer-Test>

 

12. Log in to the virtual machine instance and view the status.

login as: root
root@192.168.2.7's password:
[root@UnixArena ~]#
[root@UnixArena ~]#
[root@UnixArena ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol06   17G  1.3G   16G   8% /
devtmpfs                       484M     0  484M   0% /dev
tmpfs                          496M     0  496M   0% /dev/shm
tmpfs                          496M  7.1M  489M   2% /run
tmpfs                          496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/VolGroup-LogVol02 1014M   33M  982M   4% /home
/dev/mapper/VolGroup-LogVol01 1014M   33M  982M   4% /tmp
/dev/mapper/VolGroup-LogVol03  2.0G  101M  1.9G   5% /var
/dev/mapper/VolGroup-LogVol04 1014M   36M  979M   4% /var/log
/dev/mapper/VolGroup-LogVol05  509M   26M  483M   6% /var/log/audit
/dev/sda1                      509M  129M  381M  26% /boot
tmpfs                          100M     0  100M   0% /run/user/0
[root@UnixArena ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   1   7   0 wz--n- 23.91g    0
[root@UnixArena ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup lvm2 a--  23.91g    0
[root@UnixArena ~]# lvs
  LV       VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sy                                                                                        nc Convert
  LogVol01 VolGroup -wi-ao----   1.00g                                                                                                                                  
  LogVol02 VolGroup -wi-ao----   1.00g                                                                                                                                  
  LogVol03 VolGroup -wi-ao----   2.00g                                                                                                                                  
  LogVol04 VolGroup -wi-ao----   1.00g                                                                                                                                  
  LogVol05 VolGroup -wi-ao---- 512.00m                                                                                                                                  
  LogVol06 VolGroup -wi-ao----  16.44g                                                                                                                                  
  lv_swap  VolGroup -wi-ao----  <1.97g                                                                                                                                  
[root@UnixArena ~]# which lsof
/usr/sbin/lsof
[root@UnixArena ~]#

 

We have successfully created a VM and built RHEL7/CentOS7 using ISO and the legacy kickstart file. In the next article, we'll see how to make VM templates using a wrapper.

Keywords: Linux Operation & Maintenance CentOS

Added by taurus5_6 on Tue, 21 Dec 2021 02:13:41 +0200