├── http ├── meta-data └── user-data ├── .gitignore ├── .devcontainer └── devcontainer.json ├── ubuntu-2004.pkrvars.hcl ├── ubuntu-2204.pkrvars.hcl ├── variables.auto.pkrvars.hcl.sample ├── setup └── setup.sh ├── README.md └── ubuntu.pkr.hcl /http/meta-data: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | variables.auto.pkrvars.hcl 2 | packer_cache/ 3 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "image": "andif888/docker-pataz:v0.9.2" 3 | } 4 | -------------------------------------------------------------------------------- /ubuntu-2004.pkrvars.hcl: -------------------------------------------------------------------------------- 1 | boot_command = [ 2 | "", 3 | "", 4 | "/casper/vmlinuz ", 5 | "root=/dev/sr0 ", 6 | "initrd=/casper/initrd ", 7 | "autoinstall ", 8 | "ds=nocloud-net;", 9 | "" 10 | ] -------------------------------------------------------------------------------- /ubuntu-2204.pkrvars.hcl: -------------------------------------------------------------------------------- 1 | boot_command = [ 2 | "c", 3 | "linux /casper/vmlinuz --- autoinstall ds=\"nocloud-net\"", 4 | "", 5 | "initrd /casper/initrd", 6 | "", 7 | "boot", 8 | "" 9 | ] 10 | -------------------------------------------------------------------------------- /variables.auto.pkrvars.hcl.sample: -------------------------------------------------------------------------------- 1 | # Name or IP of you vCenter Server 2 | vsphere_server = "vcenter.demolab.com" 3 | 4 | # vsphere username 5 | vsphere_username = "administrator@vsphere.local" 6 | 7 | # vsphere password 8 | vsphere_password = "SomeSecurePassword" 9 | 10 | # vsphere datacenter name 11 | vsphere_datacenter = "datacenter1" 12 | 13 | # name or IP of the ESXi host 14 | vsphere_host = "esx1.demolab.com" 15 | 16 | # vsphere network 17 | vsphere_network = "VM Network" 18 | 19 | # vsphere datastore 20 | vsphere_datastore = "datastore1" 21 | 22 | # cloud_init files for unattended configuration for Ubuntu 23 | cloudinit_userdata = "./http/user-data" 24 | cloudinit_metadata = "./http/meta-data" 25 | 26 | # final clean up script 27 | shell_scripts = ["./setup/setup.sh"] 28 | 29 | # SSH username (created in user-data. If you change it here the please also adjust in ./html/user-data) 30 | ssh_username = "vagrant" 31 | 32 | # SSH password (created in autounattend.xml. If you change it here the please also adjust in ./html/user-data) 33 | ssh_password = "vagrant" 34 | -------------------------------------------------------------------------------- /setup/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo '> Cleaning all audit logs ...' 4 | if [ -f /var/log/audit/audit.log ]; then 5 | cat /dev/null > /var/log/audit/audit.log 6 | fi 7 | if [ -f /var/log/wtmp ]; then 8 | cat /dev/null > /var/log/wtmp 9 | fi 10 | if [ -f /var/log/lastlog ]; then 11 | cat /dev/null > /var/log/lastlog 12 | fi 13 | # Cleans SSH keys. 14 | echo '> Cleaning SSH keys ...' 15 | rm -f /etc/ssh/ssh_host_* 16 | # Sets hostname to localhost. 17 | echo '> Setting hostname to localhost ...' 18 | cat /dev/null > /etc/hostname 19 | hostnamectl set-hostname localhost 20 | # Cleans apt-get. 21 | echo '> Cleaning apt-get ...' 22 | apt-get clean 23 | # Cleans the machine-id. 24 | echo '> Cleaning the machine-id ...' 25 | truncate -s 0 /etc/machine-id 26 | rm /var/lib/dbus/machine-id 27 | ln -s /etc/machine-id /var/lib/dbus/machine-id 28 | 29 | # optional: cleaning cloud-init 30 | # echo '> Cleaning cloud-init' 31 | # rm -rf /etc/cloud/cloud.cfg.d/subiquity-disable-cloudinit-networking.cfg 32 | # rm -rf /etc/cloud/cloud.cfg.d/99-installer.cfg 33 | # echo 'datasource_list: [ VMware, NoCloud, ConfigDrive ]' | tee /etc/cloud/cloud.cfg.d/90_dpkg.cfg 34 | # /usr/bin/cloud-init clean 35 | -------------------------------------------------------------------------------- /http/user-data: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | autoinstall: 3 | version: 1 4 | early-commands: 5 | # Stop ssh for packer 6 | - sudo systemctl stop ssh 7 | locale: en_US 8 | keyboard: 9 | layout: en 10 | variant: us 11 | identity: 12 | hostname: ubuntu-server 13 | username: vagrant 14 | password: '$6$rounds=4096$5CU3LEj/MQvbkfPb$LmKEF9pCfU8R.dA.GemgE/8GT6r9blge3grJvdsVTMFKyLEQwzEF3SGWqAzjawY/XHRpWj4fOiLBrRyxJhIRJ1' 15 | ssh: 16 | install-server: yes 17 | allow-pw: yes 18 | storage: 19 | layout: 20 | name: direct 21 | apt: 22 | primary: 23 | - arches: [i386, amd64] 24 | uri: "http://de.archive.ubuntu.com/ubuntu/" 25 | packages: 26 | - apt-transport-https 27 | - ca-certificates 28 | - curl 29 | - gnupg-agent 30 | - software-properties-common 31 | - ufw 32 | - unzip 33 | - python3 34 | - python3-pip 35 | - sshpass 36 | user-data: 37 | disable_root: false 38 | late-commands: 39 | - sed -i -e 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /target/etc/ssh/sshd_config 40 | - sed -i -e 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /target/etc/ssh/sshd_config 41 | - echo 'vagrant ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/vagrant 42 | - curtin in-target --target=/target -- chmod 440 /etc/sudoers.d/vagrant 43 | - curtin in-target --target=/target -- apt-get update 44 | - curtin in-target --target=/target -- apt-get upgrade --yes 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # packer-ubuntu-vsphere-iso 2 | 3 | This repo builds automatically Ubuntu VM templates (Ubuntu 20.04, 22.04) for VMware vSphere environment using Hashicorp's Packer and downloads ISOs automatically. 4 | 5 | With this repo VM templates for the following Ubuntu systems can be built. 6 | 7 | - Ubuntu Server 20.04 8 | - Ubuntu Server 22.04 9 | 10 | You don't need do pre-download any Windows ISO. 11 | Ubuntu ISO files gets download automatically from public sources. 12 | 13 | ## How to use this repo 14 | 15 | ### Pre-requesites 16 | 17 | Download or `git clone https://github.com/andif888/packer-ubuntu-vsphere-iso.git` this repo and make sure you have [Packer](https://www.packer.io/downloads) Version 1.7.1 or later installed. If you don't know Packer, it's a single commandline binary which only needs to be on your `PATH`. 18 | 19 | ### Step 1: Adjust variables 20 | 21 | Rename the file [variables.auto.pkrvars.hcl.sample](variables.auto.pkrvars.hcl.sample) to `variables.auto.pkrvars.hcl` and adjust the variables for your VMware vSphere environment. Some documentation on each variable is inside the sample file. 22 | ```bash 23 | mv variables.auto.pkrvars.hcl.sample variables.auto.pkrvars.hcl 24 | nano variables.auto.pkrvars.hcl 25 | ``` 26 | 27 | ### Step 2: Init Packer 28 | 29 | Init Packer by using the following command. (Spot the dot at the end of the command!) 30 | ```bash 31 | packer init . 32 | ``` 33 | 34 | ### Step 3: Build a VM Template 35 | 36 | To build a VM template run one of the provided `build`-scripts. 37 | For example to build a Ubuntu Server 20.04 template run: 38 | ```bash 39 | ./build-2004.sh 40 | ``` 41 | If you are on a Windows machine then use the `build-*.ps1` files. 42 | 43 | 44 | ### Optional: Template default credentials 45 | 46 | the default credentials after a successful build are 47 | Username: `vagrant` 48 | Password: `vagrant` 49 | 50 | If you would like to change the default ćredentials before a packer build, then you need to edit the following files: 51 | 52 | - **variables.auto.pkrvars.hcl** 53 | - **user-data** (Line 13.14; Line 41) 54 | 55 | To generate an encypted password for [user-data](./html/user-data) use the following command: 56 | ```bash 57 | mkpasswd -m SHA-512 --rounds=4096 58 | ``` 59 | -------------------------------------------------------------------------------- /ubuntu.pkr.hcl: -------------------------------------------------------------------------------- 1 | packer { 2 | required_plugins { 3 | vsphere = { 4 | version = ">= 0.0.1" 5 | source = "github.com/hashicorp/vsphere" 6 | } 7 | } 8 | } 9 | 10 | variable "cpu_num" { 11 | type = number 12 | default = 2 13 | } 14 | 15 | variable "disk_size" { 16 | type = number 17 | default = 51200 18 | } 19 | 20 | variable "mem_size" { 21 | type = number 22 | default = 2048 23 | } 24 | 25 | variable "os_iso_checksum" { 26 | type = string 27 | default = "" 28 | } 29 | 30 | variable "os_iso_url" { 31 | type = string 32 | default = "" 33 | } 34 | 35 | variable "vsphere_datastore" { 36 | type = string 37 | default = "" 38 | } 39 | 40 | variable "vsphere_datacenter" { 41 | type = string 42 | default = "" 43 | } 44 | 45 | variable "vsphere_guest_os_type" { 46 | type = string 47 | default = "" 48 | } 49 | 50 | variable "vsphere_host" { 51 | type = string 52 | default = "" 53 | } 54 | 55 | variable "vsphere_password" { 56 | type = string 57 | default = "" 58 | sensitive = true 59 | } 60 | 61 | variable "vsphere_network" { 62 | type = string 63 | default = "" 64 | } 65 | 66 | variable "vsphere_server" { 67 | type = string 68 | default = "" 69 | } 70 | 71 | variable "vsphere_vm_name" { 72 | type = string 73 | default = "" 74 | } 75 | 76 | variable "vsphere_username" { 77 | type = string 78 | default = "" 79 | } 80 | 81 | variable "ssh_password" { 82 | type = string 83 | default = "" 84 | sensitive = true 85 | } 86 | 87 | variable "ssh_username" { 88 | type = string 89 | default = "" 90 | } 91 | 92 | variable "cloudinit_userdata" { 93 | type = string 94 | default = "" 95 | } 96 | 97 | variable "cloudinit_metadata" { 98 | type = string 99 | default = "" 100 | } 101 | 102 | variable "shell_scripts" { 103 | type = list(string) 104 | description = "A list of scripts." 105 | default = [] 106 | } 107 | 108 | variable "boot_command" { 109 | type = list(string) 110 | description = "Ubuntu boot command" 111 | default = [] 112 | } 113 | 114 | source "vsphere-iso" "ubuntu" { 115 | 116 | vcenter_server = var.vsphere_server 117 | host = var.vsphere_host 118 | username = var.vsphere_username 119 | password = var.vsphere_password 120 | insecure_connection = "true" 121 | datacenter = var.vsphere_datacenter 122 | datastore = var.vsphere_datastore 123 | 124 | CPUs = var.cpu_num 125 | RAM = var.mem_size 126 | RAM_reserve_all = true 127 | disk_controller_type = ["pvscsi"] 128 | guest_os_type = var.vsphere_guest_os_type 129 | iso_checksum = var.os_iso_checksum 130 | iso_url = var.os_iso_url 131 | cd_content = { 132 | "/meta-data" = file("${var.cloudinit_metadata}") 133 | "/user-data" = file("${var.cloudinit_userdata}") 134 | } 135 | cd_label = "cidata" 136 | 137 | network_adapters { 138 | network = var.vsphere_network 139 | network_card = "vmxnet3" 140 | } 141 | 142 | storage { 143 | disk_size = var.disk_size 144 | disk_thin_provisioned = true 145 | } 146 | 147 | vm_name = var.vsphere_vm_name 148 | convert_to_template = "true" 149 | communicator = "ssh" 150 | ssh_username = var.ssh_username 151 | ssh_password = var.ssh_password 152 | ssh_timeout = "30m" 153 | ssh_handshake_attempts = "100000" 154 | 155 | boot_order = "disk,cdrom,floppy" 156 | boot_wait = "3s" 157 | boot_command = var.boot_command 158 | shutdown_command = "echo '${var.ssh_password}' | sudo -S -E shutdown -P now" 159 | shutdown_timeout = "15m" 160 | 161 | configuration_parameters = { 162 | "disk.EnableUUID" = "true" 163 | } 164 | } 165 | 166 | build { 167 | sources = ["source.vsphere-iso.ubuntu"] 168 | 169 | provisioner "shell" { 170 | execute_command = "echo '${var.ssh_password}' | {{.Vars}} sudo -S -E bash '{{.Path}}'" 171 | environment_vars = [ 172 | "BUILD_USERNAME=${var.ssh_username}", 173 | ] 174 | scripts = var.shell_scripts 175 | expect_disconnect = true 176 | } 177 | 178 | } 179 | --------------------------------------------------------------------------------