josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
538 stars 154 forks source link

[Help] Unable to create usable Ubuntu 20.04.4 VM #174

Closed MarkLFT closed 2 years ago

MarkLFT commented 2 years ago

I am trying to create some templates to allow me to build an Ubuntu 20.04.4 server. I am using the latest Ubuntu cloud OVA to build from, which I believe has the required cloud-init and vmware guest data functionality included.

The VM is being created, however I am unable to login to the VM, and from watching the terminal, it does not appear that the cloud-init is running.

Can someone please help me with what I am doing wrong? Perhaps with some working files that work with Ubuntu 20.04.4.

Many thanks.

Terraform files below:

main.tf

provider "esxi" { esxi_hostname = var.esxi_hostname esxi_hostport = var.esxi_hostport esxi_hostssl = var.esxi_hostssl esxi_username = var.esxi_username esxi_password = var.esxi_password }

data "template_file" "userdata_default" { template = file("userdata.cfg") vars = { HOSTNAME = var.guest_name ssh_public_key = var.user_ssh } }

resource "esxi_guest" "Default" { guest_name = var.guest_name

disk_store = var.disk_store boot_disk_type = var.disk_type boot_disk_size = var.disk_size boot_firmware = "BIOS"

memsize = var.memsize numvcpus = var.numvcpus

guestos = var.guest_os

ovf_source = var.ovf_source power = var.power

network_interfaces { virtual_network = "VM Network" }

tried both with and without these ovf properties.

ovf_properties { key = "password" value = var.vm_password }

ovf_properties { key = "hostname" value = var.guest_name }

guestinfo = { "userdata.encoding" = "gzip+base64" "userdata" = base64gzip(data.template_file.userdata_default.rendered) } }

userdate.cfg

cloud-config

packages:

runcmd:

final_message: "The system is finally up, after $UPTIME seconds"

versions.tf terraform { required_version = ">= 0.13" required_providers { esxi = { source = "registry.terraform.io/josenk/esxi" } } }

variables.tf

variable "esxi_hostname" { default = "192.168.88.25" }

variable "esxi_hostport" { default = "22" }

variable "esxi_hostssl" { default = "443" }

variable "esxi_username" { default = "root" }

variable "esxi_password" {

Unspecified will prompt

}

variable "guest_name" { type = string default = "rmserver" }

variable "disk_store" { default = "WD240G" type = string }

variable "disk_size" { default = 20 type = number }

variable "disk_type" { default = "thin" type = string }

variable "numvcpus" { default = 2 type = number }

variable "memsize" { default = 2048 type = number }

variable "power" { default = "on" }

variable "guest_os" { default = "ubuntu-64" }

variable "ovf_source" { default = "C:\Users\mark\Downloads\focal-server-cloudimg-amd64.ova" }

variable "vm_password" { default = "RandomPassword" }

Desktop (please complete the following information):

MarkLFT commented 2 years ago

Just to let you know, with lots of trawling the internet, and borrowing ideas forma few places, I have it working. I am just fine tuning my build files, once done, I will post them for others to use if it helps.

nb25186 commented 2 years ago

@MarkLFT i'd be grateful if you could share. cloud-init is also not doing anything to me, although I installed on the original vm, and did there the could-init clean command

MarkLFT commented 2 years ago

@nb25186 Below is the config file I used, this also installs Docker and KIND. But you can remove those if not required.

Hope this helps.

#cloud-config

groups:
  - docker

system_info:
  default_user:
    name: joe
    groups: [docker]
    lock_passwd: false
    plain_text_passwd: ${default_password}
  ssh_svcname: sshd

ssh_pwauth: true

chpasswd:
  expire: false

ssh_authorized_keys:
  - ${ssh_public_key}

disable_root: true

hostname: ${hostname}
fqdn: ${hostname}.local

timezone: Asia/Singapore

manual_cache_clean: False

apt_pipelining: False

apt:
  preserve_sources_list: true
  conf: | 
    APT {
      Get {
        Assume-Yes "true";
        Fix-Broken "true";
      };
    };

package_update: true
package_upgrade: true
package_reboot_if_required: true

ssh:
  allow-pw: true
  install-server: true

packages:
  - apt-transport-https
  - ca-certificates
  - curl
  - gnupg
  - lsb-release
  - software-properties-common

runcmd:
  - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
  - add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  - apt-get update -y
  - apt-get install -y docker-ce docker-ce-cli containerd.io
  - systemctl start docker
  - systemctl enable docker
  - curl -Lo ./kind "https://github.com/kubernetes-sigs/kind/releases/download/v0.12.0/kind-linux-amd64"
  - chmod +x ./kind
  - mv ./kind /usr/local/bin/kind
  - curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  - install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
  - apt autoremove --purge
  - apt clean

write_files:
  - path: /etc/sysctl.d/enabled_ipv4_forwarding.conf
    content: |
      net.ipv4.conf.all.forwarding=1  

final_message: "The system is finally up, after $UPTIME seconds"
nb25186 commented 2 years ago

Thank you @MarkLFT , unfortunately the config is still not being executed. The main difference I see is that I am doing a clone_from_vm , the catch may be there.

MarkLFT commented 2 years ago

I think that may your issue. Whilst cloud-init is run every time a Linux server starts, many of the parts are ignored on all except the first boot during installation. With a clone operation, that first boot happened on the clone source, and does not happen again on the clone destination. This is a Linux issue, not a terraform or this provider issue.

If you need to do something to the clone, you will need a remote execute type action.

josenk commented 2 years ago

On the source, you need to clean up cloud-init to believe it will need to run first-boot. You will need to remember to do it every time you boot the source system.

https://cloudinit.readthedocs.io/en/latest/topics/boot.html#first-boot-determination

When that source system is cloned, the clones will then run first-boot.