【问题标题】:Terraform, select value from powershell gridviewTerraform,从powershell gridview中选择值
【发布时间】:2019-06-08 18:40:58
【问题描述】:

我执行了一个调用 powershell 命令的 terraform 文件。我的想法是,不同的订阅出现在屏幕上并选择其中一个,以便稍后我可以在 Azure 中创建某些资源。

选择后,在所选订阅中正确创建了资源组,但再次执行后,选择另一个订阅后,我没有创建任何其他内容并出现添加和删除消息

provider "azurerm" {
    version = "=1.28.0"
 }

resource "null_resource" "script1" {

    triggers {
        build_number = "${timestamp()}"
    }
    provisioner "local-exec" {
    command = <<EOT
        Login-AzureRmAccount
        $SubscriptionId = (Get-AzureRmSubscription | select Name, State, SubscriptionId, TenantId | Out-GridView -Title "Azure Subscription Selector" -PassThru).SubscriptionId
        Get-AzureRmSubscription -SubscriptionId $SubscriptionId | Select-AzureRmSubscription
    EOT

        interpreter = ["powershell"]
        }
    }

#variable "subscripcion" {}


resource "azurerm_resource_group" "resources_groups" {
    name        =  "resources_groups"
    location    =   "west europe"
 }

申请完成!资源:添加 1 个,更改 0 个,销毁 1 个。

我如何使用此选项,我是否可以使用不同的订阅来创建指定的资源而不删除我已经实现的资源?

谢谢

【问题讨论】:

    标签: powershell terraform


    【解决方案1】:

    当您应用 terraform 时,它首先会刷新现有资源的状态。假设您已经在一个订阅中创建了资源组,当您再次运行应用时,它将刷新该特定订阅中资源组的状态。

    当您确认应用操作时,它将更改您在本地 shell 中选择的订阅(通过 null_resource),但此时它已经计算出不需要更改之前选择的订阅。

    如果您在 local-exec 选择了所需的订阅后再次运行它,它可能会起作用。

    我可以建议您在调用 terraform apply 时将 subscription_id 作为参数传递吗?这样,从一开始就可以为提供者配置正确的订阅。

    代码如下:

    variable "subscription_id" {
      type = "string"
    }
    
    provider "azurerm" {
        version = "=1.28.0"
        subscription_id = var.subscription_id
     }
    
    resource "azurerm_resource_group" "resources_groups" {
        name        =  "resources_groups"
        location    =   "uksouth"
     }
    

    并使用命令terraform apply -var 'subscription_id=&lt;my_subscription_id&gt;调用

    然后您可以使用 powershell 运行 terraform 以进行交互式选择

    Login-AzAccount
    $SubscriptionId = (Get-AzSubscription | select Name, State, SubscriptionId, TenantId | Out-GridView -Title "Azure Subscription Selector" -PassThru).SubscriptionId
    terraform apply -var `subscription_id=$SubscriptionId`
    

    【讨论】:

    • 首先,请原谅延迟回答。非常感谢您的指示。他们正确地为我工作!
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多