Hello everybody, we will start to learn about terraform. For this beginning tutoriel i will explain you how you can create simple machine with terraform on your proxmox.
How terraform work ? Terraform is tools allow you to manage infrastructure with configuration files (Iac : Infrastructure as code) rather than through a graphical user interface. Iac allows you to build and manage your infrastucture in a safe and repeatable way by defining resource configurations that you can version, reuse and share. For this tutoriel we need one pc where we will install terraform and one server with proxmox.
Start to install terraform on your pc.
$ sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
$ wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
$ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
$ sudo apt update
$ sudo apt-get install terraform
For the next step you need to create one account on your proxmox server with admin rights. Now you need to create one directory for our terraform configuration file on PC. on this directory we will create 2 files provider.tf with information of terraform provider (module that we will use), variables.tf with declaration of variable that provider we will use.
joel@console:~/$ mkdir terraform && cd terraform
joel@console:~/terraform$ cat <<EOF >>provider.tf
terraform {
required_providers {
proxmox = {
source = "Telmate/proxmox"
version = "2.9.10"
}
}
}
provider "proxmox" {
pm_parallel = 1
pm_tls_insecure = true
pm_api_url = var.pm_api_url
pm_api_token_id = var.pm_user
pm_api_token_secret = var.pm_password
}
EOF
joel@console:~/terraform$ cat <<EOF >>variables.tf
variable "admin_user" {
default = "joel"
}
variable "pm_api_url" {
default = "https://192.168.23.101:8006/api2/json"
}
variable "pm_user" {
default = "joel@pam"
}
variable "pm_password" {
default = "5fsdfb22fSFE20c1e663"
}
EOF
joel@console:~/terraform$ ls
provider.tf variables.tf
After you create this 2 files you will start to run the first terraform command to download provider and init trivial conf.
joel@console:~/terraform$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding telmate/proxmox versions matching "2.9.10"...
- Installing telmate/proxmox v2.9.10...
...
Terraform has been successfully initialized!
joel@console:~/terraform$ terraform plan
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
If you have same message everything look good. For the next step we will create one manifest to create virtual machine on our proxmox with terraform.
joel@console:~/terraform$ cat <<EOF >>ressource_testvm.tf
resource "proxmox_vm_qemu" "ressource_testvm" {
desc = "VM test Server"
name = "test"
target_node = "proxmox1" #Internal name of your proxmox server
cores = 2
sockets = 2
onboot = true
numa = true
hotplug = "network,disk,usb"
iso = "vms2:iso/pfSense-CE-2.6.0-RELEASE-amd64.iso" #replace vms2 with real datastore name
memory = 2048
balloon = 2048
scsihw = "virtio-scsi-pci"
bootdisk = "scsi0"
disk {
size = "10G"
storage = "vms"
type = "scsi"
}
network {
bridge = "vmbr1"
model = "virtio"
}
}
EOF
joel@console:~/terraform$ ls
provider.tf ressource_testvm.tf variables.tf
joel@console:~/terraform$ terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# proxmox_vm_qemu.ressource_testvm will be created
+ resource "proxmox_vm_qemu" "ressource_testvm" {
+ additional_wait = 0
+ agent = 0
...
+ queues = (known after apply)
+ rate = (known after apply)
+ tag = -1
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
joel@console:~/terraform$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# proxmox_vm_qemu.ressource_testvm will be created
+ resource "proxmox_vm_qemu" "ressource_testvm" {
+ additional_wait = 0
+ agent = 0
...
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
proxmox_vm_qemu.ressource_testvm: Creating...
proxmox_vm_qemu.ressource_testvm: Still creating... [10s elapsed]
proxmox_vm_qemu.ressource_testvm: Creation complete after 13s [id=proxmox1/qemu/101]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
If you check your proxmox dashboad you can see that terraform create your vm. to understand. know that command line
- terraform plan: show you difference between configuration on proxmox and you file configuration. same of our case when terraform see that configuration change, he print you différence between real state and your configuration file
- terraform apply: look like terraform plan with same information but he will as you if you want to apply your configuration on the server. And we tell you what i will go to do, create , change or distroy virtual machine on the last message « Plan: 1 to add, 0 to change, 0 to destroy. » please becare full before apply something.
If you want to destroy one ressource with terraform you can use terraform command with ressource name to destroy one ressouce or without ressource name to destroy all ressource. To list the available ressource you can use terraform state list.
joel@console:~/terraform$ terraform state list
proxmox_vm_qemu.ressource_testvm
joel@console:~/terraform$ terraform destroy --target proxmox_vm_qemu.ressource_testvm
proxmox_vm_qemu.ressource_testvm: Refreshing state... [id=planet/qemu/101]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
- destroy
Terraform will perform the following actions:
# proxmox_vm_qemu.ressource_testvm will be destroyed
- resource "proxmox_vm_qemu" "ressource_testvm" {
- additional_wait = 0 -> null
....
- tag = -1 -> null
}
}
Plan: 0 to add, 0 to change, 1 to destroy.
╷
│ Warning: Resource targeting is in effect
Enter a value: yes
proxmox_vm_qemu.ressource_testvm: Destroying... [id=planet/qemu/101]
proxmox_vm_qemu.ressource_testvm: Destruction complete after 4s
╷
│ Warning: Applied changes may be incomplete
....
Destroy complete! Resources: 1 destroyed.
Now you can easy start with terraform.
Laisser un commentaire