{"id":559,"date":"2023-12-13T21:36:54","date_gmt":"2023-12-13T21:36:54","guid":{"rendered":"http:\/\/virtuality.pt\/?p=559"},"modified":"2023-12-14T08:31:53","modified_gmt":"2023-12-14T08:31:53","slug":"automate-vm-creation-with-vagrant-on-vmware-fusion","status":"publish","type":"post","link":"https:\/\/virtuality.pt\/?p=559","title":{"rendered":"Automate VM creation with Vagrant on VMware Fusion"},"content":{"rendered":"\n<p>When I&#8217;m on the go and need to test, study, or swiftly showcase a solution to a client while traveling, I rely on VMware Fusion on my MacBook.<\/p>\n\n\n\n<p>Recently, I found myself setting up Linux VMs to build a Kubernetes cluster for my testing purposes. As usual, creating multiple VMs can be time-consuming. Seeking a robust yet user-friendly way to automate this process on my laptop, I explored Vagrant by HashiCorp.<\/p>\n\n\n\n<p><strong>Vagrant boxes<\/strong> serve as ready-made environments for development purposes, bundling all the necessary components and configurations required to set up a virtual machine swiftly. Picture them as templates or molds for your virtual machines, streamlining the setup process across platforms like  VMware, VirtualBox, or Hyper-V.<\/p>\n\n\n\n<p>These boxes are a time-saver, eliminating the manual installation and configuration of operating systems and software. With Vagrant, deploying VMs becomes automated. Additionally, there&#8217;s a repository\u2014curated by HashiCorp and the community\u2014that houses a plethora of Vagrant boxes, akin to Docker hub&#8217;s collection of containers. This repository simplifies the search for specific boxes tailored to your infrastructure needs.<\/p>\n\n\n\n<p>Numerous Linux Vagrant boxes are available, contributed by the community. These encompass open-source distributions such as Ubuntu, CentOS, and Fedora, among others. Surprisingly, there are also Windows Vagrant boxes. Each box comes with detailed descriptions and documentation, aiding users in selecting the ideal one for their requirements.<\/p>\n\n\n\n<p>Deciphering the Vagrantfile, written in Ruby, is crucial. It furnishes directives on how to establish and manage individual virtual machine environments. Furthermore, running the &#8216;vagrant init&#8217; command generates the Vagrantfile, kickstarting the configuration process for you.<\/p>\n\n\n\n<p>After diving into the Vagrantfile, I&#8217;ve crafted what I believe is a solution that can streamline the creation of multiple boxes for your needs.<\/p>\n\n\n\n<p>Specifically, I&#8217;ve tailored it for a scenario where one VM operates as the Kubernetes Master while two other VMs function as Workers. This setup aims to cater to your use case efficiently.<\/p>\n\n\n\n<p><strong>But first the basics:<\/strong><\/p>\n\n\n\n<p>I already had homebrew installed on my MAC but assuming you don&#8217;t have it <\/p>\n\n\n\n<p>Just run on your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/bin\/bash -c \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/HEAD\/install.sh)\"<\/code><\/pre>\n\n\n\n<p>for more information about homebrew:<\/p>\n\n\n\n<p><a href=\"https:\/\/brew.sh\/\">https:\/\/brew.sh\/<\/a><\/p>\n\n\n\n<p><strong>And now Vagrant installation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew tap hashicorp\/tap\nbrew install hashicorp\/tap\/hashicorp-vagrant<\/code><\/pre>\n\n\n\n<p>The output should be similar to the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"289\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-70-1024x289.png\" alt=\"\" class=\"wp-image-560\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-70-1024x289.png 1024w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-70-300x85.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-70-768x217.png 768w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-70.png 1356w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now since we are going to use Vagrant with a VMware product we will need to install a plugin for it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install --cask vagrant-vmware-utility<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"121\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-71-1024x121.png\" alt=\"\" class=\"wp-image-561\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-71-1024x121.png 1024w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-71-300x36.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-71-768x91.png 768w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-71.png 1394w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant plugin install vagrant-vmware-desktop<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"75\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-72.png\" alt=\"\" class=\"wp-image-562\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-72.png 808w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-72-300x28.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-72-768x71.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/figure>\n\n\n\n<p>Then you can run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant init <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"77\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-73.png\" alt=\"\" class=\"wp-image-563\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-73.png 668w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-73-300x35.png 300w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/figure>\n\n\n\n<p>It will generate a Vagrantfile from scratch that I didn&#8217;t use because I wanted something customized for my needs. <\/p>\n\n\n\n<p>in the site https:\/\/app.vagrantup.com\/boxes\/search<\/p>\n\n\n\n<p>I looked for an image with Ubuntu server for me to use as BOX for my Linux projects<\/p>\n\n\n\n<p>And I picked this one:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"988\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-74.png\" alt=\"\" class=\"wp-image-564\" style=\"width:576px;height:auto\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-74.png 900w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-74-273x300.png 273w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-74-768x843.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>Note that we will only use the config.vm.box = &#8220;gusztavvargadr\/ubuntu-server-2204-lts&#8221;<\/p>\n\n\n\n<p>As I said created a customized Vagrantfile to create 3 Boxes with a specific setup:<\/p>\n\n\n\n<p>You can copy and paste the information below to a new Vagrantfie and change whatever you need<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- mode: ruby -*-\n# vi: set ft=ruby :\n\nnodes = {\n    'kmaster01'\t=> &#91;1, 235],\n    'knode01'  => &#91;1, 236],\n    'knode02'  => &#91;1, 237],\n}\n\nVagrant.configure(\"2\") do |config|\n    config.vm.box = \"gusztavvargadr\/ubuntu-server-2204-lts\"\n    nodes.each do |prefix, (count, ip_start)|\n        count.times do |i|\n            hostname = \"%s\" % &#91;prefix, (i+1)]\n            config.vm.define \"#{hostname}\" do |box|\n                box.vm.hostname = \"#{hostname}.book\"\n                box.vm.network :private_network, ip: \"192.168.0.#{ip_start+i}\", :netmask => \"255.255.255.0\"\n                #box.vm.network :private_network, ip: \"10.10.0.#{ip_start+i}\", :netmask => \"255.255.0.0\" \n                # VMware Fusion\n                box.vm.provider :vmware_fusion do |v|\n        v.ssh_info_public = true\n        v.gui = true\n        v.linked_clone = false\n        v.vmx&#91;\"ethernet0.virtualdev\"] = \"vmxnet3\"\n        v.vmx&#91;\"ethernet1.virtualdev\"] = \"vmxnet3\"\n   \tv.vmx&#91;'mks.enable3d'] = true                        # enable 3D acceleration\n        v.vmx&#91;'vmci0.present'] = true                       # enable Virtual Machine Configuration Interface\n        v.vmx&#91;'hpet0.present'] = true                       # enable High Precision Event Timer\n        v.vmx&#91;'powerType.powerOff'] = 'soft'                # regular power settings \n        v.vmx&#91;'powerType.powerOn'] = 'soft'                 # regular power settings\n        v.vmx&#91;'powerType.suspend'] = 'soft'                 # regular power settings\n        v.vmx&#91;'powerType.reset'] = 'soft'                   # regular power settings\n        v.vmx&#91;'guestOS'] = 'ubuntu-64'                      # select 'ubuntu-64' as 'guostOS' in VMware\n    \tv.vmx&#91;'numvcpus'] = '2'                             # two processor cores reserved for vm\n    \tv.vmx&#91;'cpuid.coresPerSocket'] = '2'                 # one socket reserved for vm &#91;1(socket)*2(CPU)=2(CPU)]\n    \tv.vmx&#91;'vcpu.hotadd'] = true                         # enable VMware Hot Add for CPU              \n    \tv.vmx&#91;'memsize'] = '2048'                           # reserve 2GB RAM for vm\n    \tv.vmx&#91;'mem.hotadd'] = true                          # enable VMWare Hot Add for RAM\n    \tv.vmx&#91;'usb.present'] = true                         # enable USB controller\n    \tv.vmx&#91;'svga.graphicsMemoryKB'] = '262144'           # reserve 256MB for GPU\n                end\n                end\n            end\n        end\nconfig.vm.provision \"shell\", inline: &lt;&lt;-SHELL\n    apt-get update\n    apt-get full-upgrade -y\n    sudo useradd -m -s \/bin\/bash cpotrica\n    reboot\n  SHELL\n\n    end<\/code><\/pre>\n\n\n\n<p>I&#8217;m creating 3 VMS with the names kmaster01, knode01, and knode02<\/p>\n\n\n\n<p>with 2 interfaces one for NAT and the other in a network later used for the Kubernetes configuration. with the IP address: 192.168.0.235 to 237<\/p>\n\n\n\n<p>each VM will have 2GB of RAM and two cores, below I have a small configuration to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get update\napt-get full-upgrade -y\nsudo useradd -m -s \/bin\/bash cpotrica<\/code><\/pre>\n\n\n\n<p>that will update the VMs and create a username for me.<\/p>\n\n\n\n<p>Note that the default user for these boxes is : <\/p>\n\n\n\n<p>Username: vagrant, Password: vagrant or user: root with password: vagrant<\/p>\n\n\n\n<p>Now to create the VMs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant up<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"352\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-75-1024x352.png\" alt=\"\" class=\"wp-image-565\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-75-1024x352.png 1024w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-75-300x103.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-75-768x264.png 768w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-75.png 1166w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"599\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-76.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-76.png 1008w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-76-300x178.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-76-768x456.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"603\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-77-1024x603.png\" alt=\"\" class=\"wp-image-567\" style=\"width:840px;height:auto\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-77-1024x603.png 1024w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-77-300x177.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-77-768x452.png 768w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-77.png 1073w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"563\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-78-1024x563.png\" alt=\"\" class=\"wp-image-568\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-78-1024x563.png 1024w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-78-300x165.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-78-768x422.png 768w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-78.png 1195w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>you will see the machines appear on VMware Fusion:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"114\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-79.png\" alt=\"\" class=\"wp-image-569\"\/><\/figure>\n\n\n\n<p>Now some simple and basic commands for vagrant<\/p>\n\n\n\n<p>Check the status of the VMs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant status<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"177\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-84.png\" alt=\"\" class=\"wp-image-582\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-84.png 666w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-84-300x80.png 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p>Connecting to a VM<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant ssh kmaster01<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"314\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-80.png\" alt=\"\" class=\"wp-image-570\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-80.png 787w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-80-300x120.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-80-768x306.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<p>update the box image of a VM<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant box update kmaster01<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"985\" height=\"137\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-81.png\" alt=\"\" class=\"wp-image-571\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-81.png 985w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-81-300x42.png 300w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-81-768x107.png 768w\" sizes=\"auto, (max-width: 985px) 100vw, 985px\" \/><\/figure>\n\n\n\n<p>list the images (Box) on the system<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant box list<\/code><\/pre>\n\n\n\n<p>more:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"208\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-82.png\" alt=\"\" class=\"wp-image-572\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-82.png 456w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-82-300x137.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<p>Now my tests are over and I want to delete all vms<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vagrant destroy<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"184\" src=\"http:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-83.png\" alt=\"\" class=\"wp-image-573\" srcset=\"https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-83.png 754w, https:\/\/virtuality.pt\/wp-content\/uploads\/2023\/12\/image-83-300x73.png 300w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><\/figure>\n\n\n\n<p>conclusion : <\/p>\n\n\n\n<p>in less than 5 minutes I have 3 VM&#8217;s running with Linux on my fusion ready to go<\/p>\n\n\n\n<p>I hope this was useful for you the example I used for fusion is usable also on Linux or Windows with VMware workstation.<\/p>\n\n\n\n<p>Just check the Vagrant installation on those OS, it&#8217;s very simple and clean.<\/p>\n\n\n\n<p>You can also create folders with different vagrant projects and deployments with different Vagrantfiles<\/p>\n\n\n\n<p>HF !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I&#8217;m on the go and need to test, study, or swiftly showcase a solution to a client while traveling, I rely on VMware Fusion on my MacBook. Recently, I found myself setting up Linux VMs to build a Kubernetes cluster for my testing purposes. As usual, creating multiple VMs can be time-consuming. Seeking a robust yet user-friendly way to automate this process on my laptop, I explored Vagrant by HashiCorp. Vagrant boxes serve as ready-made environments for development purposes, bundling all the necessary components and configurations required to set up a virtual machine swiftly. Picture them as templates or molds for your virtual machines, streamlining the setup process across platforms like VMware, VirtualBox, or Hyper-V. These boxes are a <a href=\"https:\/\/virtuality.pt\/?p=559\" class=\"btn btn-link continue-link\">Read More \/ Ler Mais<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtuality.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=559"}],"version-history":[{"count":6,"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":583,"href":"https:\/\/virtuality.pt\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions\/583"}],"wp:attachment":[{"href":"https:\/\/virtuality.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtuality.pt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtuality.pt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}