【问题标题】:terraform local-exec provisioner failing to see azdo organization in cli commandterraform local-exec provisioner 无法在 cli 命令中看到 azdo 组织
【发布时间】:2021-05-04 21:17:19
【问题描述】:

为了使local-exec 配置程序能够成功运行az cli 命令,需要对以下 terraform 语法进行哪些具体更改?

这是导致问题的 terraform 代码:​​

resource "azuredevops_git_repository" "repository" {
  project_id = data.azuredevops_project.p.id
  name = var.repoName
  initialization {
    init_type = "Uninitialized"
  }
  provisioner "local-exec" {
    working_dir = "C:\\projects\\acm\\Apr2021\\config-outside-acm-path\\vars\\deleteThis\\"
    command = "az repos import create --git-source-url \"${var.sourceRepo}\" --repository \"${azuredevops_git_repository.repository.name}\" --organization \"${var.azdoOrgServiceURL}\" --project \"${var.projectName}\""
  }
}

这是我们得到的错误:

Error: Error running command 
'az repos import create --git-source-url "https://github.com/PublicGitHubAccount/public-github-repo.git" 
--repository "private-azure-repo" --organization "https://dev.azure.com/OurValidOrganizationName" 
--project "SampleProject"'
: exit status 1. 

Output: --organization must be specified. 
The value should be the URI of your Azure DevOps organization, for example: https://dev.azure.com/MyOrganization/ or your Azure DevOps Server organization. 
You can set a default value by running: az devops configure --defaults organization=https://dev.azure.com/MyOrganization/. 
For auto detection to work (--detect true), you must be in a local Git directory that has a "remote" referencing a Azure DevOps or Azure DevOps Server repository.

当我们从错误消息中复制命令并通过 Python 程序将该命令作为 shell 命令运行时,该命令可以正常运行而不会出现错误。以下是使用 Python shell 执行时正确运行的命令:

'az repos import create --git-source-url "https://github.com/PublicGitHubAccount/public-github-repo.git" 
--repository "private-azure-repo" --organization "https://dev.azure.com/OurValidOrganizationName" 
--project "SampleProject"'  

因此,问题在于 terraform 无法看到 --organization 变量,即使您可以从上面看到 terraform 正确地插入了字符串。

【问题讨论】:

    标签: azure azure-devops terraform terraform-provider-azure


    【解决方案1】:

    尝试省略 URL 周围的双引号 ("),看看它是否可以工作:

    command = "az repos import create --git-source-url \"${var.sourceRepo}\" --repository \"${azuredevops_git_repository.repository.name}\" --organization ${var.azdoOrgServiceURL} --project \"${var.projectName}\""
    

    【讨论】:

    • 省略引号会导致呈现文字 var.sourceRepo 而不是变量值。请注意,引号已被转义。另请注意,terraform 需要将转义引号作为其插值语法的一部分。
    • 嗨@CodeMed,如何显式输入 URL 而不是在命令中使用变量?就像您在 Python 代码中尝试过的一样。我认为这个问题可能是由某些语法或格式错误引起的,terraform 代码可能有一些语法/格式限制和约定。
    • 显式命令在命令行工作。因此,问题在于 azure devops terraform 提供程序解析变量插值语法的方式。我们需要在 terraform 中使用变量而不是文字,因为正如您可以想象的那样,在自动化期间更改 terraform 模块中的文字字符串的代码会很脆弱,维护成本很高,并且当机密意外发送到版本控制时容易出现安全漏洞.
    • 嗨@CodeMed,如果可能的话,你能在 Azure DevOps 上分享你的管道的 YAML 文件吗?并指定您使用哪个任务在管道中运行 terraform 代码?
    猜你喜欢
    • 2020-10-04
    • 2019-09-30
    • 2020-01-02
    • 2019-09-21
    • 2023-02-14
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多