Skip to content

Commit

Permalink
refactor: ubuntu cloud-init (#843)
Browse files Browse the repository at this point in the history
Refactors the Ubuntu image builds **not** to use cloud-init by default. 

Set `vm_guest_os_cloudinit` to `true` to enable cloud-init on the machine image

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Mar 28, 2024
1 parent bcff257 commit fcfce8f
Show file tree
Hide file tree
Showing 44 changed files with 193 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ variables:

.packer-build:
stage: build
#tags:
#tags:
# - packer
before_script:
- "packer init \"$INPUT_PATH\""
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
- Adds Ansible integration for Windows Server 2025, 2022, and 2019 images. [GH-801](https://github.com/vmware-samples/packer-examples-for-vsphere/pull/801)
- Adds Ansible integration for Windows 11 and 10 images. [GH-801](https://github.com/vmware-samples/packer-examples-for-vsphere/pull/801)

:wrench: **Refactor**:

- Refactors the Ubuntu image builds **not** to use cloud-init by default. Set `vm_guest_os_cloudinit` to `true` to enable cloud-init on the machine image. [GH-#](https://github.com/vmware-samples/packer-examples-for-vsphere/pull/#)

:sweat_drops: **Chore**:

- Updates `required_versions` for `packer` to `>= 1.10.0`. [GH-828](https://github.com/vmware-samples/packer-examples-for-vsphere/pull/828)
Expand Down
12 changes: 12 additions & 0 deletions ansible/roles/clean/tasks/debian.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
---
# Tasks for setting custom facts.
- name: "Setting custom facts."
set_fact:
enable_cloudinit: "{{ enable_cloudinit | default('false') }}"

# Tasks for removing the cloud-init package.
- name: "Removing the cloud-init package."
apt:
name: cloud-init
state: absent
when: ansible_distribution == 'Ubuntu' and enable_cloudinit == 'false'

# Tasks to clean the audit logs.
- name: "Cleaning the audit logs."
file:
Expand Down
10 changes: 9 additions & 1 deletion ansible/roles/configure/tasks/debian.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
# Tasks for setting custom facts.
- name: "Setting custom facts."
set_fact:
enable_cloudinit: "{{ enable_cloudinit | default('false') }}"

# Tasks for configuring SSH for public key authentication.
- name: "Configuring SSH for Public Key Authentication."
lineinfile:
Expand Down Expand Up @@ -28,6 +33,9 @@
# Tasks for configuring cloud-init.
- name: "Configuring cloud-init."
block:
- name: "Message: Configuring cloud-init"
debug:
msg: "Configuring cloud-init"
- file:
path: "{{ item }}"
state: absent
Expand All @@ -42,7 +50,7 @@
- copy:
content: 'datasource_list: [ VMware, OVF, None ]'
dest: /etc/cloud/cloud.cfg.d/90_dpkg.cfg
when: ansible_distribution == 'Ubuntu'
when: ansible_distribution == 'Ubuntu' and enable_cloudinit == 'true'

# Tasks for setting SSH keys to regenerate.
- name: "Setting SSH keys to regenerate."
Expand Down
23 changes: 14 additions & 9 deletions ansible/roles/users/tasks/linux.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
---
# Tasks for setting custom facts.
- name: "Setting custom facts."
set_fact:
enable_cloudinit: "{{ enable_cloudinit | default('false') }}"

# Tasks for creating the local group for Ansible.
- name: "Creating the local group for Ansible."
group:
name: "{{ ANSIBLE_USERNAME }}"
name: "{{ ansible_username }}"

# Tasks for creating the sudo group.
- name: "Creating the sudo group."
Expand All @@ -12,8 +17,8 @@
# Tasks for creating the local user for Ansible.
- name: "Creating the local user for Ansible."
user:
name: "{{ ANSIBLE_USERNAME }}"
group: "{{ ANSIBLE_USERNAME }}"
name: "{{ ansible_username }}"
group: "{{ ansible_username }}"
groups: sudo
password: '!'
shell: /bin/bash
Expand All @@ -24,10 +29,10 @@
user: "{{ item.user }}"
key: "{{ item.key }}"
loop:
- user: "{{ ANSIBLE_USERNAME }}"
key: "{{ ANSIBLE_KEY }}"
- user: "{{ BUILD_USERNAME }}"
key: "{{ BUILD_KEY }}"
- user: "{{ ansible_username }}"
key: "{{ ansible_key }}"
- user: "{{ build_username }}"
key: "{{ build_key }}"
no_log: true

# Tasks for managing sudoers.d for the local users.
Expand All @@ -37,6 +42,6 @@
user: "{{ item }}"
commands: ALL
loop:
- "{{ BUILD_USERNAME }}"
- "{{ ANSIBLE_USERNAME }}"
- "{{ build_username }}"
- "{{ ansible_username }}"

2 changes: 1 addition & 1 deletion build-ci.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ variables:

.packer-build:
stage: build
#tags:
#tags:
# - packer
before_script:
- "packer init \"$INPUT_PATH\""
Expand Down
35 changes: 12 additions & 23 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ else
CONFIG_PATH=$(realpath "${SCRIPT_PATH}/config")
fi

menu_banner=$(cat << "EOF"
____ __ ____ _ __ __
/ __ \____ ______/ /_____ _____ / __ )__ __(_) /___/ /____
/ /_/ / __ / ___/ //_/ _ \/ ___/ / __ / / / / / / __ / ___/
/ ____/ /_/ / /__/ ,< / __/ / / /_/ / /_/ / / / /_/ (__ )
/_/ \__,_/\___/_/|_|\___/_/ /_____/\__,_/_/_/\__,_/____/
EOF
)

menu_message="Select a HashiCorp Packer build for VMware vSphere."

if [ "$debug_mode" = true ]; then
Expand Down Expand Up @@ -217,15 +208,15 @@ menu_option_5() {
BUILD_PATH=${INPUT_PATH#"${SCRIPT_PATH}/builds/"}
BUILD_VARS="$(echo "${BUILD_PATH%/}" | tr -s '/' | tr '/' '-').pkrvars.hcl"

echo -e "\nCONFIRM: Build a Ubuntu Server 23.10 (cloud-init) Template for VMware vSphere?"
echo -e "\nCONFIRM: Build a Ubuntu Server 23.10 Template for VMware vSphere?"
echo -e "\nContinue? (y/n)"
read -r REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi

### Build a Ubuntu Server 23.10 (cloud-init) Template for VMware vSphere. ###
echo "Building a Ubuntu Server 23.10 (cloud-init) Template for VMware vSphere..."
### Build a Ubuntu Server 23.10 Template for VMware vSphere. ###
echo "Building a Ubuntu Server 23.10 Template for VMware vSphere..."

### Initialize HashiCorp Packer and required plugins. ###
echo "Initializing HashiCorp Packer and required plugins..."
Expand Down Expand Up @@ -254,15 +245,15 @@ menu_option_6() {
BUILD_PATH=${INPUT_PATH#"${SCRIPT_PATH}/builds/"}
BUILD_VARS="$(echo "${BUILD_PATH%/}" | tr -s '/' | tr '/' '-').pkrvars.hcl"

echo -e "\nCONFIRM: Build a Ubuntu Server 22.04 LTS (cloud-init) Template for VMware vSphere?"
echo -e "\nCONFIRM: Build a Ubuntu Server 22.04 LTS Template for VMware vSphere?"
echo -e "\nContinue? (y/n)"
read -r REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi

### Build a Ubuntu Server 22.04 LTS (cloud-init) Template for VMware vSphere. ###
echo "Building a Ubuntu Server 22.04 LTS (cloud-init) Template for VMware vSphere..."
### Build a Ubuntu Server 22.04 LTS Template for VMware vSphere. ###
echo "Building a Ubuntu Server 22.04 LTS Template for VMware vSphere..."

### Initialize HashiCorp Packer and required plugins. ###
echo "Initializing HashiCorp Packer and required plugins..."
Expand Down Expand Up @@ -291,15 +282,15 @@ menu_option_7() {
BUILD_PATH=${INPUT_PATH#"${SCRIPT_PATH}/builds/"}
BUILD_VARS="$(echo "${BUILD_PATH%/}" | tr -s '/' | tr '/' '-').pkrvars.hcl"

echo -e "\nCONFIRM: Build a Ubuntu Server 20.04 LTS (cloud-init) Template for VMware vSphere?"
echo -e "\nCONFIRM: Build a Ubuntu Server 20.04 LTS Template for VMware vSphere?"
echo -e "\nContinue? (y/n)"
read -r REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi

### Build a Ubuntu Server 20.04 LTS (cloud-init) Template for VMware vSphere. ###
echo "Building a Ubuntu Server 20.04 LTS (cloud-init) Template for VMware vSphere..."
### Build a Ubuntu Server 20.04 LTS Template for VMware vSphere. ###
echo "Building a Ubuntu Server 20.04 LTS Template for VMware vSphere..."

### Initialize HashiCorp Packer and required plugins. ###
echo "Initializing HashiCorp Packer and required plugins..."
Expand Down Expand Up @@ -1364,8 +1355,6 @@ incorrect_selection() {
until [ "$selection" = "0" ]; do
clear
echo ""
echo -e "$menu_banner"
echo ""
echo -e "$menu_message"
echo ""
echo " Linux Distribution:"
Expand All @@ -1374,9 +1363,9 @@ until [ "$selection" = "0" ]; do
echo " 2 - VMware Photon OS 4"
echo " 3 - Debian 12"
echo " 4 - Debian 11"
echo " 5 - Ubuntu Server 23.10 (cloud-init)"
echo " 6 - Ubuntu Server 22.04 LTS (cloud-init)"
echo " 7 - Ubuntu Server 20.04 LTS (cloud-init)"
echo " 5 - Ubuntu Server 23.10"
echo " 6 - Ubuntu Server 22.04 LTS"
echo " 7 - Ubuntu Server 20.04 LTS"
echo " 8 - Red Hat Enterprise Linux 9"
echo " 9 - Red Hat Enterprise Linux 8"
echo " 10 - Red Hat Enterprise Linux 7"
Expand Down
11 changes: 0 additions & 11 deletions build.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ else
CONFIG_PATH=$(realpath "${SCRIPT_PATH}/config")
fi

menu_banner=$(cat << "EOF"
____ __ ____ _ __ __
/ __ \____ ______/ /_____ _____ / __ )__ __(_) /___/ /____
/ /_/ / __ / ___/ //_/ _ \/ ___/ / __ / / / / / / __ / ___/
/ ____/ /_/ / /__/ ,< / __/ / / /_/ / /_/ / / / /_/ (__ )
/_/ \__,_/\___/_/|_|\___/_/ /_____/\__,_/_/_/\__,_/____/
EOF
)

menu_message="Select a HashiCorp Packer build for VMware vSphere."

if [ "$debug_mode" = true ]; then
Expand Down Expand Up @@ -143,8 +134,6 @@ incorrect_selection() {
until [ "$selection" = "0" ]; do
clear
echo ""
echo -e "$menu_banner"
echo ""
echo -e "$menu_message"
echo ""
{{- $menu_index := 0 }}
Expand Down
6 changes: 3 additions & 3 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ menu:
var_files:
- "network.pkrvars.hcl"
- "linux-storage.pkrvars.hcl"
- entry: "Ubuntu Server 23.10 (cloud-init)"
- entry: "Ubuntu Server 23.10"
build:
path: "builds/linux/ubuntu/23-10/"
var_files:
- "network.pkrvars.hcl"
- "linux-storage.pkrvars.hcl"
- entry: "Ubuntu Server 22.04 LTS (cloud-init)"
- entry: "Ubuntu Server 22.04 LTS"
build:
path: "builds/linux/ubuntu/22-04-lts/"
var_files:
- "network.pkrvars.hcl"
- "linux-storage.pkrvars.hcl"
- entry: "Ubuntu Server 20.04 LTS (cloud-init)"
- entry: "Ubuntu Server 20.04 LTS"
build:
path: "builds/linux/ubuntu/20-04-lts/"
var_files:
Expand Down
4 changes: 2 additions & 2 deletions builds/build.pkrvars.hcl.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

// Default Account Credentials
build_username = "packer"
build_password = "R@in!$aG00dThing."
build_password_encrypted = "$6$jqoxlQyW2ROxsLK4$P3gGp2Bf0FCSLAZTNaE/LZBqSsvVAYZNPNwTehXZbMAhRJdKOjNqqyUWVUTwql9CnhHUlpiluWzHlR2DpUKD4/"
build_password = "_P@cker_Ex#mple$_"
build_password_encrypted = "$6$rak.KyfWvfvqCy88$UmDg3XUsVCo.51sTKm3sFClrs3MCUssSOUHYBl/d9kfzLNwRR3lEaFFjgCr2eGI3rvOwycXFsGGDLynrmPsOK1"
build_key = "ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBADwXV3rbRCWwhSr6aMkHukV5O7OGAEyUtAerj2anJHm3mwbOxlBU/uO4f0ELqo2GJcTALMC0aFrbvu9qonIH5VF7wBBfCP1cS5B92sUagVV9ldI/uo89e/7dVYC9maPsFaZq2G0/PLU0hZKOohq99Oxc2RMSiJaaenX/hNqx5xYSaK+CA== [email protected]"
8 changes: 4 additions & 4 deletions builds/linux-storage.pkrvars.hcl.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

// VM Storage Settings
vm_disk_device = "sda"
vm_disk_use_swap = true
vm_disk_device = "sda"
vm_disk_use_swap = true
vm_disk_partitions = [
{
name = "efi"
Expand Down Expand Up @@ -53,8 +53,8 @@ vm_disk_partitions = [
]
vm_disk_lvm = [
{
name: "sysvg",
partitions: [
name : "sysvg",
partitions : [
{
name = "lv_swap",
size = 1024,
Expand Down
8 changes: 4 additions & 4 deletions builds/linux/almalinux/8/linux-almalinux.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
8 changes: 4 additions & 4 deletions builds/linux/almalinux/9/linux-almalinux.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
8 changes: 4 additions & 4 deletions builds/linux/centos/7/linux-centos.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
8 changes: 4 additions & 4 deletions builds/linux/centos/8-stream/linux-centos-stream.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
8 changes: 4 additions & 4 deletions builds/linux/centos/9-stream/linux-centos-stream.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
8 changes: 4 additions & 4 deletions builds/linux/debian/11/linux-debian.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ build {
]
extra_arguments = [
"--extra-vars", "display_skipped_hosts=false",
"--extra-vars", "BUILD_USERNAME=${var.build_username}",
"--extra-vars", "BUILD_KEY='${var.build_key}'",
"--extra-vars", "ANSIBLE_USERNAME=${var.ansible_username}",
"--extra-vars", "ANSIBLE_KEY='${var.ansible_key}'",
"--extra-vars", "build_username=${var.build_username}",
"--extra-vars", "build_key='${var.build_key}'",
"--extra-vars", "ansible_username=${var.ansible_username}",
"--extra-vars", "ansible_key='${var.ansible_key}'",
]
}

Expand Down
Loading

0 comments on commit fcfce8f

Please sign in to comment.