Hashicorp Packer to Build a Ubuntu 20.04 Image Template in VMware

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone this is sam gabriel and i'm excited to share with you today a new video uh regarding hashicorp packer and specifically we're going to see an example of creating a ubuntu 2004 image inside of vmware using packer so that later in another video we can see how to use that image to build a vm inside of vmware with terraform now in talking to multiple customers i found that many teams at different organizations have seen the benefits of infrastructures code specifically the cloud teams and using packer and terraform in cloud environments such as aws azure gcp but then we find that the private cloud teams that have vmware environments on-prem have seen the benefits that these teams the cloud teams have gained by using infrastructure as code so we're seeing a lot more adoption in terms of using packer and terraform in vmware environments and that's why i decided to record this video and show an example of how you can use packer and terraform inside of vmware so again for today for this video we're going to talk about packer and in another video later we'll talk about terraform on vmware so let's get started all right so let's take a look at the setup it's very simple i have my desktop and i have a packer cli basically running on my desktop and then i'm calling out to vcenter and i have an instance of that in my home lab and packer talks to vcenter creates a vm based on the configuration parameters that i put in place it takes a little bit of time i think about 15 minutes or so and once you have everything going it's going to create that image for you and there is really no intervention needed you don't need to go click anywhere except anything inside of you know the ubuntu installer or anything like that and basically it helps really when you want to automate the process when you want to self-document the process and all the benefits that come with infrastructure as code so let's get started and take a look here in my repo what i'll do is i will just run the command that will get this all to work and while we're waiting for this to actually finish i'll explain to you how everything works so this is the command here i have in my readme file and this repo is going to be shared with everybody so you can go ahead and leverage that and use it so let's paste that command here and hit enter and we're just going to sit and wait if you're interested in obviously looking through the logs feel free to do so especially the first time but basically we are now connected into vsphere and we've asked vcenter to go ahead and create a template for us so while this runs i will do a couple of things first let me show you um what what vcenter looks like so as you can see here under my templates section uh i have a template running called ubuntu 2004 template or 2004 basically template 100 gigabytes thin so this is currently running right now it's generating a vm and it's creating everything necessary in that vm installing ubuntu and so on and if you click inside the web console you can see it live has as it as it happens inside of the console here so you can obviously look at the the logs here directly in v and vsphere and or check the logs inside of inside of uh packer now there are some obviously some more logs that you'll see as the vm loads and reboots and and gets things going so you might want to check both at least for the first time until you're comfortable with what's going on okay so we'll let this run as you can see here we're just waiting for an ip to come back so we're running the http dhcp server in in vsphere and uh and here you go we got an ip back and now we're just waiting for ssh to become available and i'll explain why that is uh but things are going very well right now so let's bring this down a little bit i'll explain a couple of things um there the new way of installing ubuntu's especially since 2004 uh it relies on something called subequity server and it's a different way of doing things than what we're used to before with a pre-seed file and so on that was called a debian what's it called let me double check here by the way there is a blog post that i'll be uh releasing with all this information as well with the video embedded in it as usual so look out for that yeah so sub equity server installer is the new way of installing ubuntu it was available in ubuntu 1804 but it was optional the classic server was called debian installer and so so the new one uses cloud init the old one used the precede file that i mentioned before and i had it in a previous video of packer with ubuntu 1804 where we walked through the precede file and showed how that file is used basically for the installation all the configuration necessary for the installation of of ubuntu so yeah so the new way of doing things is moving towards cloud init and with cloud init there are a few things to to take into consideration you can see here the http folder it has two files the metadata file which is empty for an on-prem installation in our case in vmware so we don't need to put anything there but we need to have the file there nevertheless if you're running this in cloud then you you can add some metadata in here and then the other file is called the user data file so in here there are a few things that you can you can take a look at so you can see here that we can have some early commands in the very beginning and actually in this installer we stop the um ssh process because we found that packer somehow times out thinking that you know it's still waiting for for ssh to become available as you can see here it may time out so it's better to to shut this off and once the machine restarts the ssh process is going to come up automatically so that you can run a bunch of early commands in this section the other things as you can see here some of the configurations around the keyboard you can also install packages as you can see we have a bunch of packages that we're installing here as part of cloud init some network configuration some identity configuration and notice this one here this password to get to that i have it here in the readme file how to create that i can actually show you how to do it if you open a new terminal i have the commands here first thing is make sure you have who is installed once you have that we're going to use the mk password utility so if i copy that and let's paste that command in here it's going to ask you for the password that you're trying to hash or encrypt so if i if i type say ubuntu hit enter it will come back with a a hash like you see here and that is what's needed so you can copy that and that's what's needed to be put inside this user data password field in in the identity section after that you can see the ssh section where we're allowing a password yes install server yes this is the public key that is needed so that you can ssh into this box once it's created so you can you can push the public key or put the public key here and then storage section and then some late commands that you can have at the very end uh it's pretty pretty straightforward so you're adding this ubuntu user to the sudoers files file you don't need the password to issue the sudo command so that's the first section to notice in our configuration this http folder the second piece is a scripts folder and these scripts can run at the very end after packer has completed it's run let's keep it here so we can see if it's done but here's an example of a script that you can run at the very end in the provisioning section of packer so you can see a few things one important thing to do is to make sure that you clean the machine id so you're able to clone that image later on as you're creating that vm so these are the commands to do so for my particular use case i'm using i'm going to create a k3s installation a master and a few worker nodes this is a a version of or distribution of kubernetes from created by the rancher folks so it's a stripped down kubernetes distribution that's very suitable in lab environments small environments edge environments even on your raspberry pi and so on so some of the requirements there you know adding iscsi daemon getting it to restart ntp and so on if you're using longhorns specifically for for storage other things that we need to take into consideration as well such as making a directory directory for dhcp in some of these things i added them because the packer install will fail and then i go google and find out okay why did it fail oh it's because of this thing that needs to be added so some of these things i had to add so this is all based on a lot of research a lot of hours spent to get this to finally work but once it works you're good to go to reuse it over and over again so don't be discouraged if you know the packer run doesn't doesn't succeed from the very first try it didn't for me it took me a good over 20 hours to get this finally to work especially since we're it's using the new like i said cloud init version with 20.04 so don't be discouraged once again um you'll eventually get it to work um so but this this one works it's tested and in the blog post i'll i'll put all the instructions if i miss anything here in this video uh so you can follow along you can also see that we're installing a docker in this case i want this template to have docker so that whenever i use terraform to spin up vm based on this template it already has docker installed all right so that's one script i have another script as well it's the same exact thing except that i'm not installing docker the reason for the difference is that one with docker i actually used it as a vm to run gitlab in as a gitlab runner inside of it whereas the other one is going to be used for all my k3s machines so the master and the workers okay so that's the scripts the other piece that i want to show you is that i have two versions of this packer template basically the first one is based on the old way of running packer with json and the second one is using hcl which is the hashicorp configuration language the preference or my preference now is to move towards the hashicorp configuration language hcl this is the same language that's used for all of hashicorps tools such as terraform and vault console nomad and so on and that's definitely the direction that hashicorp is taking packers so might as well get on board with that so what i'll do is i'll walk you through the a couple of files here the hcl ones but again this has been tested for both the json version and the hdl version so basically what you need here is this htl main file and then a bunch of variable files one here this 100 gig gigabyte disk the 650 and then an example um file where you can put all the secret information that you need which is basically uh the credentials that you need to get into your vsphere environment so i also have this example ready for you to to look at in the repo obviously remove this example keyword i guess and then you put your own secrets in there but make sure you again you don't check it into um into your git so as you can see my git ignore here i'm ignoring the variable secrets.json if i'm using if you use the json version or the uh the vsphere packervars hcl which is this one okay so let's uh let's take a quick look at this main file here and see what it's all about in the very beginning similar to terraform if you're familiar with terraform it's a very exact same syntax in terms of defining variables so you can see vcenter username vcenter password and so on so i have a bunch of variables that are defined at the top of this file i'm not going to go into each one of those they're pretty easy to follow and understand there's a locals variable here to be used for basically a build time that you can attach in the notes section of that template and then there's the source here relying on vsphere iso and we name it linux ubuntu server and here we attach a lot of these variables that we kind of defined before vcenter server username password data center and so on here is the notes section where we can say built by hashicorp packer on specific time defining storage network adapters the url for the iso image where actually the the base image the base ubuntu image where we're grabbing that from uh checksum um an http directory which is the actual directory here cd files this is we're going to be mounting those two files metadata and user data using cd-rom inside of vsphere boot command to get things started the ssh password and username um and so on finally the build section we call on our source we call on our source vsphere iso linux ubuntu server which is what i just showed you here vsphere iso linux ubuntu server and in here i have a provisioner and this is the provision that's going to run after everything is is complete to ssh into the machine and run the scripts that i mentioned before so the scripts that are in here you can see it's referred to here okay and that basically concludes the main hcl file to get everything running now we can take a look at the variables files i have two of those the first one is this one which defines or assigns i should say all the uh http directory and all the vm settings so what what the name of the vm is going to be or the template i should say so this is the template that will end up showing inside of vsphere which is what we're already seeing as it's running the type of vm guest os the version and a few other things in addition to that i'm already showing the the disk so this disk size is 100 gigabytes this one here is going to be 650 and the reason why i have two is one is going to be for my master uh k3s machine and the other one is going to be for my worker notes the provisioning type is going to be thin provisioning you can also change that to thick provisioning if you wish ssh username and password would be ubuntu and here is where i define the the iso file and the checksum and also where i'm grabbing the ubuntu iso from okay and finally the shell scripts that i'm going to use for this machine which i'll use with um the master node is going to run in this case docker for example and then finally the last uh the last file that i want to show you in terms of variables is this vsphere pkr or packer vars example where we're looking at the credentials for for your vcenter or vsphere in general obviously these are not mine i just put them here as an example and then also how to connect to to to vcenter so the ip address uh the name of the data center the name of the data store network uh the folder and so on and and that's pretty much it i can see here that the build has successfully completed you can see that it completed in 14 and a half minutes everything is complete everything is green looks good to me so we can go back and take a look here is our web console it has completed so this we don't know we no longer need it it's already powered off the template is ready to go ready to be provisioned ready to use terraform for example to provision that and and we're good to go so hopefully this has been helpful as an example to show you how to use packer within vmware to generate a ubuntu 2004 image the same thing can be done with any kind of image in previous videos i showed you how to use windows 2019 i create a windows image also with packer and also do you how to use terraform to do that but i didn't show you 2004 or ubuntu 24 before with the new cloud in it so i wanted to record this video to show you this is an example and any subsequent video i will use the same template we just created with terraform to build a vm inside of vmware thank you for watching [Music]
Info
Channel: TeKanAid
Views: 837
Rating: undefined out of 5
Keywords: hashicorp packer to build a ubuntu 20.04 image tempalte in vmware, hashicorp packer, hashicorp packer tutorial, hashicorp packer vmware, infrastructure as code for vmware with terraform, HasiCorp Packer for VMware, packer linux, packer ubuntu, Packer HasiCorp, infrastructure as code, packer ubuntu 20.04, packer ubuntu 20.04 vmware, packer vmware-iso example, packer vmware tutorial, Packer vmware-iso ubuntu 20.04, automation with hashicorp packer, packer ubuntu 20.04 vsphere
Id: SQnjEcpXX_I
Channel Id: undefined
Length: 20min 10sec (1210 seconds)
Published: Tue Sep 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.