theme: condensed-night-purple
这是我参与11月更文挑战的第4天,活动详情查看:2021最后一次更文挑战
六、使用Git管理模块
如果我们计划在多个环境中共享这个模块,最好的做法是将该模块放在源代码控制存储库中,那么我们就可以从源代码控制中获得所有的好处,比如更改跟踪。
此外,我们还得到了版本标签。标记模块是一种最佳实践,因为它允许我们将模块的稳定工作版本“固定”到 Terraform 配置中。这样可以防止任何中断更改影响已经在生产中的配置。
要从 git 仓库中使用 Terraform 模块,请将 source 参数更改为 git URL。在我们的示例中,我已经将我们的存储帐户模块上传到 Azure DevOps 回购。这是一个公共 git repo,不需要任何身份验证配置。我们可以使用 https URL,并在前缀 git: : :。
```
Create Storage Account
module "storageaccount" { source = "git::https://allanore@dev.azure.com/allanore/TerraformModulesExample/git/TerraformModulesExample?ref=v0.1"
saname = "tfdemosa23432" rgname = azurermresourcegroup.rg.name location = azurermresourcegroup.rg.location } ```
如果我们运行 terraform init,我们可以在控制台输出中看到该模块从 git repo 下载并保存到。Terraform/modules 本地目录:
``` .. Please install a compatible extension version or remove it. Initializing modules... Downloading git::https://allanore@dev.azure.com/allanore/TerraformModulesExample/git/TerraformModulesExample?ref=v0.1 for storageaccount... - storageaccount in .terraform/modules/storageaccount Downloading git::https://allanore@dev.azure.com/allanore/TerraformModulesExample/git/TerraformModulesExample?ref=v0.1 for storageaccount2... - storageaccount2 in .terraform/modules/storageaccount2
Initializing the backend... ... ```
另外,如果我们想使用带 SSH 的私有 Azure Repo,我们可以通过如下所示的 SSH URL 在 source 参数中引用我们的模块。我们还需要生成并安装用于认证的 SSH 证书。
git::git@ssh.dev.azure.com:v3/allanore/TerraformModulesExample/TerraformModulesExample?ref=v0.1
要使用来自 GitHub repo 的 Terraform 模块源代码,请使用到 GitHub 项目的 URL。在下面的例子中,我上传了我们的模块到 Github repo:
```
Create Storage Account
module "storage_account" { source = "github.com/**/TerraformModulesExample"
saname = "tfdemosa23432" rgname = azurermresourcegroup.rg.name location = azurermresourcegroup.rg.location } ```
少年,没看够?点击石头的主页,随便点点看看,说不定有惊喜呢?欢迎支持点赞/关注/评论,有你们的支持是我更文最大的动力,多谢啦!