Skip to content

Commit

Permalink
feat: switch from json to hcl files
Browse files Browse the repository at this point in the history
  • Loading branch information
papanito committed May 13, 2021
1 parent fe219f6 commit 4301d50
Show file tree
Hide file tree
Showing 38 changed files with 612 additions and 607 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -18,4 +18,5 @@ gradle.properties
wim

# packer variable files
/packerfiles/variables*.cfg
/packerfiles/variables_*.pkrvars.hcl
packer_cache/**
51 changes: 26 additions & 25 deletions README.md
@@ -1,49 +1,50 @@
# Example templates for Vagrant and Packer
## Example templates for Vagrant and Packer

## Intro
### Intro

There are already some existing GitHub projects providing templates to create machines with Packer. However I created this project for learning purposes . In addition to the packer documentation I also took some inspiration from these great projects:

* https://github.com/kaorimatz/packer-templates
* https://github.com/mwrock/packer-templates
* https://github.com/MattHodge/PackerTemplates
* https://github.com/jacqinthebox/packer-templates

## Structure

* [images](https://github.com/papanito/packer-vagrant/tree/master/images) - images for better illustration in the README.txt
* [packerfiles](https://github.com/papanito/packer-vagrant/tree/master/packerfiles) - json and cfg (variables) for packera
* [iso](https://github.com/papanito/packer-vagrant/tree/master/packerfiles/iso) - os specific variables like iso name and checksum
* [scripts](https://github.com/papanito/packer-vagrant/tree/master/packerfiles/scripts) - scripts for provisioning machines with packer
* [scripts](https://github.com/papanito/packer-vagrant/tree/master/scripts) - script to be used for provisioning
* [unattended](https://github.com/papanito/packer-vagrant/tree/master/unattended) - preseed files for Linux and Autounattend files for Windows
* [windows](https://github.com/papanito/packer-vagrant/tree/master/unattended/windows) - autounattended files for Windows
* [kaorimatz: packer-templates](https://github.com/kaorimatz/packer-templates)
* [mwrock: packer-templates](https://github.com/mwrock/packer-templates)
* [MattHodge: PackerTemplates](https://github.com/MattHodge/PackerTemplates)
* [jacqinthebox: packer-templates](https://github.com/jacqinthebox/packer-templates)
* [chef-bento: packer-templates](https://github.com/chef/bento/tree/master/packer_templates)

### Structure

* [images](./images) - images for better illustration in the README.txt
* [packerfiles](./packerfiles) - hcl (variables) for packera
* [iso](./packerfiles/iso) - os specific variables like iso name and checksum
* [scripts](./packerfiles/scripts) - scripts for provisioning machines with packer
* [scripts](./scripts) - script to be used for provisioning
* [unattended](./unattended) - preseed files for Linux and Autounattend files for Windows
* [windows](./unattended/windows) - autounattended files for Windows
* 10-ent-n-2016-LTSB - Windows 10 Enterprise Edition N2016 LTSB
* ...
* [linux](https://github.com/papanito/packer-vagrant/tree/master/unattended/linux) - answer files for Linux
* [linux](./unattended/linux) - answer files for Linux
* Ubuntu - Ubuntu specific files
* ...
* [vagrantfiles](https://github.com/papanito/packer-vagrant/tree/master/vagrantfiles)
* [vagrantfiles](./vagrantfiles)
* @os version@ -

Additional information can be found in the related subfolders

## Build
### Build

In order to build VMs with packer you can use the gradle script ```build.gradle``` as follows
In order to build VMs with packer you can use the gradle script `build.gradle` as follows

1. Add a gradle.properties file and define the following values. The values represent tokens which will be replaced in respective answer and packer files

* ```username=@username for user account to be created@```
* ```password=@password for user account 'username'@```
* ```rootpassword=@password for root/Administrator account@```
* `username=@username for user account to be created@`
* `password=@password for user account 'username'@`
* `rootpassword=@password for root/Administrator account@`

2. Define additional parameters

* ```builders=@packer builders to execute e.g. virtualbox-iso@```
* `builders=@packer builders to execute e.g. virtualbox-iso@`

3. Build machines by calling either `gradle buildLinux -PconfigFile=xxx` or `gradle buildWindows -PconfigFile=xxx`. You have to submit a config file (name form `packerfiles/iso` folder) which defines which distro / windows version to build

```gradle clean buildWindows -PconfigFile=windows_server_2016_standard_x64.cfg```
`gradle clean buildWindows -PconfigFile=windows_server_2016_standard_x64.hcl`

**Remark: Some characters may break the scripts e.g. using `$` will break `windows-base.ps1`**
50 changes: 16 additions & 34 deletions build.gradle
@@ -1,24 +1,9 @@
import org.apache.tools.ant.filters.ReplaceTokens

/*task build(type:Exec) {
println "Task build"
//executable = "dir"
//args = [ "packerfiles/iso" ]
//println "call 'gradle buildWindows -PconfigFile=<config file>' or 'gradle buildLinux -PconfigFile=<config file>'"
String osName = System.getProperty("os.name").toLowerCase();
if (!osName.contains("windows")) {
ext.packer = "packer-io"
}
}
*/
project.ext {
packer = 'packer'
}

task showConfigs(type:Exec) {
new File("${buildDir}/packerfiles/iso").eachDir{ println it.name }
}

task clean(type: Delete) {
delete rootProject.buildDir
}
Expand All @@ -27,18 +12,17 @@ task processResources(type: Copy) {
from('unattended') {
include '**/*.xml'
include '**/*.cfg'
into "unattended"
into "unattended"
}
from('packerfiles') {
include '**/*.json'
include '**/*.cfg'
include '**/*.hcl'
include '**/*.ps1'
include '**/*.sh'
into 'packerfiles'
}
filter(ReplaceTokens, tokens: [
'username': username,
'password' : password,
'username': username,
'password': password,
'rootpassword': rootpassword
])
into(buildDir)
Expand All @@ -49,9 +33,9 @@ task buildWindowsBase(type:Exec) {
workingDir = buildDir
executable = project.ext.packer
args = ["build",
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.cfg",
"./packerfiles/windows_1_base.json" ]
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.pkrvars.hcl",
"./packerfiles/windows_1_base.pkr.hcl" ]
commandLine
}

Expand All @@ -60,9 +44,9 @@ task buildWindowsUpdates(type:Exec, dependsOn: "buildWindowsBase") {
workingDir = buildDir
executable = project.ext.packer
args = ["build",
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.cfg",
"./packerfiles/windows_2_updates.json" ]
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.pkrvars.hcl",
"./packerfiles/windows_2_updates.pkr.hcl"]
commandLine
}

Expand All @@ -71,9 +55,9 @@ task buildWindowsPackages(type:Exec, dependsOn: "buildWindowsUpdates") {
workingDir = buildDir
executable = project.ext.packer
args = ["build",
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.cfg",
"./packerfiles/windows_3_package.json" ]
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.pkrvars.hcl",
"./packerfiles/windows_3_package.pkr.hcl" ]
commandLine
}

Expand All @@ -89,11 +73,9 @@ task buildLinux(type:Exec) {
workingDir = buildDir
executable = project.ext.packer
println "Building Linux $configFile at $workingDir"
showConfigs
args = ["build",
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.cfg",
"-only=$builders",
"./packerfiles/linux.json" ]
"-var-file=./packerfiles/iso/$configFile",
"-var-file=./packerfiles/variables.pkrvars.hcl",
"./packerfiles/linux.pkr.hcl" ]
commandLine
}
50 changes: 30 additions & 20 deletions packerfiles/README.md
@@ -1,27 +1,30 @@
# Introduction
## Introduction

For Linux boxes there is a .json file per OS version (e.g. Ubuntu 17.04 and Ubuntu 16.10).
For Windows boxes there are always at least 3 .json files - base, updates and package - per OS version (e.g. Windows 10, Windows 2016, ...) - this is inspired by [https://hodgkins.io/best-practices-with-packer-and-windows](https://hodgkins.io/best-practices-with-packer-and-windows).
For Linux boxes there is a `.hcl` file per OS version (e.g. Ubuntu 20.01 and Ubuntu 21.04).

For Windows boxes there are always at least 3 `.hcl` files - base, updates and package - per OS version (e.g. Windows 10, Windows 2016, ...) - this is inspired by [https://hodgkins.io/best-practices-with-packer-and-windows](https://hodgkins.io/best-practices-with-packer-and-windows).

For different variations of a system (e.g. Ubuntu Desktop 32-bit or 64-bit, Ubuntu Server, Windows 10 Enterprise 2016 LTSB, Windows 10 Enterprise 2016 N LTSB, ...) I have a separate config file with specific parameters like ISO name and Checksum. So a creation of a base image is called with appropriate -var-file. Example:

```bash
packer build -var-file=iso/windows_10_enterprise_2016_ltsb_en_n_x64.cfg windows_10.json
packer build -var-file=iso/ubuntu_16.10_x64_server.cfg ubuntu_16.10.json
packer build -var-file=iso/windows_10_enterprise_2016_ltsb_en_n_x64.pkrvars.hcl windows_10.hcl
packer build -var-file=iso/ubuntu_16.10_x64_server.pkrvars.hcl ubuntu_16.10.hcl
```

In additon certain variables defined in the packer files are set to null and therefore should be specified when calling ```packer build```. Mainly this are user credentials and mirrors for e.g. iso files - I keep them on an internal sever which is faster for download than over internet. There is an example config file which I usually copy and modify to my needs.
In addition certain variables defined in the packer files are set to null and therefore should be specified when calling `packer build`. Mainly this are user credentials and mirrors for e.g. iso files - I keep them on an internal sever which is faster for download than over internet. There is an example config file which I usually copy and modify to my needs.

```bash
packer build -var-file=iso/windows_10_enterprise_2016_ltsb_en_n_x64.cfg -var-file=myconfig.cfg windows_10.json
packer build -var-file=iso/ubuntu_16.10_x64_server.cfg -var-file=myconfig.cfg ubuntu_16.10.json
packer build -var-file=iso/windows_10_enterprise_2016_ltsb_en_n_x64.pkrvars.hcl -var-file=myconfig.pkrvars.hcl windows_10.hcl
packer build -var-file=iso/ubuntu_16.10_x64_server.pkrvars.hcl -var-file=myconfig.pkrvars.hcl ubuntu_16.10.hcl
```

**Remark:** All packer, answer and script files contain ant-like tokens for username and passwords. If you want to manually create the vms as mentioned above, please replace the respective tokens with the desired values. Otherwise, use the gradle script ```build.gradle``` as described [here](https://github.com/papanito/packer-vagrant/tree/master)
> **Remark:**
>
> All packer, answer and script files contain ant-like tokens for username and passwords. If you want to manually create the vms as mentioned above, please replace the respective tokens with the desired values. Otherwise, use the gradle script `build.gradle` as described [here](../README.md)
## Remarks for Linux
### Remarks for Linux

### boot_command
#### boot_command

The boot_command is essential to initiate the unattended installation. For Linux systems one has to modify the boot parameters and specify a pre-seed file either by an url or a file location. If taken from a file, is shall be mounted in the vm for example via the floppy

Expand Down Expand Up @@ -49,28 +52,35 @@ And then used in the boot command as follows

Alternatively one can specify an url as follows (replacing "preseed/file"):

```preseed/url=http://artifact-repo/{{user `preseed_name`}}```
```
preseed/url=http://artifact-repo/{{user `preseed_name`}}
```

The url can contain a host name or an ip but in case you use an IP you may enabled natdnshostresolver1 on virtualbox:

```["modifyvm", "{{.Name}}", "--natdnshostresolver1", "on"]```
```
["modifyvm", "{{.Name}}", "--natdnshostresolver1", "on"]
```

#### shutdown_command

### shutdown_command
As for any sudo command, it expects a password to be provided unless it is configured passwordless. If not you might have packer stuck at "Gracefully halting virtual machine" and then will timeout (see https://github.com/hashicorp/packer/issues/4813). So my shutdown_command looks like this:

```shutdown_command": "echo '{{user `pwd`}}' | {{user `shutdown_command`}}"```
```
shutdown_command": "echo '{{user `pwd`}}' | {{user `shutdown_command`}}"
```

## Remarks for Windows
### Remarks for Windows

TBD
ISO files and checksum info can be found at [Download Windows 10 Disc Image (ISO File)](https://www.microsoft.com/en-us/software-download/windows10ISO)

# Scripts
## Scripts

Scripts for provisioning machines with packer, more details at https://github.com/papanito/packer-vagrant/blob/master/packerfiles/scripts/README.md

# Common Errors and Troubleshooting
## Common Errors and Troubleshooting

## ==> virtualbox-iso: Waiting for WinRM to become available...
### ==> virtualbox-iso: Waiting for WinRM to become available...

This may have various reasons but most probably the username and/or password for the user defined in the packer files does not match the one in the answer file.
Usually for packer you would pass the password and username by variables as suggested [here](https://www.packer.io/docs/templates/user-variables.html) but you also have the username and password hard-coded in the unattended files.
6 changes: 0 additions & 6 deletions packerfiles/iso/ubuntu_20.04_x64_desktop.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions packerfiles/iso/ubuntu_20.04_x64_desktop.pkrvars.hcl
@@ -0,0 +1,3 @@
iso_name = "ubuntu-20.04.2.0-desktop-amd64.iso"
iso_checksum = "sha256:93bdab204067321ff131f560879db46bee3b994bf24836bb78538640f689e58f"
iso_mirror" = "http://releases.ubuntu.com/20.04/"
6 changes: 0 additions & 6 deletions packerfiles/iso/ubuntu_20.04_x64_server.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions packerfiles/iso/ubuntu_20.04_x64_server.pkrvars.hcl
@@ -0,0 +1,3 @@
iso_name = "ubuntu-20.04.2-live-server-amd64.iso"
iso_checksum = "sha256:d1f2bf834bbe9bb43faf16f9be992a6f3935e65be0edece1dee2aa6eb1767423"
iso_mirror = "http://releases.ubuntu.com/20.04/"
6 changes: 0 additions & 6 deletions packerfiles/iso/ubuntu_21.04_x64_desktop.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions packerfiles/iso/ubuntu_21.04_x64_desktop.pkrvars.hcl
@@ -0,0 +1,3 @@
iso_name = "ubuntu-21.04-desktop-amd64.iso"
iso_checksum = "sha256:fa95fb748b34d470a7cfa5e3c1c8fa1163e2dc340cd5a60f7ece9dc963ecdf88"
iso_mirror = "http://releases.ubuntu.com/21.04/"
6 changes: 0 additions & 6 deletions packerfiles/iso/ubuntu_21.04_x64_server.cfg

This file was deleted.

3 changes: 3 additions & 0 deletions packerfiles/iso/ubuntu_21.04_x64_server.pkrvars.hcl
@@ -0,0 +1,3 @@
iso_name = "ubuntu-21.04-live-server-amd64.iso"
iso_checksum = "sha256:e4089c47104375b59951bad6c7b3ee5d9f6d80bfac4597e43a716bb8f5c1f3b0"
iso_mirror = "http://releases.ubuntu.com/21.04/"
6 changes: 6 additions & 0 deletions packerfiles/iso/windows_10_20H2_de_x64.pkrvars.hcl
@@ -0,0 +1,6 @@
iso_name = "Win10_20H2_v2_German_x64.iso"
iso_checksum = "sha256:EC1BDCEA7CC1CDD5C31D1D73E3E8708A8E117767FC03F7123AE65A418035224E"
license_key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXX"
guest_os_type = "Windows10_64"
winversion = "10-20h2"
output = "win10-20h2-x64-de"
6 changes: 6 additions & 0 deletions packerfiles/iso/windows_10_20H2_en_x64.pkrvars.hcl
@@ -0,0 +1,6 @@
iso_name = "win10_20H2_v2_EnglishInternational_x64.iso"
iso_checksum = "sha256:BD9E41BDF9E23DCF5A0592F3BFE794584C80F1415727ED234E8929F656221836"
license_key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXX"
guest_os_type = "Windows10_64"
winversion = "10-20h2"
output = "win10-20h2-x64"
9 changes: 0 additions & 9 deletions packerfiles/iso/windows_10_enterprise_2016_ltsb_en_x64.cfg

This file was deleted.

@@ -0,0 +1,6 @@
iso_name = "en_windows_10_enterprise_2016_ltsb_x64_dvd_9059483.iso"
iso_checksum = "sha1:031ED6ACDC47B8F582C781B039F501D83997A1CF"
license_key = "WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY"
guest_os_type = "Windows10_64"
winversion = "10-ent-n-2016-ltsb"
output = "windows-10-enterprise-ltsb-x64"
9 changes: 0 additions & 9 deletions packerfiles/iso/windows_server_2016_standard_x64.cfg

This file was deleted.

7 changes: 7 additions & 0 deletions packerfiles/iso/windows_server_2016_standard_x64.pkrvars.hcl
@@ -0,0 +1,7 @@
iso_name = "en_windows_server_2016_x64_dvd_9327751"
iso_checksum = "sha256:91d7b2ebcff099b3557570af7a8a5cd6"
iso_checksum_type = "md5"
license_key = "WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY",
guest_os_type = "Windows10_64"
winversion = "server-2016-std"
output = "windows-server-2016-standard-x64"
9 changes: 0 additions & 9 deletions packerfiles/iso/windows_server_2016_storage_x64.cfg

This file was deleted.

7 changes: 7 additions & 0 deletions packerfiles/iso/windows_server_2016_storage_x64.pkrvars.hcl
@@ -0,0 +1,7 @@
iso_name = "en_windows_storage_server_2016_x64_dvd_9327790"
iso_checksum = "sha256:"
iso_checksum_type = "
license_key = "
guest_os_type = "Windows10_64"
winversion = "server-2016-storage"
output = "windows-server-2016-storage-x64"

0 comments on commit 4301d50

Please sign in to comment.