IBM Cloud Docs
在版本库中使用 Terraform 模板和模块

在版本库中使用 Terraform 模板和模块

Schematics 和 Terraform 支持从各种资源库类型下载 Terraform 模板和模块:Terraform Registry、GitHub, GitLab, S3/COS buckets、IBM Catalog、Artifactory 等。 请参阅 Terraform 文档中的 模块源

使用 Schematics时,在执行 Terraform 计划或 Apply 操作之前下载 Terraform 模板和模块是一个两步过程。 在工作空间创建时,Schematics 仅克隆包含模板的存储库以及子文件夹中的任何嵌入式模块。 在工作空间创建时,不会下载使用模块 source 参数引用的任何模块。 用于访问专用存储库中的模板/配置的凭证必须在工作空间创建时传递到 Schematics。

在计划或应用操作的 terraform init 阶段,将下载使用 source 参数引用的模块。 terraform init 命令解析模板文件并从 source 字段引用的存储库下载任何模块。 驻留在专用存储库中的模块需要将其他凭证传递到 Terraform。 这些凭证单独定义并传递给 Schematics使用的凭证。

要从专用 Git 存储库,IBM Cloud 目录或任何其他存储库下载模块,Terraform 支持使用 netrc 配置来传递任何必需的访问标识和令牌。

支持的 Git 存储库
存储库
模板
公用存储库
模板
私人资料库
模块
公共存储库

私人资料库模块
注释
GitHub Git 令牌-1 Git 令牌-2
GitLab Git 令牌-1 Git 令牌-2
IBM GitLab Git 令牌-1 Git 令牌-2
Terraform.io 不适用
  1. Git 令牌在工作区创建时定义。
  2. Git 令牌定义为 netrc

使用 Schematics时,可以使用 __netrc__ 环境变量向传递凭证配置对模块凭证的 netrc 支持。 __netrc__ 环境变量接受 hostnameusernamepassword 自变量的列表。 仅支持使用 Schematics 命令行APIs 来设置环境变量。 语法是使用 JSON 有效内容文件中的 env_values 参数提供的。

__netrc__ 期望 hostnameusernamepassword 参数的顺序与语法中列出的顺序相同。

具有 __netrc__ 列表的 env_values 语法:

"env_values":[
            {
               "__netrc__":"[['example.com', 'user1', 'pass1']['example1.com', 'user2' , 'pass2']]"
            }
         ]

将专用模块与模板配合使用

Schematics 在内部根据 JSON 文件中配置的 env_values 创建 .netrc 文件。 以下是一个语法和样本 testexample.json 示例文件,用于克隆所有文件以通过命令行和 API 创建和应用 Schematics 工作空间。

带有描述的语法:

{
  "name": "<workspace_name>",
  "shared_data": {
    "region": "<region_name>"
  },
  "type": [
    "<terraform_version>"
  ],
  "description": "<description of the workspace>",
  "template_repo": {
    "url": "<your Git repository with the module>"
  },
  "template_data": [
    {
      "folder": ".",
      "type": "<terraform_version>",
      "env_values": [
        {
          "__netrc__":"[['<git repository>','<git username>','<git_password>']]"
        }
      ]
    }
  ]
}

具有 netrc 有效内容的示例 testexample.json

{
  "name": "testnetrcworkspaceexample",
  "shared_data": {
    "region": "us-south"
  },
  "type": [
    "terraform_v1.4"
  ],
  "description": "terraform workspace",
  "template_repo": {
    "url": "https://github.com/xxxx/test-template-private-module"
  },
  "template_data": [
    {
      "folder": ".",
      "type": "terraform_v1.4",
      "env_values": [
        {
          "__netrc__":"[['github.com','testuser','ghp_x0000000xxxxxxxx000000efZxxxxxxxV']]"
        }
      ]
    }
  ]
}

创建工作空间的示例

ibmcloud schematics workspace new --file testexample.json

运行 ibmcloud schematics workspace get --id WORKSPACE_ID 命令以分析成功创建工作空间,或者使用 用户界面 来查看模块中的所有文件,并在工作空间中使用这些文件进行供应。