0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenTofuでProxmox VEのCT(Container)を起動する

Last updated at Posted at 2024-10-23

0. はじめに

Proxmoxでコンテナとか気軽に立てられたらいいんじゃないかと思い立って、Proxmoxをインストールし、時代は豆腐だと思ったので、OpenTofuを使ったらハマったので、ムシャクシャしてQiita記事を書きます。

1. Proxmoxは普通に?インストールします。

バージョンは以下の通りです。

Manager Version pve-manager/8.2.7/3e0176e6bb2ade3b
Linux 6.8.4-2-pve (2024-04-10T17:36Z)

ubuntu-22.04-standard_22.04-1_amd64.tar.zst を後ほど使うので、
CT Templateでダウンロードしておきましょう。
image.png

2. OpenTofuの注意点

以下の方法でインストールすればOK
何の問題もありません。

Installing OpenTofu on .deb-based Linux (Debian, Ubuntu, etc.) | OpenTofu
https://opentofu.org/docs/intro/install/deb/

OpenTofuのバージョンは以下の通りです。

OpenTofu v1.8.3
on linux_amd64
+ provider registry.opentofu.org/telmate/proxmox v2.9.11

3. tfファイル

terraform {
  required_providers {
    proxmox = {
      source = "telmate/proxmox"
      version = "2.9.11"
    }
  }
}

provider "proxmox" {
  pm_api_url = "https://proxmox.hogehoge.dev:8006/api2/json"
  pm_log_enable = true
  pm_log_file   = "terraform-plugin-proxmox.log"
  pm_debug      = true
  pm_log_levels = {
    _default    = "debug"
    _capturelog = ""
  }
}

resource "proxmox_lxc" "ubuntu_container" {
  target_node  = "proxmox"
  hostname     = "ubuntu-22-04-lxc"
  ostemplate   = "local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst"
  password     = "your-root-password"
  unprivileged = true

  // 基本設定
  cores  = 2
  memory = 2048
  swap   = 512
  onboot = true

  // ストレージ
  rootfs {
    storage = "local-lvm"
    size    = "8G"
  }
}

超適当ですが、動かせることを確認するだけなのでこれで十分です。

気になる方は、こちらを参照してください。

lxc - telmate/proxmox - OpenTofu Registry
https://search.opentofu.org/provider/telmate/proxmox/latest/docs/resources/lxc

3. tofu用の認証アカウントを作成

以下のコマンドでTofu用の専用アカウントを作製しましょう。
API Tokenでもいいはずですが、うまくいきませんでした。

pveum role add TerraformProv -privs "Datastore.AllocateSpace Datastore.AllocateTemplate Datastore.Audit Pool.Allocate Sys.Audit Sys.Console Sys.Modify VM.Allocate VM.Audit VM.Clone VM.Config.CDROM VM.Config.Cloudinit VM.Config.CPU VM.Config.Disk VM.Config.HWType VM.Config.Memory VM.Config.Network VM.Config.Options VM.Migrate VM.Monitor VM.PowerMgmt SDN.Use"
pveum user add terraform-prov@pve --password {Password}
pveum aclmod / -user terraform-prov@pve -role TerraformProv

上記でアカウントはできるのですが、Priviledge Separationを外す必要があります。

image.png

今回のはまりポイントはここ

4. Tofuオペレーションを実行

ユーザーを環境変数で宣言

export PM_USER='terraform-prov@pve'
export PM_PASS="{Password}"

Providerをインストール

tofu init

Planを実行

tofu plan

Applyを実行

tofu apply

実行結果

$ tofu apply

OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

OpenTofu will perform the following actions:

  # proxmox_lxc.ubuntu_container will be created
  + resource "proxmox_lxc" "ubuntu_container" {
      + arch         = "amd64"
      + cmode        = "tty"
      + console      = true
      + cores        = 2
      + cpulimit     = 0
      + cpuunits     = 1024
      + hostname     = "ubuntu-22-04-lxc"
      + id           = (known after apply)
      + memory       = 2048
      + onboot       = true
      + ostemplate   = "local:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst"
      + ostype       = (known after apply)
      + password     = (sensitive value)
      + protection   = false
      + start        = false
      + swap         = 512
      + target_node  = "proxmox"
      + tty          = 2
      + unprivileged = true
      + unused       = (known after apply)
      + vmid         = (known after apply)

      + rootfs {
          + size    = "8G"
          + storage = "local-lvm"
          + volume  = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  OpenTofu will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

proxmox_lxc.ubuntu_container: Creating...
proxmox_lxc.ubuntu_container: Still creating... [10s elapsed]
proxmox_lxc.ubuntu_container: Creation complete after 10s [id=proxmmox/lxc/101]

4. まとめ

次は、Startスクリプトにも挑戦してみます。

5. 参考

Proxmox with OpenTofu Kubespray and Kubernetes | From 0.985mhz... to several Ghz
https://blog.andreasm.io/2024/01/15/proxmox-with-opentofu-kubespray-and-kubernetes/

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?