- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 Terraform 在 Azure 上部署虚拟机。在 AzureRM 提供程序 block 中使用 client_id、subscription_id、client_secret 和tenant_id 时,VM 部署正确。但是,我想利用托管身份,这样我就不必公开 client_secret。
我尝试过的事情:为此,我遵循了这个guide
我包含了 azuread 提供程序 block ,使用“use_msi = true”来指示应使用托管身份。还包括 azurerm_subscription block 、azurerm_Client_config 以及资源定义。然后将角色分配添加到虚拟机。
代码:
terraform {
required_providers {
azuread = {
source = "hashicorp/azuread"
}
}
}
provider "azurerm" {
features {}
//client_id = "XXXXXXXXXXXXXX"
//client_secret = "XXXXXXXXXXXXXX"
//subscription_id = "XXXXXXXXXXXXXX"
tenant_id = "TENANT_ID"
//use_msi = true
}
provider "azuread" {
use_msi = true
tenant_id = "TENANT_ID"
}
#Resource group definition
resource "azurerm_resource_group" "myVMachineRG" {
name = "testnew-resources"
location = "westus2"
}
resource "azurerm_virtual_network" "myVNet" {
name = "testnew-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.myVMachineRG.location
resource_group_name = azurerm_resource_group.myVMachineRG.name
}
resource "azurerm_subnet" "mySubnet" {
name = "testnew-internal-subnet"
resource_group_name = azurerm_resource_group.myVMachineRG.name
virtual_network_name = azurerm_virtual_network.myVNet.name
#256 total IPs
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "myNIC" {
name = "testnew-nic"
location = azurerm_resource_group.myVMachineRG.location
resource_group_name = azurerm_resource_group.myVMachineRG.name
ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.mySubnet.id
private_ip_address_allocation = "Dynamic"
}
}
#ADDED HERE:
data "azurerm_subscription" "current" {}
data "azurerm_client_config" "example" {
}
resource "azurerm_virtual_machine" "example" {
name = "testnew-vm"
location = azurerm_resource_group.myVMachineRG.location
resource_group_name = azurerm_resource_group.myVMachineRG.name
network_interface_ids = ["${azurerm_network_interface.myNIC.id}"]
vm_size = "Standard_F2"
#Option to delete disks when Terraform destroy is performed.
#This is to ensure that we don't keep wasting balance
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "OSDISK"
caching = "Readwrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
#Just for testing purposes, would be better to use a KeyVault reference here instead.
os_profile {
computer_name = "XXXXXXXXXXXXXX"
admin_username = "XXXXXXXXXXXXXX"
admin_password = "XXXXXXXXXXXXXX"
}
#Force password to authenticate
os_profile_linux_config {
disable_password_authentication = false
}
identity {
type = "SystemAssigned"
}
}
data "azurerm_role_definition" "contributor" {
name = "Contributor"
}
resource "azurerm_role_assignment" "example" {
//name = azurerm_virtual_machine.example.name
scope = data.azurerm_subscription.current.id
role_definition_name = "Contributor"
//role_definition_id = "${data.azurerm_subscription.current.id}${data.azurerm_role_definition.contributor.id}"
//principal_id = azurerm_virtual_machine.example.identity[0].principal_id
principal_id = data.azurerm_client_config.example.object_id
}
错误:
Error: building AzureRM Client: obtain subscription() from Azure CLI: parsing json result from the Azure CLI: waiting for the Azure CLI: exit status 1: ERROR: Please run 'az login' to setup account.
│
│ with provider["registry.terraform.io/hashicorp/azurerm"],
│ on main.tf line 9, in provider "azurerm":
│ 9: provider "azurerm" {
我不明白为什么当我尝试使用托管身份登录时它仍然要求使用 az login。
出于安全目的,编辑了tenantID。
任何帮助将不胜感激:)
最佳答案
我尝试在我的环境中重现相同的要求,并成功部署它。
如果您通过 terraform 中的托管身份进行身份验证,则需要提供托管身份的名称。
添加 msi_name
在 azureread
提供程序下。
注意:正如您所给出的,请确保托管身份应具有足够的权限(贡献者角色)来进行身份验证和创建资源,否则部署将失败。
main.tf
data "azurerm_subscription" "current" {}
variable "subscription_id" {
default = "xxxxxxxxxxxx"
}
provider "azurerm"{
features{}
subscription_id = var.subscription_id
}
provider "azuread"{
features{}
use_msi = true
msi-name = "jahnaviidentity" //Give Name of the Managed identity
}
resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "West Europe"
}
resource "azurerm_virtual_network" "main" {
name = "main-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.main.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "main" {
name = "main-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
ip_configuration {
name = "<configurationname>"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "main-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.main.id]
vm_size = "Standard_DS1_v2"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "<computername>"
admin_username = "<admin/username>"
admin_password = "xxxxxx"
}
os_profile_linux_config {
disable_password_authentication = false
}
identity {
type = "SystemAssigned"
}
}
输出:
terraform初始化
:
地形计划
:
地形应用
:
已在 Azure 门户中成功部署:
关于azure - 在 Azure 上使用 Terraform 部署具有托管标识的 VM 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75227193/
有什么方法可以将 Terraform 模板输出用于另一个 Terraform 模板的输入? 例如:我有一个创建 ELB 的 Terraform 模板,我有另一个 Terraform 模板,它将创建一个
我正在使用 Terraform 在 Azure 中设置虚拟网络。 我有几个 VNet,每个 VNet 都有自己的网络安全组 100% 在 Terraform 中管理,在运行 Terraform 之前不
resources and data sources在 terraform 文档中 link ,谁能解释一下它们的区别以及可以使用它们的示例场景 最佳答案 Data Sources :允许 Terra
terraform plan 等命令如何知道/决定使用哪些文件? -help 显示了一个 DIR-OR-PLAN 参数,但没有显示如何使用它: $ terraform -help plan Usage
我在尝试运行使用 terraform lock 的 terraform 脚本时收到以下错误消息。 *Acquiring state lock. This may take a few moments.
我想简化这样的构造 variable "google" { type = object({ project = string region = string
这是一个场景 - 您开发用于研发组织的 terraform 模块。它们已经被一两个微服务使用,转化为十几个 pod。您确定了重构机会,例如将某些功能提取到其自己的 terraform 模块中。很好,但
Terraform 是否支持条件属性?我只想根据变量的值使用属性。 例子: resource "aws_ebs_volume" "my_volume" { availability_zone =
我想将此作为功能请求发布,但我想在发布之前看看是否有其他人找到了一些聪明的方法。或者也许 Hashicorp 的某个人可以告诉我这将是 future 的一个功能 在运行 terraform apply
我在 terraform 的变量插值中遇到了麻烦。这是我的 terraform 配置的样子。即内置函数内的变量 variable "key" {} ssh_keys { pat
运行 terraform 并等待需要很长时间。 所以我想运行它来排除需要最长执行时间的 rds 或者我只想运行 ec2 资源。 有没有办法在 terraform 中做这样的事情? 最佳答案 您可以使用
terraform 是否提供这样的功能来覆盖变量值?假设我已经声明了下面给出的两个变量。 variable "foo" {} variable "bar" { default = "false"} f
我正在为 Terraform Associate Certification 做准备考试。我在 Udemy 上进行了一次练习考试,并收到了一个关于自动安装社区提供程序的问题。但是,根据实际 terra
我有很多使用 Terraform 的 gcp-provider 用 Terraform 0.11 编写的 Terraform 模块,并希望将其升级到 Terraform 0.12。 为此,我需要保留系
我的项目有 2 个存储库。静态网站和服务器。我希望网站由 cloudfront 和 s3 托管,服务器在 elasticbeanstalk 上。我知道这些资源至少需要了解 Route53 资源才能在同
我能有这样的资源吗 resource "foo" "bar.baz"{ ... } 或者以后 . 会把我搞砸吗?特别是,是否允许这样做: resource "foo" "other"{ ...
我能有这样的资源吗 resource "foo" "bar.baz"{ ... } 或者以后 . 会把我搞砸吗?特别是,是否允许这样做: resource "foo" "other"{ ...
运行时terraform init使用 Terraform 时 0.11.3我们收到以下错误: Initializing provider plugins... - Checking for avai
我正在尝试将项目的 CLI 工作区迁移到 Terraform Cloud。我正在使用 Terraform 版本 0.14.8 并遵循官方指南 here . $ terraform0.14.8 work
尝试在Azure Pipeline中将terraform init作为任务运行时,错误指出 spawn C:\hostedtoolcache\windows\terraform\0.12.7\x64\
我是一名优秀的程序员,十分优秀!