简体   繁体   English

Terraform azurerm 读取当前登录用户?

[英]Terraform azurerm read current signed in user?

Looking at the documentation I am unable to find a data source which gives me the current user (preferably the email) logged in to az when using the azurerm provider in terraform.查看文档,我无法找到在 terraform 中使用 azurerm 提供程序时为我提供登录到az的当前用户(最好是电子邮件)的数据源。

This information is available when I run az ad signed-in-user and I would like to use it to tag the resources created by terraform in azure.当我运行az ad signed-in-user时可以使用此信息,我想用它来标记 azure 中 terraform 创建的资源。

Is this not possible right now?现在这不可能吗?

You can use azurerm_client_config to get the AD object ID for the current user and then look up the returned object id with azuread_user to get the user principal name (UPN).您可以使用 azurerm_client_config 获取当前用户的 AD object ID,然后使用 azuread_user 查找返回的 object id 以获取用户主体名称 (UPN)。 Then, the UPN can be assigned to a tag.然后,可以将 UPN 分配给标签。 In the code below, outputs are not necessary but are helpful for validation because their values appear in the plan.在下面的代码中,输出不是必需的,但有助于验证,因为它们的值出现在计划中。

data "azurerm_client_config" "current" { }

data "azuread_user" "current_user" {
  object_id = data.azurerm_client_config.current.object_id
}

resource "azurerm_resource_group" "example-rg" {
  name     = "example-rg"
  location = "westus"
  tags = {
    userCreated = data.azuread_user.current_user.user_principal_name
  }
}

output "object_id" {
  value = data.azurerm_client_config.current.object_id
}

output "user_principal_name" {
  value = data.azuread_user.current_user.user_principal_name
}

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

相关问题 Terraform Azurerm:如果不存在则创建 blob - Terraform Azurerm: Create blob if not exists Terraform 导入 azurerm_role_assignment - Terraform Import azurerm_role_assignment terraform azurerm - 不能破坏公共 ip - terraform azurerm - cannot destroy public ip Terraform azurerm_windows_function_app ip_restrictions 问题 - Terraform azurerm_windows_function_app ip_restrictions issues terraform 错误:周期:data.azurerm_key_vault_secret - terraform Error: Cycle: data.azurerm_key_vault_secret terraform azurerm_data_factory_pipeline 将类型分配给变量 - terraform azurerm_data_factory_pipeline assing type to the variables terraform azurerm:错误:退出状态 1 - 需要 az 登录 - terraform azurerm : ERROR : exit status 1 - az login required Terraform AzureRM 不断修改 API 使用默认端点的代理配置进行管理 - Terraform AzureRM Continually Modifying API Management with Proxy Configuration for Default Endpoint 如何在 Google Firestore 中获取当前登录用户的文档 - How to get document by current signed in user in Google Firestore Terraform - Azure - 同时使用“azurerm_windows_virtual_machine”和“azurerm_mssql_virtual_machine” - 但未配置 SQL 存储 - Terraform - Azure - Using "azurerm_windows_virtual_machine" and "azurerm_mssql_virtual_machine" together - but SQL Storage isn't getting configured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM