Works for spinning up a VM
This commit is contained in:
106
Cloud-Init VM/main.tf
Normal file
106
Cloud-Init VM/main.tf
Normal file
@@ -0,0 +1,106 @@
|
||||
# Initializes a new Proxmox VM given a specific cloud-init image, and attempts to enroll it in FreeIPA
|
||||
terraform {
|
||||
required_providers {
|
||||
proxmox = {
|
||||
source = "bpg/proxmox"
|
||||
version = "0.78.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "proxmox" {
|
||||
endpoint = var.api_url
|
||||
api_token = "${var.proxmox_token_id}=${var.proxmox_token_secret}"
|
||||
insecure = true
|
||||
|
||||
ssh {
|
||||
agent = false
|
||||
username = var.proxmox_ssh_username
|
||||
password = var.proxmox_ssh_password
|
||||
}
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_file" "user_data_cloud_config" {
|
||||
content_type = "snippets"
|
||||
datastore_id = "local"
|
||||
node_name = var.proxmox_node_name
|
||||
|
||||
source_raw {
|
||||
data = <<-EOF
|
||||
#cloud-config
|
||||
hostname: ${var.vm_hostname}
|
||||
fqdn: ${var.vm_hostname}
|
||||
prefer_fqdn_over_hostname: true
|
||||
package_update: true
|
||||
packages:
|
||||
- qemu-guest-agent
|
||||
- net-tools
|
||||
- curl
|
||||
- freeipa-client
|
||||
runcmd:
|
||||
- systemctl enable qemu-guest-agent
|
||||
- systemctl start qemu-guest-agent
|
||||
- ipa-client-install --mkhomedir --unattended --principal ${var.freeipa_enrollment_principal} --password ${var.freeipa_enrollment_password}
|
||||
- echo "done" > /tmp/cloud-config.done
|
||||
EOF
|
||||
|
||||
file_name = "user-data-cloud-config.yaml"
|
||||
}
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_download_file" "vm_cloud_image" {
|
||||
content_type = "iso"
|
||||
datastore_id = "local"
|
||||
node_name = var.proxmox_node_name
|
||||
|
||||
url = var.image_url
|
||||
file_name = var.image_file_name
|
||||
}
|
||||
|
||||
resource "proxmox_virtual_environment_vm" "ubuntu_vm" {
|
||||
name = var.vm_hostname
|
||||
node_name = var.proxmox_node_name
|
||||
|
||||
agent {
|
||||
enabled = true
|
||||
}
|
||||
|
||||
cpu {
|
||||
cores = var.cores
|
||||
type ="x86-64-v2-AES"
|
||||
}
|
||||
|
||||
memory {
|
||||
dedicated = var.memory
|
||||
}
|
||||
|
||||
disk {
|
||||
datastore_id = var.disk_storage
|
||||
file_id = proxmox_virtual_environment_download_file.vm_cloud_image.id
|
||||
interface = "scsi0"
|
||||
iothread = false
|
||||
discard = "ignore"
|
||||
size = var.disk_size
|
||||
}
|
||||
|
||||
initialization {
|
||||
ip_config {
|
||||
ipv4 {
|
||||
address = var.ipv4_address
|
||||
gateway = var.ipv4_gateway
|
||||
}
|
||||
}
|
||||
|
||||
dns {
|
||||
domain = var.dns_domain
|
||||
servers = var.dns_servers
|
||||
}
|
||||
|
||||
user_data_file_id = proxmox_virtual_environment_file.user_data_cloud_config.id
|
||||
}
|
||||
|
||||
network_device {
|
||||
bridge = var.network_bridge_device
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user