Thuta Learning
ရှာဖွေရန်
AdvancedDevOpsintermediate

Provisioners (local-exec, remote-exec)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Provisioners (local-exec, remote-exec) ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် terraform command/HCL code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Resource create ပြီးတဲ့နောက် command တစ်ခု run ချင်ရင် (ဥပမာ - server create ပြီးရင် setup script run ချင်ရင်) `provisioner "local-exec"` (Terraform ကို run နေတဲ့ machine ပေါ်မှာ command run) ဒါမှမဟုတ် `provisioner "remote-exec"` (create လိုက်တဲ့ resource ပေါ်ကနေ SSH/WinRM ချိတ်ပြီး command run) ကို သုံးလို့ရပါတယ်။ ဒါပေမယ့် provisioner ရဲ့ execution ကို Terraform က state ထဲမှာ track မလုပ်ပါဘူး — provisioner fail ဖြစ်ရင် resource ကတော့ create ပြီးသား, ဒါပေမယ့် setup မပြီးဘူး ဆိုတဲ့ 'ကြားနေ' အခြေအနေ ဖြစ်နိုင်ပါတယ်။ HashiCorp ကိုယ်တိုင်က provisioner ကို 'last resort' လို့ ခေါ်ပြီး, Ansible/cloud-init/dedicated configuration management tool ကို ပိုတိုက်တွန်းပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`resource "aws_instance" "web" { ... provisioner "remote-exec" { inline = ["sudo apt update", "sudo apt install -y nginx"] } }` လို့ ရေးထားရင် server create ပြီးတဲ့နောက် Nginx install script ကို auto run ပေးပါတယ် — ဒါပေမယ့် production မှာ ဒီလို 'server setup' logic ကို Ansible ဒါမှမဟုတ် cloud-init (server boot time script) ကို ခွဲထုတ်ပြီး, Terraform ကို infrastructure creation တစ်ခုတည်းအတွက်ပဲ အာရုံစိုက်ခိုင်းတာက ပိုကောင်းပါတယ်.

အတူတူ ကြည့်မယ်

hcl
resource "aws_instance" "web" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t3.micro"

  provisioner "remote-exec" {
    inline = [
      "sudo apt update",
      "sudo apt install -y nginx",
    ]

    connection {
      type = "ssh"
      user = "ubuntu"
      host = self.public_ip
    }
  }

  provisioner "local-exec" {
    command = "echo ${self.public_ip} >> inventory.txt"
  }
}
You should see
$ terraform apply
aws_instance.web: Creating...
aws_instance.web: Provisioning with 'remote-exec'...
aws_instance.web (remote-exec): Connecting to remote host via SSH...
aws_instance.web: Creation complete after 45s

၅ မိနစ် စမ်းကြည့်

`local-exec` provisioner (SSH connection မလိုအပ်တာမို့ practice လွယ်ပါတယ်) တစ်ခုကို `local_file` resource (Basic chapter) ပေါ်မှာ ထည့်ကြည့်ပါ — resource create ပြီးတဲ့အခါ command run သွားတာကို confirm လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

Provisioner ကို 'ပထမဆုံး ရွေးစရာ' အနေနဲ့ မသုံးပါနှင့် — cloud-init (AWS `user_data` argument လိုမျိုး) ဒါမှမဟုတ် dedicated configuration management tool (Ansible) ကို အရင်ဆုံး စဉ်းစားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Provisioner fail ဖြစ်ရင် resource ကို Terraform က 'tainted' အဖြစ် mark လုပ်ပြီး နောက်တစ်ကြိမ် apply မှာ destroy+recreate ဖြစ်နိုင်ကြောင်း မသိဘဲ production မှာ provisioner ကို relied on လုပ်မိခြင်း
  • Server setup logic (software install, config file write) အားလုံးကို provisioner ထဲမှာပဲ ရေးထည့်ခြင်း — Ansible/cloud-init ကို ခွဲမသုံးဘဲ Terraform config ကို ရှုပ်ထွေးအောင် ဖြစ်စေပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`local-exec` provisioner (SSH connection မလိုအပ်တာမို့ practice လွယ်ပါတယ်) တစ်ခုကို `local_file` resource (Basic chapter) ပေါ်မှာ ထည့်ကြည့်ပါ — resource create ပြီးတဲ့အခါ command run သွားတာကို confirm လုပ်ကြည့်ပါ။

You'll know it worked when: $ terraform apply aws_instance.web: Creating... aws_instance.web: Provisioning with 'remote-exec'... aws_instance.web (remote-exec): Connecting to remote host via SSH... aws_instance.web: Creation complete after 45s

Provisioners (local-exec, remote-exec) | Thuta Learning