简体   繁体   English

使用 Azure Devops 管道运行代码时缺少 Terraform local_file

[英]Terraform local_file missing when running code using Azure Devops pipeline

When I create a "local_file" resource in the Outputs.tf using terraform:当我使用 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"
}

If i run this directly via VS Code, I see the hosts.j2 file created.如果我直接通过 VS Code 运行它,我会看到创建的 hosts.j2 文件。

When I deploy this using Azure DevOps pipelines, the Plan and Apply stage show that the files are created.当我使用 Azure DevOps 管道部署它时,Plan and Apply 阶段显示文件已创建。

在此处输入图像描述

When I check my DevOps repository, the files are not there.当我检查我的 DevOps 存储库时,文件不存在。

I am assuming (I can be wrong) this is because the files are created on the build agent.我假设(我可能是错的)这是因为文件是在构建代理上创建的。 Does anyone know how I can get the file created/copied back to the Azure DevOps Repo.有谁知道我如何将文件创建/复制回 Azure DevOps Repo。

You are right.你说的对。 The files are created on the build agent.这些文件是在构建代理上创建的。 That is the reason you cannot see them in your Devops Repository.这就是您在 Devops 存储库中看不到它们的原因。 You need to commit the changes back to the Azure devops repo in your pipeline.您需要将更改提交回管道中的 Azure devops 存储库。

You can run the git commands in a script task in the pipeline to push the changes to devops repo.您可以在管道中的脚本任务中运行git commands ,以将更改推送到 devops 存储库。

If you are using yaml pipeline.如果您使用的是 yaml 管道。 You can check out below script:您可以查看以下脚本:

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) 

Note: Allow scripts to access the system token by adding a checkout section with persistCredentials set to true注意:允许脚本通过添加将persistCredentials设置为truecheckout部分来访问系统令牌

If you are using Classic pipeline.如果您使用的是经典管道。 You can check out below scripts:您可以查看以下脚本:

First, you need to allow scripts to access the system token by enabling the below option:首先,您需要通过启用以下选项来允许脚本访问系统令牌:

In pipeline edit page-->Agent job-->Additional options在管道编辑页面-->代理作业-->附加选项

在此处输入图像描述

You can add below inline scripts in the script task:您可以在脚本任务中添加以下内联脚本:

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) 

If you encounter permission issue when running above scripts to push to azure repo.如果您在运行上述脚本以推送到 azure 存储库时遇到权限问题。 You need to go the Repositories under project settings .您需要 go项目设置下的存储库 Click Git Repositories , in the security page, click plus(+) and search for group {your project name} build service({your org name}) and click to add it, and In the access control summary page, grant contribute and read permission点击Git Repositories ,在安全页面,点击加号(+),搜索组{your project name} build service({your org name})并点击添加,并在访问控制摘要页面中,授予贡献并阅读允许

Please check out this thread .请查看此线程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 使用 terraform 创建 devops 发布管道 - Azure devops release pipeline creation using terraform 使用 Github CICD 运行 Azure DevOps 管道时,带有 ID 的 Terraform 资源已存在错误 - Terraform resource with the ID already exists error when running Azure DevOps pipeline with Github CICD 如何在发布管道中的 Azure DevOps 中输出 terraform 输出文件 - How to output terraform output file in Azure DevOps in release pipeline Azure devops 管道 terraform 错误 - 尝试角色分配时出现 403 - Azure devops pipeline terraform error - 403 when attempting role assignment 通过 azure devops 中的管道将 terraform 部署到 azure - Deploy terraform to azure through a pipeline in azure devops Azure Devops - 使用 Terraform 创建管道环境 - Azure Devops - create pipeline environment with Terraform 使用 azure 后端存储中的状态文件将 terraform 的输出传递到 Azure Devops Pipeline - pass output from terraform to Azure Devops Pipeline with state file in azure backend store 运行 docker-compose 时出现 Azure DevOps Pipeline 错误 - Azure DevOps Pipeline error when running docker-compose up 从 Azure DevOps 管道 YAML 文件运行 `az artificats` 命令 - Running `az artificats` commands from Azure DevOps pipeline YAML file 我们可以使用 sonarqube 从 Terraform 和 azure devops ci/cd 管道扫描(基础设施即代码)IaC 吗? - can we use sonarqube to scan (Infrastructure as code ) IaC from Terraform with azure devops ci/cd pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM