DEV Community

Cover image for Offline Terraform Provider
Damrongsak Reetanon
Damrongsak Reetanon

Posted on

Offline Terraform Provider

พอดีว่าระบบที่บ้านเป็น Open-Source Virtualization Solution ที่ชื่อว่า oVirt ซึ่งก็มีความสุขดี ใช้สร้างโน่นนี่นั่นเพื่อทดสอบอะไรไปเรื่อง ... เพียงแต่ว่า ขี้เกียจไม่อยากมานั่งสร้าง VM ที่ละตัว ช่วงแรก ๆ ใช้ Ansible ก็ต้องเขียน playbook สำหรับสร้าง พอจะเปลี่ยงแปลงระบบที่ต้องการทดสอบ ก็ต้องมานั่งแก้ playbook จะลบทิ้งตอนไม่ใช้แล้ว ก็ต้องเขียนอีก playbook อีกชุดเพื่อทำหน้าที่ลบ VM ทั้งหมด ... ซึ่งเป็นจุดอ่อนของการใช้งาน Ansible เพื่อ provisioning แต่พอได้รู้จัก Terraform บอกเลยว่า ตอนโจทย์ในช่วงการ provisioning มากเลย
ปัญหาก็มาอยู่ว่าพอใช้ infrastructure ตัวที่ไม่ค่อยมีใครใช้ ปัญหาเรื่อง Terraform Provider ก็ตามมาทันที ปัญหาแรก ตอน Terraform จาก version 0.12 เปลี่ยนไปเป็น 0.13 แต่ oVirt plugin ก็ไม่พัฒนาตาม จึงใช้ไม่ได้ใน Terraform v 0.13 ต้องเปลี่ยน Terraform version ไป ๆ มา ๆ เวลาทำงาน อยู่บ้านต้องใช้ v0.12 พอไปที่ทำงาน ก็เปลี่ยนเป็น version อื่น เพื่อให้ใช้ plugin รุ่นใหม่ ๆ ได้ ... และแล้ว ผู้พัฒนา oVirt plugin ก็พัฒนาก็พัฒนาให้รองรับ Terraform รุ่นใหม่ ๆ ก็ดูเหมือนจะดี มีความหวัง แต่ก็ไปไม่สุด ไม่ได้เอาไปใส่ไว้ใน Terraform Registry ให้
.... แล้วต้องทำอย่างไรดีล่ะ

  1. ตัวอย่างนี้ เป็นตัวอย่างที่ทำงานบนระบบปฏิบัติการ macOS และ ยังเป็น Macbook Pro รุ่นที่ยังใช้ chip Intel X86_64 อยู่ ดังนั้น การเลือกใช้งาน อาจมีความแตกต่างกันบ้างตามระบบที่ต่างกัน
  2. ลำดับแรก download ตัว Terraform Provider ที่ต้องการมาเก็บไว้ใน directory ที่วางแผนไว้ก่อน
d8k-bpro > ~ curl -OL https://github.com/oVirt/terraform-provider-ovirt/releases/download/v0.99.0/terraform-provider-ovirt_0.99.0_darwin_amd64.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 11.1M  100 11.1M    0     0  1824k      0  0:00:06  0:00:06 --:--:-- 2412k
d8k-bpro > ~ unzip terraform-provider-ovirt_0.99.0_darwin_amd64.zip
Archive:  terraform-provider-ovirt_0.99.0_darwin_amd64.zip
  inflating: CHANGELOG.md
  inflating: LICENSE
  inflating: README.md
  inflating: terraform-provider-ovirt_0.99.0
d8k-bpro > ~ mkdir -p ~/.terraform.d/providers/local/ovirt/ovirt/0.99/darwin_amd64
d8k-bpro > ~ mv terraform-provider-ovirt_0.99.0 ~/.terraform.d/providers/local/ovirt/ovirt/0.99/darwin_amd64
Enter fullscreen mode Exit fullscreen mode
  1. สร้าง ~/.terraformrc
d8k-bpro > ~ vi ~/.terraformrc
provider_installation {
  filesystem_mirror {
    path    = "/Users/drs/.terraform.d/providers"
  }
  direct {
    exclude = ["local/*/*"]
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. วิธีการเรียกใช้งาน Terraform Plugins จาก Terraform Configuration File
d8k-bpro > project1 vi main.tf

terraform {
  required_providers {
    ovirt = {
      source  = "local/ovirt/ovirt"
      version = "0.99.0"
    }
  }
}


provider "ovirt" {
   username = "admin@internal"
   url = "https://ovirt.example.com/ovirt-engine/api"
   password = var.ovirt_password
}
Enter fullscreen mode Exit fullscreen mode
  1. Initialization Working Directory
d8k-bpro > project1 terraform init

Initializing the backend...

Initializing provider plugins...
- Finding local/ovirt/ovirt versions matching "0.99.0"...
- Finding latest version of hashicorp/null...
- Installing local/ovirt/ovirt v0.99.0...
- Installed local/ovirt/ovirt v0.99.0 (unauthenticated)
- Installing hashicorp/null v3.2.1...
- Installed hashicorp/null v3.2.1 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

╷
│ Warning: Incomplete lock file information for providers
│
│ Due to your customized provider installation methods, Terraform was forced to calculate
│ lock file checksums locally for the following providers:
│   - local/ovirt/ovirt
│
│ The current .terraform.lock.hcl file only includes checksums for darwin_amd64, so
│ Terraform running on another platform will fail to install these providers.
│
│ To calculate additional checksums for another platform, run:
│   terraform providers lock -platform=linux_amd64
│ (where linux_amd64 is the platform to generate)
╵

Terraform has been successfully initialized!
Enter fullscreen mode Exit fullscreen mode

หมายเหตุ: อาจจะมีข้อความเตือนเรื่องการตรวจสอบความถูกต้องของ Provider ที่ไม่สามารถทำได้อยู่ แต่ไม่ได้มีปัญหาในการใช้งาน

สุดท้าย ในปี 2022 oVirt ก็มี Terraform Providers รุ่นใหม่อยู่ใน Terraform Registry เรียบร้อยแล้ว แต่พบว่า ความสามารถของ provider ตัวใหม่ยังมีความสามารถน้อยกว่าในรุ่นเก่ามาก ผู้ใช้จึงยังเลือกใน Terraform Provider ตัวเก่าอยู่

มูลค่าความสุข

Top comments (0)