Skip to content

Instantly share code, notes, and snippets.

@mrdanielmartins
Last active October 19, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdanielmartins/4a9eb3771d489cd74797ed95f7a8f15c to your computer and use it in GitHub Desktop.
Save mrdanielmartins/4a9eb3771d489cd74797ed95f7a8f15c to your computer and use it in GitHub Desktop.
#### Packer
packer {
required_version = "~> 1.7.6"
required_plugins {
vsphere = {
version = "~> 1.0.1"
source = "github.com/hashicorp/vsphere"
# Guthub plugin repo https://github.com/hashicorp/packer-plugin-vsphere
}
windows-update = {
version = "~> 0.14.0"
source = "github.com/rgl/windows-update"
# Github plugin Repo https://github.com/rgl/packer-plugin-windows-update
}
}
}
#### Build
build {
sources = ["source.vsphere-iso.linux_alma"]
provisioner "shell" {
execute_command = "echo '${var.os_password_linux}' | {{.Vars}} sudo -S -E sh -eux '{{.Path}}'" # Deals with password prompts if not root.
scripts = [
"scripts/linux/ncpa.sh",
"scripts/linux/cockpit.sh",
"scripts/linux/yum_update.sh",
"scripts/linux/cleanup.sh",
"scripts/linux/banner.sh"
]
}
}
#### Source
source "vsphere-iso" "linux_alma" {
insecure_connection = true
username = var.vcenter_username
password = var.vcenter_password
vcenter_server = var.vcenter_server
cluster = var.vcenter_cluster
datacenter = var.vcenter_datacenter
host = var.vcenter_host
datastore = var.vcenter_datastore
folder = var.vm_folder
vm_name = var.vm_name
firmware = var.vm_firmware
vm_version = var.vm_hardware_version
guest_os_type = var.guest_os_type
CPUs = var.cpu_num
CPU_hot_plug = true
RAM = var.ram
RAM_hot_plug = true
RAM_reserve_all = false
cdrom_type = "sata"
boot_wait = "10s"
boot_order = "disk,cdrom"
notes = "Packer built on ${formatdate ("YYYY-MMM-DD 'at' hh:mm ZZZ", timestamp())}.\n Access Cockpit on port 9090 and NCPA on port 5693.\n Use >>> nmtui <<< if you want to set hostname, static address etc. in an easier fashion.\n Root password can be found in Gitlab CI/CD variables."
ssh_username = var.os_username
ssh_password = var.os_password_linux
ip_wait_timeout = "30m"
ip_settle_timeout = "15s"
ssh_timeout = "15m"
pause_before_connecting = "1m"
convert_to_template = true
content_library_destination {
library = var.content_library_destination
name = var.vm_name
ovf = true
#destroy = true
}
http_directory = "scripts/linux" # Directory to serve on HTTP
boot_command = [
"<up>",
"e",
"<down><down><end>",
" inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg",
"<enter><wait><leftCtrlOn>x<leftCtrlOff>"
]
network_adapters {
network = var.vm_network
network_card = var.network_card
}
disk_controller_type = [ "pvscsi" ]
storage {
disk_thin_provisioned = true
disk_size = var.disk_size
}
iso_paths = [
var.os_iso_path
]
}
#### Variables
variable "vcenter_username" {
type = string
default = null
}
variable "vcenter_password" {
type = string
default = null
sensitive = true
}
variable "os_username" {
type = string
description = "Local OS Username for WinRM or SSH etc., set in the pkvars"
default = null
}
variable "os_password_linux" {
description = "Local root password for login, WinRM or SSH etc."
type = string
default = null
sensitive = true
}
variable "vcenter_server" {
description = "vCenter server to connect."
type = string
default = null
}
variable "vcenter_cluster" {
description = "Which cluster to select from the vCenter."
type = string
default = null
}
variable "vcenter_datacenter" {
description = "Which datacentre to select from the vCenter cluster."
type = string
default = null
}
variable "vcenter_host" {
description = "Which ESXi host to select from the vCenter datacentre."
type = string
default = null
}
variable "vcenter_datastore" {
description = "Which datastore to select from the ESXI host."
type = string
default = null
}
variable "vm_network" {
type = string
default = null
}
variable "vm_folder" {
description = "The vCenter folder to store the template."
default = null
}
variable "vm_name" {
description = "The name of the virtual machine in vCenter."
default = null
}
variable "cpu_num" {
type = number
default = 2
}
variable "ram" {
type = number
default = 4096
}
variable "disk_size" {
type = number
default = 40960
}
variable "vm_hardware_version" {
description = "Packer defaults to 9."
default = null
}
variable "vm_firmware"{
description = "Packer defaults to BIOS."
type = string
default = "efi"
}
variable "guest_os_type" {
description = "Packer defaults to guest OS type of other."
type = string
default = null
}
variable "network_card" {
description = "Packer defaults to adater type of e1000."
type = string
default = "vmxnet3"
}
variable "content_library_destination" {
type = string
default = null
}
variable "content_library_template_name" {
type = string
default = null
}
variable "auto_unattend" {
description = "The autounattend file used for automated OS isntalltion. For Windows this will be autounattend.xml and for Linux ks.cfg."
type = string
default = null
}
variable "os_iso_path" {
description = "ISO path for OS unattendeded installs."
default = null
}
variable "vmtools_iso_path" {
description = "ISO Path for VMware Tools Windows exe. Used for drivers, performance etc."
type = string
default = null
}
#### kickstart.cfg
# Install new system using the first optical drive
cdrom
# Perform kickstart install in text mode (optional) (graphical by default)
text
# Accept EULA without user interaction (optional)
eula --agreed
# Set language to use during installation and the default language to use on the installed system (required)
lang en_GB.UTF-8
# Set system keyboard type or layout (required)
keyboard --xlayouts='gb'
# Configure network information for target system and activate network devices in the installer environment (optional)
# --device device to be activated and or configured with the network command
# --onboot enable device at a boot time.
# --bootproto method to obtain networking configuration for device (default dhcp).
# --activate activate this device in the installation environment.
# --noipv6 disable IPv6 on this device.
# --hostname host name for the installed system.
# Static IP configuration example:
network --bootproto=dhcp --noipv6
# Root password (required)
# One way encrypted hash
rootpw --plaintext bahBahNah
# Configure firewall settings for the system (optional)
# firewall --enabled --ssh
# --enabled reject incoming connections that are not in response to outbound requests.
# --ssh allow sshd service through the firewall.
# --disabled do not configure any iptables rules.
#firewall --disabled
# Set the system time zone (required)
timezone Europe/London --isUtc
# Ignore other disks and only use first drive. Without this can break kickstart if muliple disks are detected.
ignoredisk --only-use=sda
# Create partitions automatically, the automatically created partitions are:
# a root (/) partition (1 GB or larger)
# a swap partition
# an appropriate /boot partition for the architecture
# On large enough drives (50 GB and larger), this also creates a /home partition
autopart
# Initialize the first drive (optional)
# --all erases all partitions from the system.
# --initlabel initializes a disk (or disks) by creating a default disk label for all disks in their respective architecture that have been designated for formatting
clearpart --drives=sda --initlabel --drives=sda
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
# Reboot after the installation is complete (optional)
# --eject attempt to eject CD or DVD media before rebooting
reboot --eject
# Install packages.
%packages --ignoremissing
#@^minimal-environment
@core --nodefaults
kexec-tools
perl
open-vm-tools
bind-utils
unzip
curl
nano
net-tools
traceroute
wget
net-snmp
cockpit
NetworkManager-tui
chrony
expect
%end
# Post-install commands.
%post
sudo yum upgrade -y
%end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment