Terraform - Azure - azurerm_virtual_machine_extension - 内联 Powershell 脚本

问题描述 投票:0回答:1

我正在准备虚拟机部署,在虚拟机上,我需要运行一些 Powershell 命令。我需要更改 Postgresql 属性。 我正在 Azure 云上工作,Terraform 部署已经过测试,并且按预期工作,VM 已使用所有必需的配置进行部署,但面临 PowerShell 命令的一些问题。我使用 terraform 资源 azurerm_virtual_machine_extension,例如,当我尝试安装/使用一段代码时,它可以工作

resource "azurerm_virtual_machine_extension" "start_postgres_service" {
  name                 = "start_postgresql"
  virtual_machine_id   = azurerm_windows_virtual_machine.virtual_machine.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  settings = <<SETTINGS
 {
  "commandToExecute": "powershell -ExecutionPolicy Unrestricted Start-Service -Name postgresql-x64-16"
 }
SETTINGS
}

我不想使用存储帐户和容器以及上传到存储容器的.ps1脚本文件,我需要使用内联powershell代码。下面是我的 powershell 文件和 terraform clode。

param (
    [string]$VMUser,
    [string]$VMPassword,
    [string]$VMName
)

$account=".\$VMUser"
$password= "$VMPassword"
$service="name='postgresql-x64-16'"

$svc=Get-WmiObject win32_service -ComputerName "$VMName" -filter $service
$svc.StopService()
$svc.change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null)
$svc.StartService()
resource "azurerm_virtual_machine_extension" "start_postgres_service" {
  name                 = "start_and_change_postgresql"
  virtual_machine_id   = azurerm_windows_virtual_machine.virtual_machine.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.9"

  settings = <<SETTINGS
    {
      "commandToExecute": "powershell -Command \\"param([string]`\\$VMUser, [string]`\\$VMPassword, [string]`\\$VMName); `\\$account = '.\\\\' + `\\$VMUser; `\\$password = `\\$VMPassword; `\\$service = 'name=\\\\'postgresql-x64-16\\\\''; `\\$svc = Get-WmiObject win32_service -ComputerName `\\$VMName -filter `\\$service; `\\$svc.StopService(); `\\$svc.change($null,$null,$null,$null,$null,$null,`\\$account,`\\$password,$null,$null,$null); `\\$svc.StartService();\\" -VMUser '${var.vm_user}' -VMPassword '${var.vm_password}' -VMName '${azurerm_windows_virtual_machine.virtual_machine.computer_name}'"
    }
  SETTINGS
}

感谢您的所有帮助。

azure powershell terraform azure-virtual-machine
1个回答
0
投票

要执行多个命令而不将脚本存储在存储帐户中,可以使用内联方法,但最好的方法是将脚本存储在存储帐户或公共存储库中,以便运行多个脚本而不发生任何冲突。

您也可以在空资源块中使用

Invoke-AzVMRunCommand

provider "azurerm" {
  features {}
}

data "azurerm_virtual_machine" "example" {
  name                = "VenkatVM"
  resource_group_name = "APIM"
}

resource "azurerm_virtual_machine_extension" "manage_services" {
  name                 = "Extensions"
  virtual_machine_id   = data.azurerm_virtual_machine.example.id
  publisher            = "Microsoft.Compute"
  type                 = "CustomScriptExtension"
  type_handler_version = "1.8"

  settings = <<SETTINGS
    {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted -Command \\\"Start-Service -Name 'postgresql-x64-16'; Start-Service -Name 'PhoneSvc'; Stop-Service -Name 'Spooler'\\\""
    }
  SETTINGS
}

Terraform 应用:

enter image description here

扩展已成功安装。

enter image description here

服务启动成功。

enter image description here

要更改

PostgreSQL account
,您需要连接psql,然后连接到数据库。因此,无法直接通过 PowerShell 命令进行操作。请参阅链接PostgreSQL中切换用户。

© www.soinside.com 2019 - 2024. All rights reserved.