Proxmox Templates Made Easy: Ansible Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and with the power of ansible we have our three templates hello again and welcome back and as you saw in our intro we're going to be creating our own proxmox templates using ansible today I don't want to go through 10 000 steps every single time I want to spin up a new virtual machine on my cluster and instead I want to simplify that process and that's exactly what I've done here with this ansible rule in this video I want to step through actually what this ansible role is doing and how it allows me to configure virtual machine templates very quickly and just allow me to dynamically Define what new templates I want to have and create them going forward so let's talk a little bit about why you would want to create a virtual machine template instead of just configuring a virtual machine and using that from the start well for one it allows you to have a consistency Factor that's associated to your machines so it allows you to maintain a level of uniformity across your infrastructure also reducing the risk of some sort of configuration error that you would have with your deployments there's also a Time efficiency metric that's associated to this right so if we're testing new VMS or new deployment methodologies for our ansible playbooks or things like that I want to be able to create a new VM quickly which this allows me to do and instead of configuring it from scratch every time I have a starting point that's already predefined and will significantly speed up my provisioning process as you saw in the intro I think there's a much better way of creating proxmox templates most of the Guides Online that I've seen are guiding you through creating the templates through the UI whereas there are commands that can be run to configure those templates through the VM and through ansible specifically in this video I want to step through how I've been doing and how I can easily create three four five six whatever templates we need using Cloud init and using ansible to pull those images and configure them for us as part of the ansible rule we're actually going to be creating three Ubuntu templates now this can be used to create other images or other templates based on cloud images but I've not tested it I know that I use Ubuntu on a regular basis and it just makes it a lot easier for me to standardize my operating systems on a couple of key images now in this video I want to step through the ansible role and tell you how I'm configuring my templates for my proxbox host before we get started digging through the test I want to step through our variables that we have at our disposal because it makes it a little bit easier to understand what's actually happening here so the isopath is where we're actually downloading the isos from the web and placing them on the proxmox instance so we're going to be creating a folder named VAR isos and downloading all of our Ubuntu images that we have here the next couple options are pretty self-explanatory of how much memory do you want for the templates how much core how many cores do you want and then what's the size of the disk that you want to create now I usually default to 32 but typically you can go to like 10 or 15 and that'll work but you'll run into some uh disk constraints as you continue to push the limit there now the other nice thing here is we have a starting template ID what this is is it actually allows you to define a template set that says okay here's your range of where your minimum template can be deployed to so if you have your VMS in let's say a thousand to two thousand you want to have your templates in some other range so they're easier to find by ID so what this will actually do is allow you to define a minimum template ID and then if your templates already exist at this ID or above it'll find the next available ID in the instance we also are able to Define where we want to host this VM on now if you have a shared storage I recommend changing it over and shares storage gives you a way of migrating instances and just makes it a little bit easier to configure your VMS and have a centralized template whereas if you have local storage for all of your hosts you then have to create these templates on each machine finally when we get to the cloud images you're going to be entering in an array of objects here these are going to be the URLs that you're pulling the isos from along with what the template name is and then an additional description you want to add to the template notes the template notes are already predefined this will just allow you to say Okay I want to have this specific template for this specific reason maybe you're doing custom modifications or isos for it so then we just have a few of those by default built into the role and then once we finish getting through those cloud images we're going to Define our Cloud init user this will be the username that by default is added to your instance and allow you to log into your instance you're also able to Define what IP configuration you want by default I just use ipp4 which is configured via DHCP I usually leave it at DHCP just because it allows me to instantly log into the machine without having to do any configurations after I create the VM from the template you're also able to provide a SSH key now I don't use the password to log into my host so I have omitted the ability to configure a password Here on your VM templates because I think it's more of a vulnerability and instead you should be using some sort of central configuration of an SSH key that allows maybe your ansible user to connect into your host and then set up your initial configuration or your group configuration for your users to log in from there now I also have this option that installs the proxmox qemu agent which allows you to make certain calls like rebooting and powering off the machine safely without actually logging into the machine itself so then when we scroll down to the bottom here I also have a configuration of what the proxmox Playbook will look like and this will be very similar to the one you've seen previously on the real Tech driver fix if you watch that video basically what this Playbook is going to be doing is going to be connecting to a specific host in your proximox cluster and then you're going to be prompted for the password for the root user basically this allows you to log into your proxmox host using the same username and password and you would as the web UI the reason I've done this is because it's just an easier way of accessing the proxmox host without needing to configure or an SSH key on the root user or potentially breaking your root user for your proxbox host so diving into the role I have actually provided a main template.yaml which provides you with the same configuration you have in the readme file which is just added in here now in order to have your role pick up on these you're going to have to name this main.yaml but since I have some sensitive information in my main yaml I have added it to a git ignore so you won't be able to commit any of that information up to your repository by mistake and have someone potentially read your SSH key or anything like that so our role will run three tasks that files that we have defined for ansible so we're first going to be downloading the images and then customizing them and then we're going to be creating the template now in the download of images is where we're actually defining the VM list and how we do the increment for our template ID so we're going to be actually reading a file called dot VM list from our PVE folder and we're going to be parsing out this Json content now this is a little bit of a blob type scroll here but it does eventually get you your max VM ID that you already have to find in your cluster and then start by saying okay well which one is the starting template ID and which one's greater and then what we're actually going to be doing is actually creating our ISO path and then downloading each of our isos that we've defined in our variables into that path once we've downloaded all of our isos in that path we're going to be moving on to customizing those images now this involves a tool called libguest FS tools and we're going to be installing that on the instance itself and we're going to be customizing our ISO to have it become a qcal 2 file along with installing the qemu agent on the iso itself and then we're going to be resizing this so it doesn't take up the whole disk when you create this Q cow 2 which makes the disc smaller to transfer over to the template now when we create a template we're going to be creating a VM first and then converting it over to our template now what we do here is we actually create our VM by defining these variables of the template memory the ansible loop index ID which allows us to increment our next VM ID now that's a little bit of a confusing because a lot of variables going on there but basically what we're saying is we're going to create a VM where the next available ID in our proximox cluster is open and then we're going to add the ansible loop index which basically says for every item in the cloud images variables that we have defined we're going to be creating an image and that's basically what it boils down to we're also going to be setting a description for our image Itself by using a fact that's defined for each of these items in the cloud images now this is the default which allows you to say okay I am using this template and what storage is configured on with how much memory how much CPU cores we also are going to add our SSH key and then modify everything else using a custom shell script that's defined in our files inside of the config file we're passing it a few different arguments to this file we're going to pass in the vmid that we're going to be modifying what ipconfig settings we want to have our SSH key our CI user which is our Cloud init user what data store this VM is going to be put on and what image is going to be modified during this qm set now all we're doing in this config is actually doing a bunch of qm set configs but in some of these you're not allowed to run them multiple times so we first have to check that the qm value has not been said before and if it hasn't where you actually go through and set these VM template settings now at the bottom here is where we actually do most of our configuration to set the auto start what the boot order is and that our VM starts on boot this is because I wanted to make sure that our instances always come up aligned even though we have battery backups insert and other things in place now that we've configured our virtual machines we actually want to convert these over our template so we can use them on our croxbox cluster to create other virtual machines to do this we're just running our qm template command with our VM IDs that we have for our cloud images so now that we help know how the role works I actually want to run this role for you guys so you can understand like what it looks like when it's running and I'll speed this up of course so the first thing that's going to happen is we're going to be prompted for the ansible password that we're going to be using to connect to the remote host so this will just be the same root user password that we use to log into the web UI once we enter that in we'll start Gathering the facts about the host and then we'll start creating the VMS okay what oh brother so now that we know we're all not perfect I spent a little bit of time debugging and found out that error was actually caused by the fact that I deleted all of my VMS from the proxbox instance and thus had no VMS to reference in the VM list so instead now what I'm doing is by default checking the qm list Dash full which lists all the VMS in your proxmox cluster and then we'll set a default set fact for the next VM ID that's available using the same IDs that are in the proxbox cluster defaults and then we're going to be only checking this VM list if the standard out of this command is not blank so this allows us to avoid the fact or the scenario that we ran into there where the VM list was empty thus it couldn't parse that file anymore I also added a debug so you can see the next VM ID that we're going to be creating the templates on so let's actually step back into the ansible role and see what we can do with their playbook now that we fix this error so now round two we're back with our ansible Playbook setup and we're going to prompt and enter our password Here Again and as you can see here we have our new Cloud init message debug here for our next available proxmox template since there were no instances on my Proxima cluster currently we defaulted to the variable of 9000 for our new proxmox template to be created on and with the power of ansible we have our three templates and there you have it the incredible efficiency that's associated to ansible with creating these proxmox templates is just insane if you found this tutorial valuable don't forget to like share and subscribe for more Tech tutorials like this if you have any questions or want to see more content on specific topics please leave a comment below and thanks for watching we'll see you in the next video
Info
Channel: Pragmatic Engineering
Views: 21,323
Rating: undefined out of 5
Keywords: ansible, ansible tutorial, ansible tutorial for beginners, automation, devops, devops tools, linux, proxmox, sysadmin, techtutorial, templates, virtualization, homelab, open source, system administration, devopslife, tech enthusiast, ITProfessional, SysAdminLife, OpenSourceCommunity, TechTalk, TechExplorers, ITPros, TechJunkies, redhat, ansible syntax, configuration management, automation with ansible, ansible modules, information technology, ansible roles, learn ansible, proxmox virtual environment
Id: lO714Bk0tV4
Channel Id: undefined
Length: 13min 29sec (809 seconds)
Published: Mon Oct 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.