gpt4 book ai didi

azure-devops - AzureDevops 管道使用 terraform 和 local-exec az 命令因服务主体失败

转载 作者:行者123 更新时间:2023-12-03 23:34:44 25 4
gpt4 key购买 nike

由于 terraform azurerm 提供程序缺少对 azure webapp 访问限制 (see github issue) 的支持。我们使用 null_resourcelocal-exec 来应用访问限制:


provisioner "local-exec" {
command = <<COMMAND
az webapp config access-restriction add --subscription ${self.triggers.subscription_id} --resource-group ${self.triggers.resource_group} \
--name ${self.triggers.web_app_name} --rule-name 'allow application gateway' --action Allow --vnet-name ${self.triggers.vnet_name} \
--subnet ${self.triggers.subnet_name} --priority 100
COMMAND
}

我们的 terraform 代码稍后由 Azure DevOps Pipeline 运行,该管道使用服务连接(使用服务主体)向 Azure 进行身份验证。以下任务正在尝试应用 terraform 资源:

  - task: TerraformCLI@0
displayName: "Terraform apply"
inputs:
command: 'apply'
commandOptions: '--var-file="./environments/${{ parameters.environment }}.tfvars"'
workingDirectory: '$(System.DefaultWorkingDirectory)/${{ parameters.projectFolder }}'
environmentServiceName: 'shared-${{ parameters.environment }}-001'

这导致以下错误:
Error: Error running command '      az webapp config access-restriction remove --subscription shared-staging-001 --resource-group rg-hub-network-staging \
--name landing-webapp-hub --rule-name 'allow application gateway'
': exit status 1. Output: Subscription 'shared-staging-001' not recognized.
Command group 'webapp config access-restriction' is in preview. It may be changed/removed in a future release.
Please run 'az login' to setup account.

不,我们尝试用普通 bash 脚本或 AzureCLI@2 任务替换 TerraformCLI@0 任务。

由于缺少 Infos,我们无法让 az login 在普通的 bash 脚本中工作。 here 描述的方法也不起作用。

在 AzureCLI@2 任务中运行 terraform 命令看起来很有希望,但会导致一些与服务主体登录相关的奇怪错误:

  - task: AzureCLI@2
displayName: "Terraform init"
inputs:
azureSubscription: shared-${{ parameters.environment }}-001
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
terraform init --backend-config="./environments/${{ parameters.environment }}_backend.tfvars"

这会导致以下错误:
Initializing modules...
- app-gateway in modules/app-gateway
- dummy1 in modules/BRZ365-AppService
- dummy2 in modules/BRZ365-AppService
- hub-network in modules/hub-network
- landing_zone_app in modules/BRZ365-AppService
- squad-area in modules/squad-area

Initializing the backend...

Error: Error building ARM Config: Authenticating using the Azure CLI is only supported as a User (not a Service Principal).

To authenticate to Azure using a Service Principal, you can use the separate 'Authenticate using a Service Principal'
auth method - instructions for which can be found here:

Alternatively you can authenticate using the Azure CLI by using a User Account.

最佳答案

我终于使用我在第一篇文章中描述的 AzureCLI 方法实现了这一点。我使用 addSpnToEnvironment(它将服务提供者凭据添加到环境中,如 the documentation 中所述)并按照 by terraform 所述设置所需参数。

      - task: AzureCLI@2
displayName: "Terraform"
inputs:
azureSubscription: shared-${{ parameters.environment }}-001
scriptType: bash
addSpnToEnvironment: true
scriptLocation: inlineScript
inlineScript: |
export ARM_CLIENT_ID=$servicePrincipalId
export ARM_CLIENT_SECRET=$servicePrincipalKey
export ARM_TENANT_ID=$tenantId

terraform init .....


关于azure-devops - AzureDevops 管道使用 terraform 和 local-exec az 命令因服务主体失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61268776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com