Terraform-provider-proxmox

来自linux中国网wiki
跳到导航 跳到搜索

terraform 和proxmox的笔记终于有空放上wiki了

prepare

Proxmox VE,terraform 已安装 我们这里用了 Cloud-Init ,其实还可以ISO的 请见

相关下载 https://cloud-images.ubuntu.com/

Oct 08 2023 update

https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img

创建Ubuntu(Cloud-Init) Template

#执行下面的命令创建一个虚拟机
#这里 9000 只是一个ID 你只要写个你的 proxmox还没用的ID就行了
qm create 9000 --name "ubuntu-2004-cloudinit-template" --memory 1024 --cores 1 --net0 virtio,bridge=vmbr0
qm importdisk 9000 ubuntu-20.04-server-cloudimg-amd64.img local-lvm
qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0
qm set 9000 --boot c --bootdisk scsi0
qm set 9000 --ide2 local-lvm:cloudinit
qm set 9000 --serial0 socket --vga serial0
qm set 9000 --agent enabled=1

#将刚创建好的虚拟机转换成模板
qm template 9000

API token

pveum user add terraform-evan@pve 
#我这里权限给得比较大 
pveum aclmod / -user terraform-evan@pve -role Administrator
pveum user token add terraform-evan@pve terraform-token --privsep=0
──────────────┬──────────────────────────────────────┐
│ key          │ value                                │
╞══════════════╪══════════════════════════════════════╡
│ full-tokenid │ terraform-evan@pve!terraform-token   │
├──────────────┼──────────────────────────────────────┤
│ info         │ {"privsep":"0"}                      │
├──────────────┼──────────────────────────────────────┤
│ value        │ 48ad9bae-98ad-49f5-a6d3-ac08f8700000 │
└──────────────┴──────────────────────────────────────┘


terraform


#用户名什么的 写在变量文件
 cat main.tf
terraform {
  required_providers {
    proxmox = {
      source = "Telmate/proxmox"
      version = "2.9.11"
    }
  }
}

provider "proxmox" {
  pm_tls_insecure     = true
  pm_api_url          = "https://192.168.10.8:8006/api2/json"
  pm_api_token_id     = "terraform-evan@pve!terraform-token"
  pm_api_token_secret = "48ad9bae-98ad-49f5-a6d3-ac08f8726020"
}
resource "proxmox_vm_qemu" "proxmox-ubuntu" {
  # 创建数量填写这里
  count = 4 
  name  = "ubuntu-2004-${count.index + 1}"
  desc  = "Ubuntu develop environment"

  # 节点名
  #target_node = "pve"
  target_node = var.proxmox_host

  # cloud-init template
  #clone = "ubuntu-2004-cloudinit-template"
  clone = var.template_name

  # 关机 guest agent
  agent   = 0
  os_type = "ubuntu"
  onboot  = true
  # CPU
  cores    = 2
  sockets  = 2
  cpu      = "host"
  # 内存M
  memory   = 4384
  scsihw   = "virtio-scsi-pci"
  bootdisk = "scsi0"

  # 硬盘设置,因计算的方式 101580M 代替 100G
  disk {
    slot     = 0
    size     = "28G"
    type     = "scsi"
    storage  = "local-lvm"
    iothread = 1
  }

  # 网络
  network {
    model  = "virtio"
    bridge = "vmbr0"
  }

  lifecycle {
    ignore_changes = [
      network,
    ]
  }
  # 记住这里要使用IP CIDR。因为只创建一个虚拟机,虚拟机的 IP 是 192.168.1.41。如果要创建多个虚拟机的话,IP 将会是 .91、.92、.93 。
  #ipconfig0 = "ip=192.168.10.4${count.index + 1}/24,gw=192.168.10.1"
  ipconfig0 = "ip=192.168.10.3${count.index + 1}/24,gw=192.168.10.1"

  # 用户名和 SSH key
  #ciuser  = "evan"
  ciuser  = var.myuser
  sshkeys = <<EOF
  ${var.ssh_key}
  EOF
}




cat vars.tf 

variable "myuser" {
    default = "evan"
}
variable "ssh_key" {
  default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTR3R8Bx1+koK8bJzoYMpGU1S8DTDnptWqrTCOMhrVlQH0I9ll/3ZAcD53JdzRXVLe1NqE6q5iTFO+HY5qxWfM0+gerJQlb1e5cb1+lUFhRVglpwYlxNSVXaJFeiCLswcnGcmqR1RnGAGhcf7ncslz/BLuxFndgLqhU04zj9ISJWYh+36vttqg9tP6a40MhmeWVPOuIGNlDDVVUyjxIepn83xr0PJyILLXTBH+OiQppO1bljguig67twUBsh/FW1Wdvuw33PjkgXmKLDVaKS2S0CtC9dPOrp0afS/fsKrIu16o/VtfSrNrTmsTCmE95Ug25yOS+WuVaU19Gauez2mj lx"
}

variable "proxmox_host" {
    default = "pve"
}

variable "template_name" {
    default = "ubuntu-2004-cloudinit-template"
}











#original notes
#cat main.tf
terraform {
  required_providers {
    proxmox = {
      source = "Telmate/proxmox"
      version = "2.9.11"
    }
  }
}

provider "proxmox" {
  pm_tls_insecure     = true
  pm_api_url          = "https://192.168.10.8:8006/api2/json"
  pm_api_token_id     = "terraform-evan@pve!terraform-token"
  pm_api_token_secret = "48ad9bae-98ad-49f5-a6d3-ac08f8000000"
}
resource "proxmox_vm_qemu" "proxmox-ubuntu" {
  # 创建数量填写这里
  count = 4 
  #name  = "ubuntu-2004-${count.index + 1}"
  name  = "ubuntu-2004-${count.index + 1}"
  desc  = "Ubuntu develop environment"

  # 节点名
  target_node = "pve"

  # cloud-init template
  clone = "ubuntu-2004-cloudinit-template"

  # 关机 guest agent
  agent   = 0
  os_type = "ubuntu"
  onboot  = true
  # CPU
  cores    = 2
  sockets  = 2
  cpu      = "host"
  # 内存M
  memory   = 4384
  scsihw   = "virtio-scsi-pci"
  bootdisk = "scsi0"

  # 硬盘设置,因计算的方式 101580M 代替 100G
  disk {
    slot     = 0
    size     = "28G"
    type     = "scsi"
    storage  = "local-lvm"
    iothread = 1
  }

  # 网络
  network {
    model  = "virtio"
    bridge = "vmbr0"
  }

  lifecycle {
    ignore_changes = [
      network,
    ]
  }
  # 记住这里要使用IP CIDR。因为只创建一个虚拟机,虚拟机的 IP 是 192.168.1.41。如果要创建多个虚拟机的话,IP 将会是 .91、.92、.93 。
  #ipconfig0 = "ip=192.168.10.4${count.index + 1}/24,gw=192.168.10.1"
  ipconfig0 = "ip=192.168.10.3${count.index + 1}/24,gw=192.168.10.1"

  # 用户名和 SSH key
  ciuser  = "evan"
  sshkeys = <<EOF
  ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTR3R8Bx1stridoYMpGU1S8DTDnptWqrTCOMhrVlQH0I9ll/3ZAcD53JdzRXVLe1NqE6q5iTFO+HY5qxWfM0+gerJQlb1e5cb1+lUFhRVglpwYlxNSVXaJFeiCLswcnGcmqR1RnGAGhcf7ncslz/BLuxFndgLqhU04zj9ISJWYh+36vttqg9tP6a40MhmeWVPOuIGNlDDVVUyjxIepn83xr0PJyILLXTBH+OiQxxxguig67twUBsh/FW1Wdvuw33PjkgXmKLDVaKS2S0CtC9dPOrp0afS/fsKrIu16o/VtfSrNrTmsTCmE95Ug25yOS+WuVaU19Gauez2mj lx
  EOF
}



运行

#init
terraform init
可以使用 terraform fmt 和 terraform validate 对配置文件进行格式化和校验。

然后执行 terraform apply 并输入 yes 开始创建虚拟机

#Destroy previously-created infrastructure
terraform destory


改进

使用 qm 创建 Ubuntu Cloud-Init Template,有可能要找个全 terraform的 通过 cloud-init 来对虚拟机进行高级定制 各种lib并没有安装 ,默认是非常干净的ubuntu

还有,这两个文章的写作格式非常的值得学习

Creating a VM Template in Proxmox

Using Terraform with Proxmox

trouble shooting

1.ubuntu terraform init 
Terraform initialized in an empty directory!

#写多了一个 t ttf 
➜  ubuntu ls 
main.ttf  ubuntu.ttf
➜  ubuntu mv main.ttf main.tf 

2.连不上 gihthub 问题 
➜  ubuntu terraform init           

Initializing the backend...

Initializing provider plugins...
- Finding telmate/proxmox versions matching "2.9.13"...
- Installing telmate/proxmox v2.9.13...
╷
│ Error: Failed to install provider
│ 
│ Error while installing telmate/proxmox v2.9.13: Get
│ "https://github.com/Telmate/terraform-provider-proxmox/releases/download/v2.9.13/terraform-provider-proxmox_2.9.13_linux_amd64.zip":
│ dial tcp 20.205.243.166:443: i/o timeout


本地有个 socket5 1081代理  

cat    ~/.ssh/config
### github.com
Host github.com
    Hostname github.com
    ProxyCommand nc -x localhost:1081 %h %p
    # git-for-windows 下可以用 connect 代替 nc
    # ProxyCommand connect -S localhost:1085 %h %p


3. v2.9.13 版本有问题 
ubuntu terraform apply 
╷
│ Error: user does not exist or has insufficient permissions on proxmox: ops@pve!terraform-token
│ 
│   with provider["registry.terraform.io/telmate/proxmox"],
│   on main.tf line 10, in provider "proxmox":
│   10: provider "proxmox" {


Also confirming v2.9.11 of provider works fine

https://github.com/Telmate/terraform-provider-proxmox/issues/703


References

最好看的两个官方文档 https://github.com/Telmate/terraform-provider-proxmox

https://github.com/Telmate/terraform-provider-proxmox/blob/master/docs/resources/vm_qemu.md

https://registry.terraform.io/providers/Telmate/proxmox/latest/docs/guides/cloud_init

快速搭建实验环境:使用 Terraform 部署 Proxmox 虚拟机


使用 Terraform 部署 Proxmox 虚拟机