我正在编写我的第一个terraform脚本,我需要创建多个服务主体应用程序。我可以使用以下脚本创建每个脚本:
resource "azuread_application" "main" {
name = var.name
available_to_other_tenants = false
identifier_uris = [format("http://%s", var.name)]
}
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}"
}
同样,我想创建其中的6个,什么时候将所有6个SP重用此代码的最佳方法是什么?
我知道可以这样做:
resource "azuread_application" "auth" {
name = "${var.sp_names[count.index]}"
available_to_other_tenants = false
identifier_uris = [format("http://%s", ${var.sp_names[count.index]})]
count = "${length(var.sp_names)}
}
但是如果我这样做,我是否会将ide传递给azuread_service_principal
块?
答案 0 :(得分:3)
要通过Terraform同时创建多个服务主体,需要使用资源中的count
属性。
这是示例代码:
resource "azuread_application" "example" {
count = 2
name = "example-${count.index}"
available_to_other_tenants = false
oauth2_allow_implicit_flow = true
}
resource "azuread_service_principal" "example" {
count = 2
application_id = "${azuread_application.example[count.index].application_id}"
app_role_assignment_required = false
}
我看到您还想为服务主体设置密码和角色分配,那么您还需要像上面一样在其资源中使用count
属性。