Proxmox virtual machine *automation* in Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in one of my last videos i've shown you how you can automate custom virtual machine templates in proxbox we use the free and open source tool packer to automatically install our images with ubuntu and we package it with custom shell scripts to install docker and set up an ip address or use configuration with cloud init but there was one big thing missing in this video and this is automating the virtual machines itself because we still cloned this template manually to a new virtual machine and we also needed to set up credentials ip configurations and resources in the web ui of proxbox and that is exactly what i want to automate in this video with one of my favorite tools terraform so let's do this by the way if you haven't watched the previous video with packers don't worry you can still follow this video along because the terraform proxmox integration doesn't need a custom image prepared with packer you can use the same automation scripts with any other custom vm template you have created and prepared with cloud init regardless of how you've done it i also think it helps to have some basic knowledge about terraform because i probably won't cover all the basics here so if you'd like to learn more about terraform and packer i've linked you both of my video tutorials in the description of this one and then you can check it out later and of course you will also find the project files for this tutorial on my personal github page in the boilerplates repository so just navigate to terraform proxmox and then you will find the templates that i'm explaining here in this video but before we start with the template first i want to say a few words about today's sponsor teleport so we keep talking about amazing free and open source tools teleport is an access proxy to protect your remote resources like ssh linux servers kubernetes databases or web applications with two-factor authentication and an audit logging i always use it when i need to log in from outside my home network or securely access my cloud resources is a great application so just donate and try it out of course you will find a link to their website in the description down below okay so let's first recap what you might or might not know about terraform this is a free open source infrastructure as code automation tool so the goal of infrastructure is code is that you don't need to set up install and configure your it infrastructure through web interfaces or manual processes anymore instead you define all the resources you need in code very similar to a programming language where you describe an object in some states you want these resources to have whatever you define in the code therefore makes sure that this is happening in your environment i've previously used it to deploy cloud infrastructure like kubernetes and docker and it's a very common devops tool that integrates well with other cloud providers but i also recently found a custom teraframe provider for proxmox i'm using proxmoxnow for quite some time in my home lab and it's absolutely my favorite hypervisor to run virtual machines and i've also done some videos about it and i guess many of you guys will already use it so let's take a look at the proxbox provider in the terraform rager street which is made by tellmate and you should know that this is not an official provider because hashicorp the vendor of terraform opened terraform up for any custom implementations and makes it very easy for developers to write their own extensions and the proxmox provider is such a custom extension however i've seen that it's around for quite some time and supported by many people so it's worth taking a look at the gita project it's pretty cool and in the official documentation you can also see how you can use it in terraform to connect it to your local proxmox server but don't worry of course i will walk you through this stuff as well so first we need to create an api token in proxmox because terraform needs to somehow authenticate to our servers and create the required resources if you don't know how to do that in proxmox you need to select your data center and go to permissions api tokens add a new api token for a specific user in my case i've just used the root user here because this user already has all the privileges we need in terraform just enter a name for the token id and now it's really important to select the privilege separation setting because we want our api token to have the same privileges as the root user note that the api token secret will only show up once so when you close the window and forgot the secret you need to create another one and then you just need to copy this token and put these settings in a credentials file in terraform so take care the api token is username and the token id separated with an exclamation mark now that we have the api token let's create the base files for this project and i always start creating a file which i call provider.tf where i put all the general configuration settings for terraform so first i defined the required version for it and the required provider section and here we just need to define proxmox and put the tailmate proxmox provider as our source if you want to specify a version you can also do it as well here and then we need to define three variables for the proxmox credentials remember that you should never store the actual secrets here in the terraform project so just declare the variables for the api url the token id and the token secret as string objects the last two ones is sensitive and i've then created a separate file which i've called credentials.out.tfrs here you can put the secrets in note this file should not be included in any repository so keep it secret because here you can add the three variables to connect to your proxmox server these auto dot tfr files by the way are special in terraform because they are automatically loaded when you locate them in the same project directory so you don't need to specify them in the command later very nice now let's come back to the provider's file and here we just need to set the three parameters that are necessary for the connection to the values of our variables and that should be all no one more thing if you're using a self-signed certificate and your client doesn't trust the connection to the proxmox ui you also need to add a pmt ls insecure attribute in the provider settings and set it to true but then that should be all so i've now initiated the terraform project by executing terraform in it until now that all was pretty straightforward if you're already familiar with terraform it should be easy and now we can create the file for our virtual machine resources we want to create in proxmox so i've just created a new file in the project folder which i've called srv demo one that will be a simple ubuntu server that i want to create from a proxmox vm template that i've already prepared in packer so on my proxmox server you can see these templates i want to use the template with the id 902 which is just a pre-installed ubuntu server in the 20.04 lts version now in the terraform file i've created a resource from the type proxbox vmq mo with the name srv demo1 and first you need to define the general settings of the vm like the name the description the vm id and what is the target node so where do you want this vm to be created on now the next parameter is the agent and this is pretty important it took me quite some time to find it out because in the past i had some problems with the speed of terraform in combination with the tailmate proxmox provider and after researching i found out this has something to do when the qmo guest agent is installed on this virtual machine but you haven't set this agent parameter into the terraform resource so keep that in mind when you're using the guest agent in your vm template don't forget to set this agent parameter to one and now we can specify the other parameters for this virtual machine like how many cpu cores and sockets you want to give it what should be the cpu mode and how much memory the virtual machine should have and of course from which vm template you want to clone it from now in this parameter you just need to set the name of the vm template not the id and it's also important to connect this virtual machine to a network so i've added my default vm bridge and now comes something that also might be a bit confusing because you can specify the disk layout for this virtual machine but be careful when you're using a vm template because this vm template already has a virtual hard drive because it already has an operating system installed on it and when you now specify a disk in terraform they need to have the exact same statements so take care of that you're specifying the same storage location the same type and size for the disk otherwise if these settings are different chevron will most likely create a second hard disk on this vm if the parameter don't match with the template and now comes something that i've already explained in my packer tutorial and this is really amazing it is cloud init remember in my packer tutorial we've used cloud init to specify settings that should be provisioned when the virtual machine is starting like an ip address add a dns server or add username and password and the terraform proxybox provider can also do this for you so in this section i have now specified the os type to cloud init and gave this machine some network configuration i added a static ip and gave it a dns server and i also added an additional user and a public ssh key know that we've done all of this stuff you can see it's not much that we need to specify here so let's go into terraform and run the terraform plan command so this will tell us which resources terraform will create when we apply the changes and if everything is okay you can run the terraform apply command to make these changes happening on proxmox you can see that it now already starts connecting to the server and it already creates the virtual machine now that might take some time to copy because it uses a full clone and not a linked clone image but after this is done it starts up the virtual machine and we should be able to just access it with a static ip address we have defined in the terraform file and yeah we are just now connected to our server and it just took like one and a half minutes to create this new virtual machine fully automatic in code if you ask me that's really amazing so currently i'm not managing any resources on my proxbox server manually i've created a few repositories for all my production and testing vms and i'm all managing entire from now and you can by the way also create and provision lxc containers as well i personally haven't done this because i'm not using alex c but i guess the process will be very similar to the template that i've just used in this video and i hope this will help you in your project as well so please tell me in the comments what are you using to manage your home lab infrastructure and as always thanks everybody for watching enjoy the rest of your day take care of yourself and i see you soon bye
Info
Channel: Christian Lempa
Views: 47,955
Rating: undefined out of 5
Keywords:
Id: dvyeoDBUtsU
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Tue Apr 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.