0

当我使用 terraform在Outputs.tf中创建“local_file”资源时:

### The hosts file
resource "local_file" "AnsibleHosts" {
 content = templatefile("${path.module}/hosts.tmpl",
   {
     vm-names                   = [for k, p in azurerm_virtual_machine.vm: p.name],
     private-ip                 = [for k, p in azurerm_network_interface.nic: p.private_ip_address],
     publicvm-names             = [for k, p in azurerm_virtual_machine.publicvm: p.name],
     publicvm-private-ip        = [for k, p in azurerm_network_interface.publicnic: p.private_ip_address],
     public-ip                  = [for k, p in azurerm_public_ip.publicip: p.ip_address],
     public-dns                 = [for k, p in azurerm_public_ip.publicip: p.fqdn],
     }
 )
 filename = "hosts.j2"
}

如果我直接通过 VS Code 运行它,我会看到创建的 hosts.j2 文件。

当我使用 Azure DevOps 管道部署它时,Plan and Apply 阶段显示文件已创建。

在此处输入图像描述

当我检查我的 DevOps 存储库时,文件不存在。

我假设(我可能是错的)这是因为文件是在构建代理上创建的。有谁知道我如何将文件创建/复制回 Azure DevOps Repo。

4

1 回答 1

1

你说的对。这些文件是在构建代理上创建的。这就是您在 Devops 存储库中看不到它们的原因。您需要将更改提交回管道中的 Azure devops 存储库。

您可以git commands在管道中运行脚本任务以将更改推送到 devops 存储库。

如果您使用的是 yaml 管道。您可以查看以下脚本:

steps:
- checkout: self
  persistCredentials: true  #Allow scripts to access the system token

- powershell: |
       
      git config --global user.email "you@example.com"
      git config --global user.name "username"

      git add .
      git commit -m "add hosts.j2"
      git push origin HEAD:$(Build.SourceBranchName) 

注意:允许脚本通过添加设置为的部分来访问系统令牌checkoutpersistCredentialstrue

如果您使用的是经典管道。您可以查看以下脚本:

首先,您需要通过启用以下选项来允许脚本访问系统令牌:

在管道编辑页面-->代理作业-->附加选项

在此处输入图像描述

您可以在脚本任务中添加以下内联脚本:

git config --global user.email "you@example.com"
git config --global user.name "username"

git add .
git commit -m "add hosts.j2"
git push https://$(System.AccessToken)@dev.azure.com/yourOrg/yourProj/_git/repoName HEAD:$(Build.SourceBranchName) 

如果您在运行上述脚本以推送到 azure repo 时遇到权限问题。您需要进入项目设置下的存储库。单击Git Repositories,在安全页面中,单击加号(+)并搜索组并单击添加,在访问控制摘要页面中,授予贡献和读取权限{your project name} build service({your org name})

请查看此线程

于 2021-03-22T03:52:04.390 回答