Saturday, May 18, 2024
HomePowershellPacker the way to use it

Packer the way to use it


Packer is an open-source software that permits the standardization of our photographs for a number of platforms with a single template. It permits us a easy and declarative automation to create our reference photographs or our golden photographs.

IAC

It comes within the type of a single executable accessible on a number of platforms.

Platforms Structure
MacOs Amd64 Arm64
Home windows 386 Amd64
Linux 386 Amd64 Arm Arm64

A small particularity for the Home windows world it will likely be vital so as to add an setting variable to specify the trail of the binary

For the format to make use of it is strongly recommended to make use of the brand new HCL format for Hashicorp Configuration Language. It’s extremely really useful to make use of it, nevertheless the JSON format stays accessible however for the way lengthy.

supply "amazon-ebs" "predominant" {
  ami_name = "main-ami"
}

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block physique
  <IDENTIFIER> = <EXPRESSION> # Argument
}

{
  "kind": "amazon-ebs",
  "access_key": "...",
  "secret_key": "..."
}

The benefit of utilizing the HCL language which is supported by packer since model 1.6 is the truth that HCP Packer makes use of the HCl format to be subsequently interpreted by Terraform which makes use of this identical format.

There are a handful of phrases used all through the Packer. Fortunately, there are comparatively few.

  • Artifacts are the outcomes of a single construct, and are normally a set of IDs or recordsdata to symbolize a machine picture. Each builder produces a single artifact. For example, within the case of the Amazon EC2 builder, the artifact is a set of AMI IDs (one per area). For the VMware builder, the artifact is a listing of recordsdata comprising the created digital machine.

  • Builds are a single activity that finally produces a picture for a single platform. A number of builds run in parallel. Instance utilization in a sentence: “The Packer construct produced an AMI to run our internet utility.” Or: “Packer is working the builds now for VMware, AWS, and VirtualBox.”

  • Builders are parts of Packer which are capable of create a machine picture for a single platform. Builders learn in some configuration and use that to run and generate a machine picture. A builder is invoked as a part of a construct with the intention to create the precise ensuing photographs. Instance builders embody VirtualBox, VMware, and Amazon EC2.

  • Information Sources are parts of Packer that fetch information from exterior Packer and make it accessible to make use of throughout the template. Instance of information sources embody Amazon AMI, and Amazon Secrets and techniques Supervisor.

  • Publish-processors are parts of Packer that take the results of a builder or one other post-processor and course of that to create a brand new artifact. Examples of post-processors are compress to compress artifacts, add to add artifacts, and many others.

  • Provisioners are parts of Packer that set up and configure software program inside a working machine previous to that machine being was a static picture. They carry out the main work of constructing the picture comprise helpful software program. Instance provisioners embody shell scripts, Chef, Puppet,PowerShell, Command Line, Scripts. and many others.

  • Templates are both HCL or JSON recordsdata which outline a number of builds by configuring the varied parts of Packer. Packer is ready to learn a template and use that info to create a number of machine photographs in parallel.

Create your individual VMware vSphere Template with Packer.

With packer we’ve to separate as a lot as doable our info in a number of recordsdata that can comprise the mandatory info, linked to our personal or public cloud setting.

We may have the next recordsdata:

  • vSphere.pkvars.hcl => credential for my personal cloud and settings.
  • construct.pkvars.hcl => the default person for all my builds
  • widespread.pkvars.hcl => the default setting to export my artifacts , content material Library , Template, OVA or OVF File

  • vSphere.pkvars.hcl
/*
    DESCRIPTION:
    VMware vSphere variables used for all builds.
    - Variables are use by the supply blocks.
*/

// vSphere Credentials
vsphere_endpoint            = "SCO-LABO-VCSA"
vsphere_username            = "packer_vsphere@area.loc"
vsphere_password            = "P@$$w0rd!VMware123!."
vsphere_insecure_connection = false

// vSphere Settings
vsphere_datacenter = "Datacenter"
vsphere_cluster    = "SCO_LABO"
vsphere_datastore  = "FREENAS1_VOL1"
vsphere_network    = "Packer_DHCP"
vsphere_folder     = "Packer Pattern"
/*
    DESCRIPTION:
    Construct account variables used for all builds.
    - Variables are handed to and utilized by visitor working system configuration recordsdata (e.g., ks.cfg, autounattend.xml).
    - Variables are handed to and utilized by configuration scripts.
*/

// Default Account Credentials
build_username           = "3ICSAdmin"
build_password           = "3Ics@dmin!."
build_password_encrypted = "$6$jqoxlQyW2ROxsLK4$P3gGp2Bf0FCSLAZTNaE/LZBqSsvVAYZNPNwTehXZbMAhRJdKOjNqqyUWVUTwql9CnhHUlpiluWzHlR2DpUKD4/"
/*
    DESCRIPTION:
    Widespread variables used for all builds.
    - Variables are use by the supply blocks.
*/

// Digital Machine Settings based on your environnement
common_vm_version           = 16
common_tools_upgrade_policy = true
common_remove_cdrom         = true

// Template and Content material Library Settings
common_template_conversion         = true
common_content_library_name        = "3ICS_Library"
common_content_library_ovf         = true
common_content_library_destroy     = true
common_content_library_skip_export = false

// Detachable Media Settings
common_iso_datastore = "FREENAS2_Vol1"

// Boot and Provisioning Settings
common_data_source      = "http"
common_http_ip          = null
common_http_port_min    = 8000
common_http_port_max    = 8099
common_ip_wait_timeout  = "20m"
common_shutdown_timeout = "15m"

And now one pattern for Home windows Server 2022

/*
    DESCRIPTION:
    Microsoft Home windows Server 2022 template utilizing the Packer Builder for VMware vSphere (vsphere-iso).
*/

//  BLOCK: packer
//  The Packer configuration.

packer {
  required_version = ">= 1.8.0"
  required_plugins {
    vsphere = {
      model = ">= v1.0.4"
      supply  = "github.com/hashicorp/vsphere"
    }
  }
  required_plugins {
    home windows-replace = {
      model = ">= 0.14.1"
      supply  = "github.com/rgl/windows-update"
    }
  }
}

//  BLOCK: locals
//  Defines the native variables.

locals {
  build_by      = "Constructed by: HashiCorp Packer ${packer.model}"
  build_date    = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
  build_version = formatdate("YY.MM", timestamp())
  manifest_date = formatdate("YYYY-MM-DD hh:mm:ss", timestamp())
  manifest_path = "${path.cwd}/manifests/"
}

//  BLOCK: supply
//  Defines the builder configuration blocks.

supply "vsphere-iso" "windows-server-standard-core" {

  // vCenter Server Endpoint Settings and Credentials
  vcenter_server      = var.vsphere_endpoint
  username            = var.vsphere_username
  password            = var.vsphere_password
  insecure_connection = var.vsphere_insecure_connection

  // vSphere Settings
  datacenter = var.vsphere_datacenter
  cluster    = var.vsphere_cluster
  datastore  = var.vsphere_datastore
  folder     = var.vsphere_folder

  // Digital Machine Settings
  guest_os_type        = var.vm_guest_os_type
  vm_name              = "${var.vm_guest_os_family}-${var.vm_guest_os_name}-${var.vm_guest_os_version}-${var.vm_guest_os_edition_standard}-${var.vm_guest_os_experience_core}-v${native.build_version}"
  firmware             = var.vm_firmware
  CPUs                 = var.vm_cpu_sockets
  cpu_cores            = var.vm_cpu_cores
  CPU_hot_plug         = var.vm_cpu_hot_add
  RAM                  = var.vm_mem_size
  RAM_hot_plug         = var.vm_mem_hot_add
  cdrom_type           = var.vm_cdrom_type
  disk_controller_type = var.vm_disk_controller_type
  storage {
    disk_size             = var.vm_disk_size
    disk_thin_provisioned = var.vm_disk_thin_provisioned
  }
  network_adapters {
    community      = var.vsphere_network
    network_card = var.vm_network_card
  }
  vm_version           = var.common_vm_version
  remove_cdrom         = var.common_remove_cdrom
  tools_upgrade_policy = var.common_tools_upgrade_policy
  notes                = "Model: v${native.build_version}nConstructed on: ${native.build_date}n${native.build_by}"

  // Detachable Media Settings
  iso_paths    = ["[${var.common_iso_datastore}] ${var.iso_path}/${var.iso_file}", "[] /vmimages/tools-isoimages/${var.vm_guest_os_family}.iso"]
  iso_checksum = "${var.iso_checksum_type}:${var.iso_checksum_value}"
  cd_files = [
    "${path.cwd}/scripts/${var.vm_guest_os_family}/",
    "${path.cwd}/certificates/"
  ]
  cd_content = {
    "autounattend.xml" = templatefile("${abspath(path.root)}/information/autounattend.pkrtpl.hcl", {
      build_username       = var.build_username
      build_password       = var.build_password
      vm_inst_os_language  = var.vm_inst_os_language
      vm_inst_os_keyboard  = var.vm_inst_os_keyboard
      vm_inst_os_image     = var.vm_inst_os_image_standard_core
      vm_inst_os_kms_key   = var.vm_inst_os_kms_key_standard
      vm_guest_os_language = var.vm_guest_os_language
      vm_guest_os_keyboard = var.vm_guest_os_keyboard
      vm_guest_os_timezone = var.vm_guest_os_timezone
    })
  }

  // Boot and Provisioning Settings
  http_port_min    = var.common_http_port_min
  http_port_max    = var.common_http_port_max
  boot_order       = var.vm_boot_order
  boot_wait        = var.vm_boot_wait
  boot_command     = var.vm_boot_command
  ip_wait_timeout  = var.common_ip_wait_timeout
  shutdown_command = var.vm_shutdown_command
  shutdown_timeout = var.common_shutdown_timeout

  // Communicator Settings and Credentials
  communicator   = "winrm"
  winrm_username = var.build_username
  winrm_password = var.build_password
  winrm_port     = var.communicator_port
  winrm_timeout  = var.communicator_timeout

  // Template and Content material Library Settings
  convert_to_template = var.common_template_conversion
  dynamic "content_library_destination" {
    for_each = var.common_content_library_name != null ? [1] : []
    content material {
      library     = var.common_content_library_name
      description = "Model: v${native.build_version}nConstructed on: ${native.build_date}n${native.build_by}"
      ovf         = var.common_content_library_ovf
      destroy     = var.common_content_library_destroy
      skip_import = var.common_content_library_skip_export
    }
  }
}

//  BLOCK: construct
//  Defines the builders to run, provisioners, and post-processors.

construct {
  sources = [
    "source.vsphere-iso.windows-server-standard-core",
  ]

  provisioner "powershell" {
    environment_vars = [
      "BUILD_USERNAME=${var.build_username}"
    ]
    elevated_user     = var.build_username
    elevated_password = var.build_password
    scripts           = formatlist("${path.cwd}/%s", var.scripts)
  }

  provisioner "powershell" {
    elevated_user     = var.build_username
    elevated_password = var.build_password
    inline            = var.inline
  }

  provisioner "windows-update" {
    pause_before    = "30s"
    search_criteria = "IsInstalled=0"
    filters = [
      "exclude:$_.Title -like '*VMware*'",
      "exclude:$_.Title -like '*Preview*'",
      "exclude:$_.Title -like '*Defender*'",
      "exclude:$_.InstallationBehavior.CanRequestUserInput",
      "include:$true"
    ]
    restart_timeout = "120m"
  }

  put up-processor "manifest" {
    output     = "${native.manifest_path}${native.manifest_date}.json"
    strip_path = true
    strip_time = true
    custom_data = {
      build_username           = var.build_username
      build_date               = native.build_date
      build_version            = native.build_version
      common_data_source       = var.common_data_source
      common_vm_version        = var.common_vm_version
      vm_cpu_cores             = var.vm_cpu_cores
      vm_cpu_sockets           = var.vm_cpu_sockets
      vm_disk_size             = var.vm_disk_size
      vm_disk_thin_provisioned = var.vm_disk_thin_provisioned
      vm_firmware              = var.vm_firmware
      vm_guest_os_type         = var.vm_guest_os_type
      vm_mem_size              = var.vm_mem_size
      vm_network_card          = var.vm_network_card
      vsphere_cluster          = var.vsphere_cluster
      vsphere_datacenter       = var.vsphere_datacenter
      vsphere_datastore        = var.vsphere_datastore
      vsphere_endpoint         = var.vsphere_endpoint
      vsphere_folder           = var.vsphere_folder
      vsphere_iso_path         = "[${var.common_iso_datastore}] ${var.iso_path}/${var.iso_file}"
    }
  }
}

you’ll discover that within the HCL file above all of the values are variables and are saved in a separate file. you additionally want a file that can describe all of the variables with their varieties.

Lastly it’s now doable to automate the creation of your templates inside your personal or public cloud setting

On the following half I’ll talk about Terraform and VWware Residence Lab 😉.

Thank’s for studying.🤗

Written by Jérôme Bezet-Torres @JM2K69.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments