Provision a VM using Packer and Vagrant
May 31, 2020 Leave a comment
About 8 years back I worked for a company serving customers of the Payment Card Industry. They had a dire need of Infrastructure as Code(IaC) to build a Windows Active-Passive Cluster with Connect:Direct and engineers spent day and night to set it up manually. The ruckus created by that is still etched in my mind.
Now when I tried a simple recipe it worked like a charm. It isn’t very complicated as it is a simple test.
I started with this repo.
C:\Packer\ubuntu\ubuntu>packer build -only=vmware-iso -var='ssh_fullname=mirage' -var='ssh_password=mirage' -var-file=ubuntu1804.json ubuntu.json vmware-iso: output will be in this color. Warnings for build 'vmware-iso': * A checksum type of 'none' was specified. Since ISO files are so big, a checksum is highly recommended. * Your vmx data contains the following variable(s), which Packer normally sets when it generates its own default vmx template. This may cause your build to fail or behave unpredictably: numvcpus, memsize ==> vmware-iso: Retrieving ISO ==> vmware-iso: Trying /Volumes/Storage/software/ubuntu/ubuntu-18.04.4-server-amd64.iso ==> vmware-iso: Trying /Volumes/Storage/software/ubuntu/ubuntu-18.04.4-server-amd64.iso?checksum=a5b0ea5918f850124f3d72ef4b85bda82f0fcd02ec721be19c1a6952791c8ee8 ==> vmware-iso: /Volumes/Storage/software/ubuntu/ubuntu-18.04.4-server-amd64.iso?checksum=a5b0ea5918f850124f3d72ef4b85bda82f0fcd02ec721be19c1a6952791c8ee8 => C:/Packer/ubuntu/ubuntu/Volumes/Storage/software/ubuntu/ubuntu-18.04.4-server-amd64.iso ==> vmware-iso: Creating floppy disk... vmware-iso: Copying files flatly from floppy_files vmware-iso: Copying file: http/preseed.cfg vmware-iso: Done copying files from floppy_files vmware-iso: Collecting paths from floppy_dirs vmware-iso: Resulting paths from floppy_dirs : [] vmware-iso: Done copying paths from floppy_dirs
Add box to Vagrant
C:\Packer\ubuntu\ubuntu\box\vmware>vagrant box add ubuntu1804-0.1.0.box --name vmwarepackeransible ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'vmwarepackeransible' (v0) for provider: box: Unpacking necessary files from: file://C:/Packer/ubuntu/ubuntu/box/vmware/ubuntu1804-0.1.0.box box: ==> box: Successfully added box 'vmwarepackeransible' (v0) for 'vmware_desktop'!
Initialize
C:\Packer\ubuntu\ubuntu\box\vmware>vagrant init vmwarepackeransible A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. C:\Packer\ubuntu\ubuntu\box\vmware>vagrant up Bringing machine 'default' up with 'vmware_desktop' provider... ==> default: Cloning VMware VM: 'vmwarepackeransible'. This can take some time... ==> default: Verifying vmnet devices are healthy... ==> default: Preparing network adapters... ==> default: Starting the VMware VM... ==> default: Waiting for the VM to receive an address... ==> default: Forwarding ports... default: -- 22 => 2222 ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Configuring network adapters within the VM... ==> default: Waiting for HGFS to become available... ==> default: Enabling and configuring shared folders... default: -- C:/Packer/ubuntu/ubuntu/box/vmware: /vagrant
Shell provisioner in Vagrantfile
config.vm.provision "shell", inline: <<-SHELL add-apt-repository ppa:openjdk-r/ppa -y apt-get update echo "\n----- Installing Java 8 ------\n" apt-get -y install openjdk-8-jdk update-alternatives --config java
SSH into vagrant and check
SHELLvagrant@vagrant:~$ java -version openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
There are other scenarious that are complicated but a simple test like this works as expected.