2

《Terraform 101 从入门到实践》 第二章 Providers插件管理 - 南瓜慢说

 1 year ago
source link: https://www.cnblogs.com/larrydpk/p/17106783.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

随笔 - 144, 文章 - 0, 评论 - 43, 阅读 -

21万

《Terraform 101 从入门到实践》 第二章 Providers插件管理

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


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

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

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

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

我们可以到官方仓库( https://registry.terraform.io/browse/providers )去搜有什么插件可用,这里有极其丰富的插件,也有详细的使用说明:

946674-20230209192309795-1850664218.png

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

  • 怎么指定下载哪些插件和版本号?
  • 从哪里下载?
  • 下载到什么地方?
  • 没有对插件库有访问权限的环境下怎么处理?
  • 是否每个项目都要下载相同的插件?

指定下载哪些插件和版本

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 providers

Providers 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 providers

Providers 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 init

Initializing 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/plugins

Initializing 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 version
Terraform v1.0.11
on darwin_amd64
+ provider registry.terraform.io/hashicorp/local v2.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK