使用Terraform部署后,我试图在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")}"
}
答案 0 :(得分:1)
在azurerm_virtual_machine_extension中,您可以使用:
protected_settings = <<PROTECTED_SETTINGS
{
"script": "${base64encode(file(var.scfile))}"
}
PROTECTED_SETTINGS
请参阅我的answer
答案 1 :(得分:0)
首先,VM扩展名将仅执行脚本,而不将文件复制到VM。如果要将脚本复制到VM中,然后执行它。我会建议您使用Terraform provisioner file
and remote-exec
。
以下是将文件复制到现有VM并执行脚本的示例:
resource "null_resource" "example" {
connection {
type = "ssh"
user = "azureuser"
password = "azureuser@2018"
host = "13.92.255.50"
port = 22
}
provisioner "file" {
source = "script.sh"
destination = "/tmp/script.sh"
}
provisioner "remote-exec" {
inline = [
"/bin/bash /tmp/script.sh"
]
}
}
注意:该脚本应在当前目录中创建。