如何使用Terraform在Azure虚拟机上启动postgres.exe并更改postgres帐户
最佳方式
开始
任何
service
并在内部安装应用程序
VM
是使用
azurerm_virtual_machine_extension
资源。
这是
Postgres服务
执行地形脚本之前的状态。
在您的情况下,您可以使用
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
}
地形适用
已在VM中成功创建扩展,如下所示。
执行Terraform代码后
PostgreSQL
服务已成功启动。
要更改a
PostgreSQL帐户
,连接到
数据库
并检查现有用户。因为这是不可能的
Terraform
,如果您发现有任何正在使用的扩展,则可以使用扩展执行脚本。有关更多详细信息,请按照以下步骤操作
link
.