使用儲存庫中的 Terraform 範本和模組
Schematics和 Terraform 支援從各種儲存庫類型下載 Terraform 範本和模組:Terraform Registry、GitHub, GitLab, S3/COS儲存桶、IBM Catalog、Artifactory等。 請參閱 Terraform 文件中的 模組來源。
使用 Schematics時,在執行「Terraform 計劃」或「套用」作業之前下載 Terraform 範本和模組是兩個步驟的程序。 在工作區建立時,Schematics 只會複製包含您的範本及子資料夾中任何內嵌模組的儲存庫。 在建立工作區時,不會下載任何使用模組 source 參數所參照的模組。 用於存取專用儲存庫中範本/配置的認證必須在工作區建立時傳遞至 Schematics。
在計劃或套用作業的 terraform init 階段期間,會下載使用 source 參數所參照的模組。 terraform init 指令會剖析範本檔案,並從 source 欄位所參照的儲存庫下載任何模組。 位於專用儲存庫中的模組需要將其他認證傳遞至 Terraform。 這些認證會個別定義並傳遞給 Schematics所使用的認證。
若要從專用 Git 儲存庫、IBM Cloud 型錄或任何其他儲存庫下載模組,Terraform 支援使用 netrc 配置來傳遞任何必要的存取 ID 和記號。
| 儲存庫 |
範本 公用儲存庫 |
範本 私人儲存庫 |
模組 公共儲存庫 |
模組 私人儲存庫 |
註解 |
|---|---|---|---|---|---|
| GitHub | 是 | Git 記號-1 | 是 | Git 記號-2 | |
| GitLab | 是 | Git 記號-1 | 是 | Git 記號-2 | |
| IBM GitLab | 是 | Git 記號-1 | 是 | Git 記號-2 | |
| Terraform.io | 否 | 否 | 是 | NA |
- 工作區建立時定義的 Git 代碼。
- 使用
netrc定義的 Git 令牌。
使用 Schematics時,可以使用傳遞認證的 __netrc__ 環境變數來配置模組認證的 netrc 支援。 __netrc__ 環境變數接受 hostname、username 及 password 引數的清單。 僅支援使用 Schematics 指令行 和 APIs 來設定環境變數。 在 JSON 有效負載檔案中使用 env_values 參數提供語法。
__netrc__ 預期 hostname、username 及 password 引數的順序與語法中列出的順序相同。
具有 __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 指令以分析成功的工作區建立,或使用 使用者介面 來檢視模組中的所有檔案,並在工作區中使用這些檔案進行供應。