Terraform | 基于Azure上使用模块(四)


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 } ```


少年,没看够?点击石头的主页,随便点点看看,说不定有惊喜呢?欢迎支持点赞/关注/评论,有你们的支持是我更文最大的动力,多谢啦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值