gpt4 book ai didi

azure - Terraform Azure 在虚拟机上运行 bash 脚本

转载 作者:行者123 更新时间:2023-12-04 15:42:39 26 4
gpt4 key购买 nike

在使用 Terraform 部署 Azure VM 后,我尝试在 Azure VM 上运行 bash 脚本。我尝试过不同的方法,但没有一个有效。对于“custom_data”,我假设该文件将被上传并执行,但是我什至没有在虚拟机内看到该文件。

我还查看了“azurerm_virtual_machine_extension”,但这并没有给我上传文件的选项,只能执行命令或从远程位置下载(由于要求无法使用 fileUris):

resource "azurerm_virtual_machine_extension" "test" {
name = "hostname"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_machine_name = "${azurerm_virtual_machine.test.name}"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"

settings = <<SETTINGS
{
"commandToExecute": "sh my_script.sh"
}
SETTINGS

tags = {
environment = "Production"
}
}
resource "azurerm_virtual_machine" "middleware_vm" {
name = "${var.middleware_vm}"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.middleware.name}"
network_interface_ids = ["${azurerm_network_interface.middleware.id}"]
vm_size = "Standard_DS4_v2"

storage_os_disk {
name = "centos_os_disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}

storage_data_disk {
name = "managed_backup_disk"
create_option = "Empty"
caching = "ReadWrite"
disk_size_gb = "256"
managed_disk_type = "Premium_LRS"
lun = 0
}

storage_image_reference {
publisher = "OpenLogic"
offer = "CentOS"
sku = "7.5"
version = "latest"
}

os_profile {
computer_name = "${var.middleware_vm}"
admin_username = "middlewareadmin"
custom_data = "${file("scripts/middleware_disk.sh")}"
}

最佳答案

在 azurerm_virtual_machine_extension 中,您可以使用:

protected_settings = <<PROTECTED_SETTINGS
{
"script": "${base64encode(file(var.scfile))}"
}
PROTECTED_SETTINGS

请引用我的answer

关于azure - Terraform Azure 在虚拟机上运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57272318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com