CentOS 安装及使用 terraform 最新教程
?
一. 官網下載安裝包
二. 解壓到 /usr/local/bin目錄
三. 檢查terraform是否安裝成功
四. 測試百度云-對象存儲
? ??4.1 創建目錄
? ??4.2 進入?terraform-test?目錄。
? ??4.3 創建配置文件。
? ??4.4 初始化工作目錄
? ??4.5 Terraform 管理 BOS?
? ??4.6 Terraform 創建 BOS?
? ??4.7 Terraform 刪除?BOS?
?
一. 官網下載安裝包
安裝文檔?hashicorp給出的安裝文檔
文檔中包含下載地址,選擇CentOS 的安裝包點擊下載,格式為.zip
下載地址:https://www.terraform.io/downloads.html
安裝包鏈接:https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
二. 解壓到 /usr/local/bin目錄
下載完成后將 .zip解壓到 /usr/local/bin 目錄下,方便下一步配置PATH變量。解壓后的 terraform是一個 binary 可執行文件,如圖所示
解壓命令:
~$ unzip terraform_0.12.28_linux_amd64.zip -d /usr/local/bin/ ~$ cd /usr/bin ~$ sudo ln -s /usr/local/bin/terraform ~$ source ~/.bash_profile三. 檢查terraform是否安裝成功
直接在terminal中輸入terraform,如果上一步的PATH配置成功,terminal會提示以下信息
四. 測試百度云-對象存儲
? ?因為每個 Terraform 項目都需要創建 1 個獨立的工作目錄,所以先創建一個測試目錄?terraform-test。?
4.1 創建目錄
[root@test bin]#mkdir terraform-test4.2 進入?terraform-test?目錄。
[root@test bin]#cd terraform-test [root@test terraform-test]#4.3 創建配置文件。
Terraform 在運行時,會讀取該目錄空間下所有?*.tf 和?*.tfvars?文件。因此,您可以按照實際用途將配置信息寫入到不同的文件中。下面列出幾個常用的配置文件:
provider.tf -- provider 配置 terraform.tfvars -- 配置 provider 要用到的變量 varable.tf -- 通用變量 resource.tf -- 資源定義 data.tf -- 包文件定義 output.tf -- 輸出例如:創建?provider.tf?文件時,您可按以下格式配置您的身份認證信息:
[root@test terraform-test]# vim main.tf provider "baiducloud" {access_key = "LTA*****************NO2"secret_key = "MOk8x0*************wwff"region = "bj" }4.4 初始化工作目錄
[root@test terraform-test]#terraform initInitializing provider plugins... - Checking for available provider plugins on https://releases.hashicorp.com... - Downloading plugin for provider "alicloud" (1.25.0)...4.5 Terraform 管理 BOS?
terraform plan:預覽功能,允許在正式執行配置文件之前,查看將要執行哪些操作。
例如,您添加了創建 Bucket 的配置文件?test.tf?:
[root@test terraform-test]#vim main.tf provider "baiducloud" {access_key = "LTA*****************NO2"secret_key = "MOk8x0*************wwff"region = "bj" }# 創建源 bucket resource "baiducloud_bos_bucket" "source" {bucket = "charlietang"acl = "public-read-write" }使用?terraform plan?可查看到將會執行的操作。
[root@test terraform-test]# terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.------------------------------------------------------------------------An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:# baiducloud_bos_bucket.tangdd will be created+ resource "baiducloud_bos_bucket" "tangdd" {+ acl = "public-read-write"+ bucket = "charlietang1"+ creation_date = (known after apply)+ force_destroy = false+ id = (known after apply)+ location = (known after apply)+ owner_id = (known after apply)+ owner_name = (known after apply)+ server_side_encryption_rule = (known after apply)+ storage_class = "STANDARD"}Plan: 1 to add, 0 to change, 0 to destroy.------------------------------------------------------------------------Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.4.6 Terraform 創建 BOS?
terraform apply:執行工作目錄中的配置文件。
例如您想創建名為?charlietang 的 Bucket,您需要先添加創建 Bucket 的配置文件?main.tf。
[root@localhost baidu-cloud]# terraform applyAn execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:# baiducloud_bos_bucket.tangdd will be created+ resource "baiducloud_bos_bucket" "tangdd" {+ acl = "public-read-write"+ bucket = "charlietang1"+ creation_date = (known after apply)+ force_destroy = false+ id = (known after apply)+ location = (known after apply)+ owner_id = (known after apply)+ owner_name = (known after apply)+ server_side_encryption_rule = (known after apply)+ storage_class = "STANDARD"}Plan: 1 to add, 0 to change, 0 to destroy.Do you want to perform these actions?Terraform will perform the actions described above.Only 'yes' will be accepted to approve.Enter a value: yesbaiducloud_bos_bucket.tangdd: Creating... baiducloud_bos_bucket.tangdd: Creation complete after 2s [id=charlietang1]Apply complete! Resources: 1 added, 0 changed, 0 destroyed.4.7 Terraform 刪除?BOS?
terraform destroy:可刪除通過 Terraform 創建的空 Bucket。
[root@localhost baidu-cloud]# terraform destroy baiducloud_bos_bucket.tangdd: Refreshing state... [id=charlietang1]An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:- destroyTerraform will perform the following actions:# baiducloud_bos_bucket.tangdd will be destroyed- resource "baiducloud_bos_bucket" "tangdd" {- acl = "public-read-write" -> null- bucket = "charlietang1" -> null- creation_date = "2020-07-08T06:47:52Z" -> null- force_destroy = false -> null- id = "charlietang1" -> null- location = "bj" -> null- owner_id = "3b4c0907265d49daa7fbf103bb2cba4a" -> null- owner_name = "UC:29883996" -> null- server_side_encryption_rule = "none" -> null- storage_class = "STANDARD" -> null}Plan: 0 to add, 0 to change, 1 to destroy.Do you really want to destroy all resources?Terraform will destroy all your managed infrastructure, as shown above.There is no undo. Only 'yes' will be accepted to confirm.Enter a value: yesbaiducloud_bos_bucket.tangdd: Destroying... [id=charlietang1] baiducloud_bos_bucket.tangdd: Destruction complete after 1sDestroy complete! Resources: 1 destroyed.?
總結
以上是生活随笔為你收集整理的CentOS 安装及使用 terraform 最新教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 7.X 升级 Python
- 下一篇: 移动端设备标识码:DeviceID、IM