Terraform 数据源 DataSource 无 Ubuntu 20.04

Posted

技术标签:

【中文标题】Terraform 数据源 DataSource 无 Ubuntu 20.04【英文标题】:Terraform DataSource DataSourceNone with Ubuntu 20.04 【发布时间】:2021-11-20 16:52:02 【问题描述】:

问题

使用 Packer 构建 Ubuntu 20.04(也尝试过 21.04)模板后,我无法在使用 Terraform 实例化模板时设置新设置。此配置与 Debian 10 没有问题。

打包机


    "builders": [
    
        "CPUs": "user `vm-cpu-num`",
        "RAM": "user `vm-mem-size`",
        "RAM_reserve_all": true,

        "boot_command": [
        "<esc><esc><esc><esc>e<wait>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "<del><del><del><del><del><del><del><del>",
        "linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net;seedfrom=http://.HTTPIP:.HTTPPort/\"<enter><wait>",
        "initrd /casper/initrd<enter><wait>",
        "boot<enter>",
        "<enter><f10><wait>"
      ],
      "boot_wait": "3s",
      "boot_order": "disk,cdrom",
      "cluster": "user `cluster`",
      "convert_to_template": "true",
      "datastore": "user `datastore`",
      "disk_controller_type": "pvscsi",
      "folder": "user `folder`",
      "guest_os_type": "ubuntu64Guest",
      "host": "user `host`",
      "http_directory": "http",
      "http_port_min" : "8000",
      "http_port_max" : "8000",
      "insecure_connection": "true",
      "iso_paths": ["user `iso-paths`"],
      "network_adapters": [
      
          "network": "user `network`",
          "network_card": "vmxnet3"
      
      ],
      "password": "user `vcenter-password`",
      "shutdown_command": "echo 'user `ssh-username`' | sudo -S shutdown -P now",
      "ssh_username": "user `ssh-username`",
      "ssh_password": "user `ssh-password`",
      "ssh_timeout": "20m",
      "ssh_handshake_attempts": "50",
      "storage": [
      
          "disk_size": "user `vm-disk-1-size`",
          "disk_thin_provisioned": true
      
      ],
      "type": "vsphere-iso",
      "username": "user `vcenter-username`",
      "vcenter_server": "user `vcenter-server`",
      "vm_name": "user `vm-name`"
    
    ],
    "provisioners": [
    
        "type": "shell",
        "inline": ["curl -sSL https://raw.githubusercontent.com/vmware/cloud-init-vmware-guestinfo/master/install.sh | sudo sh -"]
    ,
    
        "type": "shell",
        "inline": ["mkdir $HOME/.ssh && echo  user `ssh-key`  > $HOME/.ssh/authorized_keys"]
    ,
    
        "type": "ansible",
        "playbook_file": "../../ansible/packer.yml"
    ,
    
        "inline": [
        "echo 'Packer Template Build -- Complete'"
        ],
        "type": "shell"
    
    ]

打包器分发的用户数据文件

#cloud-config
autoinstall:
  version: 1
  locale: en_US
  packages:
    - open-vm-tools
    - python-is-python3
    - cloud-init
  keyboard:
    layout: en
    variant: us
  network:
    network:
      version: 2
      ethernets:
        ens192:
          dhcp4: true
          dhcp-identifier: mac
  storage:
    layout:
      name: lvm
  identity:
    hostname: ubuntu
    username: ubuntu
    password: $6$rounds=4096$8dkK1P/oE$2DGKKt0wLlTVJ7USY.0jN9du8FetmEr51yjPyeiR.zKE3DGFcitNL/nF1l62BLJNR87lQZixObuXYny.Mf17K1
  ssh:
    install-server: yes
  user-data:
    disable_root: false
  late-commands:
    - 'sed -i "s/dhcp4: true/&\n      dhcp-identifier: mac/" /target/etc/netplan/00-installer-config.yaml'
    - echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu

由 Ansible 供应商调用的清理脚本

#!/bin/sh

set -e

# cloud init: move files and change permissions
#mv /tmp/cloud-init/* /etc/cloud/cloud.cfg.d/
chown -R root:root /etc/cloud/cloud.cfg.d/

# cleanup cloud-init data
rm -rf /var/lib/cloud/*

ln -s /var/lib/cloud/instances /var/lib/cloud/instance

# cleanup cloud-init logs
rm -rf /var/log/cloud-init*

# cleanup tmp files
rm -rf /tmp/cloud-init

truncate -s 0 /etc/machine-id

rm /var/lib/dbus/machine-id

ln -s /etc/machine-id /var/lib/dbus/machine-id

地形

main.tf

resource "vsphere_virtual_machine" "vm" 
  name             = "$var.hostname"
  #resource_pool_id = "$data.vsphere_compute_cluster.cluster.resource_pool_id"
  resource_pool_id = "$data.vsphere_resource_pool.pool.id"
  datastore_id     = "$data.vsphere_datastore.datastore.id"

  num_cpus = var.vm_cpus
  memory   = var.vm_ram

  guest_id = "$data.vsphere_virtual_machine.template.guest_id"
  scsi_type = "$data.vsphere_virtual_machine.template.scsi_type"

  network_interface 
    network_id   = "$data.vsphere_network.network.id"
    adapter_type = "$data.vsphere_virtual_machine.template.network_interface_types[0]"
  

  disk 
    label            = "disk0"
    size             = "$var.os_disk_size"
    eagerly_scrub    = "$data.vsphere_virtual_machine.template.disks.0.eagerly_scrub"
    thin_provisioned = "$data.vsphere_virtual_machine.template.disks.0.thin_provisioned"
  

  dynamic "disk" 
    for_each = var.data_disk ? [1] :[]
    content 
      #All fields in the vsphere_virtual_disk resource are currently immutable and force a new resource if changed.
      label        = "disk1"
      attach       = true
      path         = "$vsphere_virtual_disk.data-disk[0].vmdk_path"
      unit_number  = 1
      datastore_id = "$data.vsphere_datastore.datastore.id"
    
  

  extra_config = 
    "guestinfo.metadata"          = base64gzip(templatefile("$path.module/templates/metadata.tmpl",  hostname = var.hostname ))
    "guestinfo.metadata.encoding" = "base64"
    "guestinfo.userdata"          = base64gzip(templatefile("$path.module/templates/userdata.tmpl", 
        hostname = var.hostname,
        domain   = var.domain
    ))
    "guestinfo.userdata.encoding" = "base64"
  

  clone 
    template_uuid = "$data.vsphere_virtual_machine.template.id"
  


userdata.tmpl

#cloud-config

preserve_hostname: false
manage_etc_hosts: true
prefer_fqdn_over_hostname: true
fqdn: "$hostname.$domain"

runcmd:
  - echo hello > /root/hello
  - touch /root/lol
Cloud-init v. 21.2-3-g899bfaa9-0ubuntu2~21.04.1 finished at Tue, 28 Sep 2021 15:55:02 +0000. Datasource DataSourceNone.  Up 19.12 seconds
2021-09-28 15:55:02,449 - cc_final_message.py[WARNING]: Used fallback datasource

【问题讨论】:

【参考方案1】:

问题是特定于VMware vSphere。修复它:

rm -rf /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg
rm -rf /etc/cloud/cloud.cfg.d/99-installer.cfg

【讨论】:

以上是关于Terraform 数据源 DataSource 无 Ubuntu 20.04的主要内容,如果未能解决你的问题,请参考以下文章

Terraform常用命令

terraform如何创建数据盘

使用 terraform 配置 bigquery 数据集

[译]Terraform – External Data Source

Terraform 使用数据源输出动态生成字符串

terraform : 到 bigquery 的数据流 pubsubtopics