shell 尝试通过terraform从azurerm_virtual_machine_extension执行wget命令,但curl无法执行

bqucvtff  于 6个月前  发布在  Shell
关注(0)|答案(1)|浏览(90)

尝试通过terraform从azurerm_virtual_machine_extension执行wgetcommand,但是curl无法执行。

resource "azurerm_virtual_machine_extension" "extension_v1" {
  name                       = "vm-extension"
  virtual_machine_id         = azurerm_virtual_machine.PrimaryVM.id
  publisher                  = "Microsoft.Azure.Extensions"
  type                       = "CustomScript"
  type_handler_version       = "2.0"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    { 
     "commandToExecute": " sudo wget -P /etc "https://example.sh";"    
    }
    SETTINGS
}

字符串
要求是使用wget下载文件。无法为Azure扩展执行curl命令

ehxuflar

ehxuflar1#

我尝试通过Terraform从azurerm_virtual_machine_extension执行wget命令,并且我能够检查需要放置在配置中的修复。
代码片段似乎在JSON块中字符串的格式设置方面存在问题。您在双引号中使用了双引号,但没有对其进行转义,这将导致JSON解析错误。
如果你想使用wget,坚持使用它,不要和curl混合使用。wgetcurl都是从web获取内容的工具,但是它们有不同的语法和选项。

我的demo地形配置:

main.tf:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "Demorg-vk"
  location = "West US"
}

resource "azurerm_virtual_network" "example" {
  name                = "sbvk-network"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_public_ip" "example" {
  name                = "sbvk-publicip"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  allocation_method   = "Dynamic"
}

resource "azurerm_network_interface" "example" {
  name                = "sbvk-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example.id
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "sbvk-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }

  storage_os_disk {
    name              = "osdisk-example"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Premium_LRS"
  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "admin"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

  tags = {
    environment = "staging"
  }
}

resource "azurerm_virtual_machine_extension" "example" {
  name                 = "sbvk-vm-extension"
  virtual_machine_id   = azurerm_virtual_machine.example.id
  publisher            = "Microsoft.Azure.Extensions"
  type                 = "CustomScript"
  type_handler_version = "2.0"

  settings = <<SETTINGS
    {
      "commandToExecute": "sudo wget -P /etc 'https://example.sh'"
    }
SETTINGS
}

字符串
一旦您完成了使用shell脚本配置所提到的域,即example.sh
开始执行命令

Terraform init
地形图
适用Terraform

通过对上述代码进行必要的更改,我们将能够通过terraform从azurerm_virtual_machine_extension执行wget命令。
一旦你能够运行命令,运行URL下载shell脚本,你试图这样做。

相关问题