๐ป Lab - Provisioners, Variables, Outputs
๐ป Lab - Provisioners, Variables, Outputs
ํธ์ง๊ธฐ์์ ์ด๊ธฐ

- VSCode๋ฅผ ์คํํ๊ณ File(ํ์ผ) ๋ฉ๋ด์์
Open Folder...
๋ฅผ ํด๋ฆญํฉ๋๋ค. - ์์ ์ค์ต์ ์งํํ
lab02
์ ์ด์ด์ค๋๋ค.
๐ ๏ธ Use a Provisioner
๐ป Provisioner๋ฅผ ์์ ํฉ๋๋ค.
main.tf
ํ์ผ์ ์ด์ด remote-exec
ํญ๋ชฉ์ด ์๋ ๊ณณ์ผ๋ก ์ด๋ํฉ๋๋ค.
inline
ํญ๋ชฉ์ ๋ค์์ ๋์ค ์ถ๊ฐํฉ๋๋ค.
"sudo apt -y install cowsay",
"cowsay Mooooooooooo!",
ํ
terraform fmt
๋ช
๋ น์ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ๋ฉ์ง๊ฒ ์ ๋ ฌ ํ ์ ์๋ ์ข์ ์๊ฐ ์
๋๋ค.
์ด์ ๋ณ๊ฒฝ ์ฌํญ์ ์ ์ฉํ์ญ์์ค.
terraform apply -auto-approve
๋ก๊ทธ ์ถ๋ ฅ์ ๋ค๋ก ์คํฌ๋กคํฉ๋๋ค. "Moooooooo!"๋ผ๊ณ ๋งํ๋ ASCII ์ํธ ์์๊ฐ ๋ณด์ผ ๊ฒ์ ๋๋ค.
Result example

๐จ๏ธ Add an Output
๐ป Output์ ์์ ํฉ๋๋ค.
output.tf
ํ์ผ์ ์ด์ด ์๋ ํญ๋ชฉ์ ์ถ๊ฐํฉ๋๋ค.
output "ssh_info" {
value = nonsensitive("sshpass -p '${data.ncloud_root_password.hashicat.root_password}' ssh root@${ncloud_public_ip.hashicat.public_ip} -oStrictHostKeyChecking=no")
}
ํด๋น output์ ์ด๋ฆ์ ssh_info
์
๋๋ค.
์ด๋ค ์ ํ์ ์ถ๋ ฅ์ด ์ ํจํ์ง ๋ณด๋ ค๋ฉด ๋ฌธ์ ํ์ด์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
- ncloud_root_password : https://registry.terraform.io/providers/NaverCloudPlatform/ncloud/latest/docs/data-sources/root_password#argument-reference
- ncloud_public_ip :https://registry.terraform.io/providers/NaverCloudPlatform/ncloud/latest/docs/resources/public_ip#attributes-reference
output.tf
์ ์๋ก์ด ๋ด์ฉ์ ์ ์ฅํ๊ณ terraform refresh
๋ช
๋ น์ ์คํํ์ฌ ์๋ก์ด ์ถ๋ ฅ์ ํ์ธํฉ๋๋ค.
terraform refresh
terraform output
๋ช
๋ น์ ์คํํ์ฌ ๋ชจ๋ ์ถ๋ ฅ์ ๋ณผ ์๋ ์์ต๋๋ค.
terraform output
๐ถ Fun With Variables
๐ป ๋ณ์๋ฅผ ์กฐ์ ํฉ๋๋ค.
Terraform ๋ณ์๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ต๋๋ค. ์ง๊ธ๊น์ง terraform.tfvars
ํ์ผ์ ์ฌ์ฉํ์ฌ ๋ณ์๋ฅผ ์ค์ ํ์ต๋๋ค.
๋ช
๋ น์ค์์ ๊ธฐ๋ณธ๊ฐ๊ณผ ๋ค๋ฅธ height
, width
๋ณ์๋ฅผ ์ฌ์ฉ ํ์ฌ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ค์ ๋ฐฐํฌํด ๋ณด์ญ์์ค.
๋ณ๊ฒฝ ์ฌํญ์ ๊ด์ฐฐํ๊ธฐ ์ํด ์ ์ฉํ ๋๋ง๋ค ์น ์ฑ์ ๋ค์ ๋ก๋ํฉ๋๋ค.
terraform apply -auto-approve -var height=600 -var width=800
๋ค์์ผ๋ก Terraform์ด ์ฝ์ ์ ์๋ ํ๊ฒฝ ๋ณ์๋ฅผ ์ค์ ํด ๋ณด์ญ์์ค. ๋ค์ ๋ช
๋ น์ ์คํํ์ฌ placeholder
๋ณ์๋ฅผ ์ค์ ํฉ๋๋ค.
export TF_VAR_placeholder=placedog.net
ํ๊ฒฝ ๋ณ์ ์ ์ฉ ํ terraform apply -auto-approve
๋ฅผ ์คํํ์ฌ ๋ค์ ์ ์ฉํด ๋ด
๋๋ค.
terraform apply -auto-approve
์ด์ ๋ช ๋ น์ค์์ ๋์ผํ ๋ณ์๋ฅผ ๋ค๋ฅด๊ฒ ์ค์ ํ์ฌ ๋ค์ ์๋ํ์ญ์์ค.
terraform apply -auto-approve -var placeholder=placebear.com
์ด๋ค ๋ณ์๊ฐ ์ฐ์ ์ ๋์์ต๋๊น? ์ ์ดํด ๋์ จ๋์?
๋ค์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ ์ ์์ต๋๋ค.
๐ฑ Quiz Time 4. Terraform Variables
Q. *.tfvars
ํ์ผ๊ณผ ํ๊ฒฝ ๋ณ์์ ๋์ผํ ๋ณ์๊ฐ ์ค์ ๋์ด ์์ต๋๋ค. ์ด๋ ๊ฒ์ด ์ฐ์ ํฉ๋๊น?
๋ต
์ด ์ฅ์์ ์ฐ๋ฆฌ๋ :
- Terraform Provisioners์ ๋ํด ๋ฐฐ์ ์ต๋๋ค.
file
๊ณผremote-exec
ํ๋ก๋น์ ๋ ๋๊ตฌ์ ๋ํด ์์๋ณด์์ต๋๋ค.- ์๋ก์ด ํ๋ก๋น์ ๋ ๋จ๊ณ๋ก ์น์๋ฒ ์ฌ๊ตฌ์ฑ ํด๋ณด์์ต๋๋ค.