Skip to content

Latest commit

 

History

History
408 lines (286 loc) · 11 KB

README.md

File metadata and controls

408 lines (286 loc) · 11 KB

packer-ansible

Overview

This repository uses Packer to build a virtual machines for VMware, VirtualBox, or AWS AMI.

Contents

  1. Expectations
  2. Build
    1. Prerequisite software
    2. Clone repository
    3. Custom var file
    4. Packer Cache
    5. Ansible Mode
    6. Build using template-centos
    7. Build using template-debian
    8. Build using template-ubuntu
  3. Run on VMware Workstation
  4. Run on Vagrant / VirtualBox
    1. Add to library
    2. Run
    3. Login to guest machine
    4. Find guest machine IP address
    5. Remote login to guest machine
    6. Remove image from Vagrant library
  5. Troubleshoot
  6. References

Legend

  1. 🤔 - A "thinker" icon means that a little extra thinking may be required. Perhaps there are some choices to be made. Perhaps it's an optional step.
  2. ✏️ - A "pencil" icon means that the instructions may need modification before performing.
  3. ⚠️ - A "warning" icon means that something tricky is happening, so pay attention.

Expectations

  • Space: This repository and demonstration require 6 GB free disk space.
  • Time: Budget 2 hours minutes to create the virtual machine.
  • Background knowledge: This repository assumes a working knowledge of:

Build

Prerequisite software

The following software programs need to be installed:

  1. git
  2. make
  3. packer
  4. Builders (not all may be needed):
    1. AWS command line interface
    2. GCP command line interface
    3. VMware Workstation
    4. Vagrant
    5. VirtualBox

Clone repository

For more information on environment variables, see Environment Variables.

  1. Set these environment variable values:

    export GIT_ACCOUNT=senzing
    export GIT_REPOSITORY=packer-ansible
    export GIT_ACCOUNT_DIR=~/${GIT_ACCOUNT}.git
    export GIT_REPOSITORY_DIR="${GIT_ACCOUNT_DIR}/${GIT_REPOSITORY}"
  2. Follow steps in clone-repository to install the Git repository.

Custom var file

🤔 The Makefile uses the following files to create virtual images:

  1. TEMPLATE_FILE - The Packer template.json file used in the build.
  2. PLATFORM_VAR_FILE - A file of variables specifying a base image (ISO, AMI, etc.)
  3. CUSTOM_VAR_FILE - A user-configurable file specifying values to use during the build.

In the examples below, the CUSTOM_VAR_FILE is set to vars/custom-var.json In practice, this value should be modified to point to a user's custom file of variables.

The CUSTOM_VAR_FILE, can be used to:

  1. Specify an Ansible playbook.
  2. Add Multifactor Authentication information
  3. Specify SSH keys
  4. Change disk or memory size
  5. Specify an AMI Marketplace Image

Example:

export CUSTOM_VAR_FILE=~/my-vars/my-custom-var.json

Packer Cache

✏️ Packer cache the downloaded images into a folder called packer_cache. By default, the packer_cache folder is created in the packer-ansible folder. To modify the location of the packer cache folder, simply export the following variable:

Example:

export PACKER_CACHE_DIR=/path/to/packer_cache

Ansible Mode

✏️ The Ansible installation on the image can be adjusted with the following modes:

  1. install - The Ansible package is installed, used and remains on the image.
  2. ephemeral - The Ansible package is installed, used and then removed from the image.
  3. none - The Ansible package is never installed on the image (default).

Example:

export ANSIBLE_MODE=install

Build using template-centos

CentOS 7.6

Example: amazon-ebs (Before creating AMIs, go through the AWS AMI setup first)

cd ${GIT_REPOSITORY_DIR}
export TEMPLATE_FILE=template-centos.json
export PLATFORM_VAR_FILE=vars/centos-07.06.json
export CUSTOM_VAR_FILE=vars/custom-var.json
make amazon-ebs

To build GCP Compute Images (Before creating images, go through the GCP Compute Image setup first), instead of make amazon-ebs, run the following:

make googlecompute

To build virtualbox, instead of make amazon-ebs, run the following:

make virtualbox-iso

To build vmware-iso, instead of make amazon-ebs, run the following:

make vmware-iso

CentOS 8.2

Example: amazon-ebs

cd ${GIT_REPOSITORY_DIR}
export TEMPLATE_FILE=template-centos.json
export PLATFORM_VAR_FILE=vars/centos-08.02.json
export CUSTOM_VAR_FILE=vars/custom-var.json
make amazon-ebs

To build GCP Compute Images (Before creating images, go through the GCP Compute Image setup first), instead of make amazon-ebs, run the following:

make googlecompute

To build virtualbox, instead of make amazon-ebs, run the following:

make virtualbox-iso

To build vmware-iso, instead of make amazon-ebs, run the following:

make vmware-iso

Build using template-debian

Debian 9.12

Example: amazon-ebs

cd ${GIT_REPOSITORY_DIR}
export TEMPLATE_FILE=template-debian.json
export PLATFORM_VAR_FILE=vars/debian-09.12.00.json
export CUSTOM_VAR_FILE=vars/custom-var.json
make amazon-ebs

To build GCP Compute Images (Before creating images, go through the GCP Compute Image setup first), instead of make amazon-ebs, run the following:

make googlecompute

To build virtualbox, instead of make amazon-ebs, run the following:

make virtualbox-iso

To build vmware-iso, instead of make amazon-ebs, run the following:

make vmware-iso

Debian 10.04.00

Example: amazon-ebs

cd ${GIT_REPOSITORY_DIR}
export TEMPLATE_FILE=template-debian.json
export PLATFORM_VAR_FILE=vars/debian-10.04.00.json
export CUSTOM_VAR_FILE=vars/custom-var.json
make amazon-ebs

To build GCP Compute Images (Before creating images, go through the GCP Compute Image setup first), instead of make amazon-ebs, run the following:

make googlecompute

To build virtualbox, instead of make amazon-ebs, run the following:

make virtualbox-iso

To build vmware-iso, instead of make amazon-ebs, run the following:

make vmware-iso

Build using template-ubuntu

Ubuntu 18.04.04

Example: amazon-ebs

cd ${GIT_REPOSITORY_DIR}
export TEMPLATE_FILE=template-ubuntu.json
export PLATFORM_VAR_FILE=vars/ubuntu-18.04.04.json
export CUSTOM_VAR_FILE=vars/custom-var.json
make amazon-ebs

To build GCP Compute Images (Before creating images, go through the GCP Compute Image setup first), instead of make amazon-ebs, run the following:

make googlecompute

To build virtualbox, instead of make amazon-ebs, run the following:

make virtualbox-iso

To build vmware-iso, instead of make amazon-ebs, run the following:

make vmware-iso

Run on VMware Workstation

  1. Choose VMX file
    1. VMware Workstation > File > Open...
      1. Navigate to .../output-vmware-iso-nnnnnnnnnn/
      2. Choose senzing-packer-xxxxxx-nnnnnnnnnn.vmx
  2. Optional: Change networking
    1. Navigate to VMware Workstation > My Computer > senzing-packer-xxxxx-nnnnnnnnnn
    2. Right click on "senzing-packer-xxxxxx-nnnnnnnnnn" and choose "Settings"
    3. Choose "Network Adapter" > "Network Connection" > 🔘 Bridged: Connected directly to the physical network
    4. Click "Save"
  3. Run image
    1. Choose VMware Workstation > My Computer > senzing-packer-xxxxx-nnnnnnnnnn
    2. Click "Start up this guest operating system"
  4. Username: vagrant Password: vagrant

Run on Vagrant / VirtualBox

Add to library

  1. ✏️ Identify virtual image metadata. Example:

    export VAGRANT_NAME=packer-xxxxxx-virtualbox
    export VIRTUALBOX_FILE=./senzing-packer-xxxxxx-virtualbox-nnnnnnnnnn.box
  2. Add VirtualBox image to vagrant Example:

    vagrant box add --name=${VAGRANT_NAME} ${VIRTUALBOX_FILE}

Run

An example of how to run in a new directory.

Specify directory

In this example the /tmp/packer-ubuntu-18.04 directory is used.

export VAGRANT_DIR=/tmp/${VAGRANT_NAME}

Initialize directory

Back up an old directory and initialize new directory with Vagrant image.

mv ${VAGRANT_DIR} ${VAGRANT_DIR}.$(date +%s)
mkdir ${VAGRANT_DIR}
cd ${VAGRANT_DIR}
vagrant init ${VAGRANT_NAME}

Enable remote login

Modify Vagrantfile to allow remote login by uncommenting config.vm.network in ${VAGRANT_DIR}/Vagrantfile. Example:

sed -i.$(date +'%s') \
  -e 's/# config.vm.network \"public_network\"/config.vm.network \"public_network\"/g' \
  ${VAGRANT_DIR}/Vagrantfile

Start guest machine

cd ${VAGRANT_DIR}
vagrant up

Login to guest machine

cd ${VAGRANT_DIR}
vagrant ssh

Find guest machine IP address

In the vagrant machine, find the IP address.

ip addr show

Remote login to guest machine

SSH login from a remote machine.

Password: vagrant

Remove image from Vagrant library

To remove Vagrant image, on host machine:

vagrant box remove ${VAGRANT_NAME}

Troubleshoot

If the following error surfaces

Build 'vmware-iso' errored: Error detecting host IP: Could not find vmnetdhcp conf file:

To resolve the error, include the following in the template's vmware-iso builder,

"network": "vmnet8"

References

  1. Build dependencies.
  2. Bibliography