“Terraform-provider-proxmox”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第188行: 第188行:
 
</pre>
 
</pre>
  
 +
=References=
  
 +
[https://zhuanlan.zhihu.com/p/453058774 快速搭建实验环境:使用 Terraform 部署 Proxmox 虚拟机]
 
[[category:devops]] [[category:terraform]]
 
[[category:devops]] [[category:terraform]]

2023年3月11日 (六) 11:29的版本

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

pre

Proxmox VE,terraform 已安装

创建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-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的

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

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