|
1 | 1 | # Copyright (c) HashiCorp, Inc. |
2 | 2 | # SPDX-License-Identifier: MPL-2.0 |
3 | 3 |
|
4 | | -# "timestamp" template function replacement |
| 4 | +packer { |
| 5 | + required_plugins { |
| 6 | + vsphere = { |
| 7 | + version = "~> 1" |
| 8 | + source = "github.com/hashicorp/vsphere" |
| 9 | + } |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +variable "vm_name_prefix" { |
| 14 | + type = string |
| 15 | + default = "alpine" |
| 16 | + description = "Prefix for naming the virtual machine." |
| 17 | +} |
| 18 | + |
| 19 | +variable "guest_os_type" { |
| 20 | + type = string |
| 21 | + default = "other5xLinux64Guest" |
| 22 | + description = "The type of guest OS to configure for the virtual machine." |
| 23 | +} |
| 24 | + |
| 25 | +variable "username" { |
| 26 | + type = string |
| 27 | + default = "administrator@vsphere.local" |
| 28 | + sensitive = true |
| 29 | + description = "The username for authenticating with the vCenter instance." |
| 30 | +} |
| 31 | + |
| 32 | +variable "password" { |
| 33 | + type = string |
| 34 | + default = "VMw@re1!" |
| 35 | + sensitive = true |
| 36 | + description = "The password for authenticating with the vCenter instance." |
| 37 | +} |
| 38 | + |
| 39 | +variable "vcenter_server" { |
| 40 | + type = string |
| 41 | + default = "vcenter.example.com" |
| 42 | + description = "The vCenter instance used for managing the ESX host." |
| 43 | +} |
| 44 | + |
| 45 | +variable "host" { |
| 46 | + type = string |
| 47 | + default = "esx-01.example.com" |
| 48 | + description = "The ESX host where the virtual machine will be built." |
| 49 | +} |
| 50 | + |
| 51 | +variable "insecure_connection" { |
| 52 | + type = bool |
| 53 | + default = true |
| 54 | + description = "Set to true to allow insecure connections to the vCenter instance." |
| 55 | +} |
| 56 | + |
| 57 | +variable "communicator" |
| 58 | + type = string |
| 59 | + default = "none" |
| 60 | +} |
| 61 | + |
5 | 62 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") } |
6 | 63 |
|
7 | | -# source blocks are analogous to the "builders" in json templates. They are used |
8 | | -# in build blocks. A build block runs provisioners and post-processors on a |
9 | | -# source. Read the documentation for source blocks here: |
10 | | -# https://www.packer.io/docs/templates/hcl_templates/blocks/source |
11 | | -source "vsphere-clone" "example_clone" { |
12 | | - communicator = "none" |
13 | | - host = "esxi-01.example.com" |
14 | | - insecure_connection = "true" |
15 | | - password = "VMw@re1!" |
16 | | - template = "alpine" |
17 | | - username = "administrator@vsphere.local" |
18 | | - vcenter_server = "vcenter.example.com" |
19 | | - vm_name = "alpine-clone-${local.timestamp}" |
20 | | -} |
21 | | - |
22 | | -# a build block invokes sources and runs provisioning steps on them. The |
23 | | -# documentation for build blocks can be found here: |
24 | | -# https://www.packer.io/docs/templates/hcl_templates/blocks/build |
| 64 | +source "vsphere-clone" "example" { |
| 65 | + communicator = var.communicator |
| 66 | + username = var.username |
| 67 | + password = var.password |
| 68 | + vcenter_server = var.vcenter_server |
| 69 | + host = var.host |
| 70 | + insecure_connection = var.insecure_connection |
| 71 | + vm_name = "${var.vm_name_prefix}-${local.timestamp}" |
| 72 | + template = var.vm_name_prefix |
| 73 | +} |
| 74 | + |
25 | 75 | build { |
26 | | - sources = ["source.vsphere-clone.example_clone"] |
| 76 | + sources = ["source.vsphere-clone.example"] |
27 | 77 |
|
28 | 78 | } |
0 commit comments