【问题标题】:What's the equivalent terraform code for Azure AD SP create-for-rbac?Azure AD SP create-for-rbac 的等效 terraform 代码是什么?
【发布时间】:2019-09-15 00:47:51
【问题描述】:

对于集成,服务定义了以下要运行的命令

az ad sp create-for-rbac --role reader --scopes /subscriptions/{subscription_id}

我没有运行命令,而是想知道az ad sp create-for-rbac 的等效 terraform 代码是什么?

【问题讨论】:

    标签: azure azure-active-directory terraform azure-cli


    【解决方案1】:
    provider "azuread" {
      version = "=0.3.0"
    }
    
    resource "azuread_application" "auth" {
      name = "auth"
    }
    
    resource "azuread_service_principal" "auth" {
      application_id = "${azuread_application.auth.application_id}"
    }
    
    resource "random_string" "password" {
      length = 16
      special = true
      override_special = "/@\" "
    }
    
    resource "azuread_service_principal_password" "auth" {
      service_principal_id = "${azuread_service_principal.auth.id}"
      value                = "${random_string.password.result}"
      end_date_relative    = "240h"
    }
    
    output "client_secret" {
      value = "${random_string.password.result}"
      description = "Client Secret"
    }
    
    provider "azurerm" {
      version = "=1.24.0"
    }
    
    data "azurerm_subscription" "primary" {}
    
    data "azurerm_client_config" "current" {}
    
    resource "azurerm_role_assignment" "auth" {
      scope                = "${data.azurerm_subscription.primary.id}"
      role_definition_name = "Reader"
      principal_id         = "${azuread_service_principal.auth.id}"
    }
    

    【讨论】:

    • 这对你真的有用吗?对我来说,应用程序已创建,Terraform 输出密钥且没有错误消息,但是在 Azure 门户中,没有与创建的应用程序关联的客户端密钥。我已经尝试过这篇文章的提供者版本和最新版本(分别为 0.4.0 和 0.30.1)
    • 工作 100%,目前有一个正在运行的部署。您可以使用客户端 ID、客户端密码和租户 ID 来验证到 azure 吗?您可以尝试按照本指南来测试您所做的信用:azure.microsoft.com/en-us/resources/samples/…
    • @MattiasJiderhamn 我认为这段代码正在做它应该做的事情,但我认为你误解了那是什么。 azuread_service_principal_password 是服务主体帐户的密码,但这与应用程序上的客户端密码不同。我正在尝试找到一种使用 az cli 或 terraform 创建它的方法,但我认为还没有。
    • 我认为这些答案不再适用。请参阅stackoverflow.com/questions/71025381/…docs.microsoft.com/en-us/answers/questions/197819/… 现在有没有办法自动执行此操作,而无需手动登录并执行上述链接中的步骤?
    【解决方案2】:

    我必须在第二个提供者中添加一个别名才能为我工作。 Terraform 0.12 不允许我有 2 个没有别名的天蓝色不同的提供者。 Azure 资源管理和 Azure Active Directory

    
    provider "azuread" {
     version = "~> 0.3"
    
    }
    
    provider "azurerm" {
     version = "~>1.44.0"
     alias   = "azure_rm"
    }
    
    data "azurerm_subscription" "primary" {
     provider = azurerm.azure_rm
    }
    
    
    resource "azuread_application" "auth" {
     name = "${var.application_name}"
    }
    
    resource "azuread_service_principal" "auth" {
     application_id = "${azuread_application.auth.application_id}"
    }
    
    resource "azuread_service_principal_password" "auth" {
     service_principal_id = "${azuread_service_principal.auth.id}"
     value                = "${random_string.password.result}"
     end_date_relative    = "240h" 
    }
    
    resource "random_string" "password" {
     length = "${var.password_length}"
     special = "${var.password_special}"
     override_special = "${var.password_override_special}"
    }
    
    resource "azurerm_role_assignment" "auth" {
     provider = azurerm.azure_rm
     scope                = "${data.azurerm_subscription.primary.id}"
     role_definition_name = "Contributor"
     principal_id         = "${azuread_service_principal.auth.id}"
    }
    
    output "subscription-id" {
     value = "${data.azurerm_subscription.primary.id}"
     description = "subscription"
    }
    
    output "tenant" {
     value = "${data.azurerm_subscription.primary.tenant_id}"
     description = "tenant"
    }
    
    output "password" {
     value = "${random_string.password.result}"
     description = "password"
    }
    
    output "name" {
     value = "${azuread_application.auth.application_id}"
     description = "name"
    }
    

    【讨论】:

    • 我认为您可能在单独的一段代码中为提供者“azurerm”提供了提供者设置。提供者“azurerm”和提供者“azuread”是两个不同的提供者,因此您不需要别名来区分它们。
    猜你喜欢
    • 2023-03-30
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 2017-06-09
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多