代码之家  ›  专栏  ›  技术社区  ›  Nguyen Tung Son

如何使用Terraform在Azure虚拟机上启动postgres.exe并更改postgres帐户

  •  0
  • Nguyen Tung Son  · 技术社区  · 2 月前

    我正在使用Azure库中的映像创建Azure虚拟机。操作系统是Windows(Windows Server 2022数据中心Azure版)。Postgresql程序已预先安装在映像中。如何在使用Terraform部署Azure虚拟机后立即启动postgres.exe?

    我目前的解决方案是使用Invoke-AzVMRunCommand,但我想知道是否有其他可用的方法(比如使用azure rm_virtual_machine_extension)。此外,如果我想使用Terraform更改postgres帐户,我应该如何继续?

    谢谢你的帮助。

    1 回复  |  直到 2 月前
        1
  •  0
  •   Venkat V    2 月前

    如何使用Terraform在Azure虚拟机上启动postgres.exe并更改postgres帐户

    最佳方式 开始 任何 service 并在内部安装应用程序 VM 是使用 azurerm_virtual_machine_extension 资源。

    这是 Postgres服务 执行地形脚本之前的状态。

    enter image description here

    在您的情况下,您可以使用 azure rm模块 如下图所示。

    resource "azurerm_windows_virtual_machine" "main" {
    name                  = "demovm"
    
    remaining configuration
    }
    
    resource "azurerm_virtual_machine_extension" "example" {
    name                 = "postgres_Service_start"
    .
    .
    remaining configuration
    
       settings = <<SETTINGS
     {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted Start-Service -Name postgresql-x64-16"
     }
    SETTINGS
    depends_on = [ azurerm_windows_virtual_machine.main ]
    }
    

    在我的例子中,我使用现有的VM进行测试,所以我使用数据块引用它。

    provider "azurerm" {
      features {}
    }
    data "azurerm_virtual_machine" "example" {
      name                = "windows-Server-2022"
      resource_group_name = "VM-RG"
    }
    resource "azurerm_virtual_machine_extension" "example" {
      name                 = "postgres_Service_start"
      virtual_machine_id   = data.azurerm_virtual_machine.example.id
      publisher            = "Microsoft.Compute"
      type                 = "CustomScriptExtension"
      type_handler_version = "1.8"
      auto_upgrade_minor_version = true
    
      settings = <<SETTINGS
     {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted Start-Service -Name postgresql-x64-16"
     }
    SETTINGS
    }
    

    地形适用

    enter image description here

    已在VM中成功创建扩展,如下所示。

    enter image description here

    执行Terraform代码后 PostgreSQL 服务已成功启动。

    enter image description here

    要更改a PostgreSQL帐户 ,连接到 数据库 并检查现有用户。因为这是不可能的 Terraform ,如果您发现有任何正在使用的扩展,则可以使用扩展执行脚本。有关更多详细信息,请按照以下步骤操作 link .

    推荐文章