custom_data terraform 脚本中未触发参数

custom_data parameter is not triggered in terraform script

我正在使用 terraform 在 azure 中创建虚拟机,我在其中传递自定义数据参数,该参数实际上应该触发一个几乎没有安装的 yaml 文件。但它没有被触发。能否请您就此问题提出建议。enter code here

控制器虚拟机

resource "azurerm_virtual_machine" "controller-vm" {
name                  = "controller-vm"`enter code here`
location              = var.region

resource_group_name   = azurerm_resource_group.resourcegroup.name
network_interface_ids = [azurerm_network_interface.controllernic.id]
vm_size               = var.ctr_instance_type

storage_os_disk {
    name              = "controller-os-disk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    disk_size_gb      = "400"
    managed_disk_type = "Standard_LRS"
}

storage_data_disk {
    name              = "controller-data-disk0"
    caching           = "ReadWrite"
    create_option     = "Empty"
    managed_disk_type = "Standard_LRS"
    disk_size_gb      = "512"
    lun               = 1
}

storage_data_disk {
    name              = "controller-data-disk1"
    caching           = "ReadWrite"
    create_option     = "Empty"
    managed_disk_type = "Standard_LRS"
    disk_size_gb      = "512"
    lun               = 2
}

storage_image_reference {
    publisher = "OpenLogic"
    offer     = "CentOS-CI"
    sku       = "7-CI"
    version   = "latest"
}

os_profile {
    computer_name  = "controller.${var.project_id}.local"
    admin_username = var.user
    custom_data = file(pathexpand(var.ctr_cloud_init_file))
}

os_profile_linux_config {
    disable_password_authentication = true
    ssh_keys {
        path     = "/home/${var.user}/.ssh/authorized_keys"
        key_data = file(pathexpand(var.ssh_pub_key_path))
    }
}

boot_diagnostics {
    enabled     = "true"
    storage_uri = azurerm_storage_account.storageaccount.primary_blob_endpoint
}

tags = {
    environment = var.project_id,
    user = var.user
}

}

上面的代码有一个 custom_data = file(pathexpand(var.ctr_cloud_init_file)) 实际上应该调用 yaml 文件。这里没有发生。

请找到我正在使用的 yaml 文件。 ctr_cloud_init_file

users:
- name: bluedata
  groups: [sudo, wheel]
  shell: /bin/bash
  sudo: ['ALL=(ALL) NOPASSWD:ALL']
  ssh-authorized-keys: 
  ### SSH Public Key Here
  - ssh-rsa ...

package_upgrade: true
packages: 
  - epel-release
  - firewalld
repo_update: true
repo_upgrade: all

要为Azure VM 提供Cloud-init 文件,直接加载文件不是一个好方法。对于Cloud-init,我建议你使用template_cloudinit_config,这是Cloud-init 的特殊提供程序,其功能集专门针对cloud-init 的功能。这是示例代码:

data "template_file" "script" {
  template = file("cloud-init")
}

# Render a multi-part cloud-init config making use of the part
# above, and other source files
data "template_cloudinit_config" "config" {
  gzip          = true
  base64_encode = true

  # Main cloud-config configuration file.
  part {
    filename     = "cloud-init"
    content_type = "text/cloud-config"
    content      = data.template_file.script.rendered
  }
}

resource "azurerm_virtual_machine" "controller-vm" {
...
os_profile {
    computer_name  = "controller.${var.project_id}.local"
    admin_username = var.user
    custom_data = data.template_cloudinit_config.config.rendered
}
...
}