《Terraform 101 从入门到实际》这本小册在南瓜慢说官方网站和GitHub两个中央同步更新,书中的示例代码也是放在GitHub上,不便大家参考查看。

不怕出身低,行行出状元。

插件

Terraform能够对多种平台的多种资源进行治理,这个是通过插件来实现的。

这里的插件,在Terraform的世界也叫Providers,也是一个个可执行文件。不同的插件实现不同的性能,对接AWS,就要应用AWS的插件;对接GCP,就要用GCP的插件。

当咱们通过terraform init初始化一个我的项目时,Terraform就会依据配置帮咱们下载插件。在咱们执行apply的时候,就会调用这些插件实现对应的资源管理。

咱们能够到官网仓库( https://registry.terraform.io... )去搜有什么插件可用,这里有极其丰富的插件,也有具体的应用阐明:

接下来,咱们就插件探讨几个问题:

  • 怎么指定下载哪些插件和版本号?
  • 从哪里下载?
  • 下载到什么中央?
  • 没有对插件库有拜访权限的环境下怎么解决?
  • 是否每个我的项目都要下载雷同的插件?

指定下载哪些插件和版本

Terraform是通过解析required_providers晓得须要哪些插件,个别习惯是定义一个verion.tf文件,把相干配置都放在这个文件里,比方:

terraform {  required_version = "= v1.0.11"  required_providers {    local = {      source  = "hashicorp/local"      version = "= 2.1.0"    }    random = {      source  = "hashicorp/random"      version = "3.1.0"    }  }}

这个文件定义了Terraform外围组件的版本,还定义了local和random插件及其版本号。下面指定Terraform版本为1.0.11,local版本为2.1.0,random版本为3.1.0。

咱们看这里的版本号有两个等于=号,会不会感觉奇怪?其实这是HCL语言的一个个性,除了=号,还能够是><=等,这样能够指定版本范畴,而不只是某个特定版本。

从哪里下载

能够通过命令terraform providers查看以后我的项目配置的插件是从哪里下载的。如下:

$ terraform providersProviders required by configuration:.├── provider[registry.terraform.io/hashicorp/random] 3.1.0└── provider[registry.terraform.io/hashicorp/local] 2.1.0

默认是从官网的公共仓库registry.terraform.io下载的。

如果须要指定其它仓库,代码如下:

terraform {  required_version = "= v1.0.11"  required_providers {    local = {      source  = "hashicorp/local"      version = "= 2.1.0"    }    random = {      source  = "hashicorp/random"      version = "3.1.0"    }    pkslowcloud = {      source  = "registry.pkslow.com/examplecorp/pkslowcloud"      version = "0.1.0"    }  }}

这里pkslowcloud就是应用自定义的仓库地址,执行providers命令如下:

$ terraform providersProviders required by configuration:.├── provider[registry.terraform.io/hashicorp/local] 2.1.0├── provider[registry.terraform.io/hashicorp/random] 3.1.0└── provider[registry.pkslow.com/examplecorp/pkslowcloud] 0.1.0

留神:pkslowcloud理论不存在,大家不用尝试下载应用。

下载到什么中央

执行terraform init进行初始化,就会下载插件:

$ terraform initInitializing the backend...Initializing provider plugins...- Finding hashicorp/random versions matching "3.1.0"...- Finding hashicorp/local versions matching "2.1.0"...- Installing hashicorp/random v3.1.0...- Installed hashicorp/random v3.1.0 (signed by HashiCorp)- Installing hashicorp/local v2.1.0...- Installed hashicorp/local v2.1.0 (signed by HashiCorp)

执行完init命令后,当前工作目录就会有一个.terraform文件夹,这里就放了插件的程序。目录构造如下:

$ tree -a.├── .terraform│   └── providers│       └── registry.terraform.io│           └── hashicorp│               ├── local│               │   └── 2.1.0│               │       └── darwin_amd64│               │           └── terraform-provider-local_v2.1.0_x5│               └── random│                   └── 3.1.0│                       └── darwin_amd64│                           └── terraform-provider-random_v3.1.0_x5

没有网络环境怎么办

在有些状况下,并不能间接拜访Terraform的公共仓库去下载插件,如果能够从其它中央复制一份插件,并能够应用,那岂不是美哉?Terraform曾经思考了这种需要。

首先它反对有网络环境的机器把当前目录的插件复制到特定目录,命令如下:

$ terraform providers mirror /Users/larry/Software/terraform/plugins- Mirroring hashicorp/local...  - Selected v2.1.0 to meet constraints 2.1.0  - Downloading package for darwin_amd64...  - Package authenticated: signed by HashiCorp- Mirroring hashicorp/random...  - Selected v3.1.0 to meet constraints 3.1.0  - Downloading package for darwin_amd64...  - Package authenticated: signed by HashiCorp

查看一下目录构造,Terraform会打包好插件为zip文件:

$ tree -a /Users/larry/Software/terraform/plugins/Users/larry/Software/terraform/plugins-localdisk└── registry.terraform.io    └── hashicorp        ├── local        │   ├── 2.1.0.json        │   ├── index.json        │   └── terraform-provider-local_2.1.0_darwin_amd64.zip        └── random            ├── 3.1.0.json            ├── index.json            └── terraform-provider-random_3.1.0_darwin_amd64.zip

下次咱们能够指定插件目录实现复用:

$ terraform init -plugin-dir=/Users/larry/Software/terraform/pluginsInitializing the backend...Initializing provider plugins...- Reusing previous version of hashicorp/random from the dependency lock file- Reusing previous version of hashicorp/local from the dependency lock file- Using previously-installed hashicorp/random v3.1.0- Using previously-installed hashicorp/local v2.1.0

看日志能够看到,Terraform不再下载,而是重用插件。

执行完命令init后,再查看terraform version,则会显示插件的版本:

$ terraform versionTerraform v1.0.11on darwin_amd64+ provider registry.terraform.io/hashicorp/local v2.1.0+ provider registry.terraform.io/hashicorp/random v3.1.0

Terraform对于这种插件目录重用的反对,不只是zip包,二进制也是反对的,但对应的目录后果有点不一样。这里不开展介绍了。