Giter Site home page Giter Site logo

oguzhan-bolukbas / example-qemu-image-with-packer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carbasemin/example-qemu-image-with-packer

0.0 0.0 0.0 0 B

Create a custom Ubuntu 22.04 cloud image with Hashicorp Packer and Ansible.

License: MIT License

HCL 100.00%

example-qemu-image-with-packer's Introduction

Example Base VM Image

We want all our servers to have the same (or as similar as possible) state, and manage that state with IaC.

Packer file, base-image.pkr.hcl

Packer

  • creates a virtual machine with Ubuntu 22.04 cloud image, using QEMU and KVM. So, you need to have KVM installed on your local, or wherever you’re building this image
  • the output image format will be qcow2, which is a QEMU image.
  • mounts below files to the VM. cd_label absolutely needs to be named “cidata.” Otherwise it won’t mount.
cd_files = ["cloud-init/user-data", "cloud-init/meta-data", "cloud-init/network-config"]
cd_label = "cidata"
  • to be able to gracefully shutdown the VM, you have to pass
shutdown_command = "sudo shutdown -P now"

Below is the build stage of the Packer file:

build {
  sources = ["source.qemu.template"]

  # wait for cloud-init to successfully finish
  provisioner "shell" {
    inline = [
      "cloud-init status --wait > /dev/null 2>&1"
    ]
  }

  provisioner "ansible-local" {
    playbook_file = "ansible/playbook.yml"
  }
}

After the VM is up, Packer will connect to it via SSH on port 8324 (it will wait 1 minute before connecting). It will wait for the cloud-init to finish. cloud-init is like Ubuntu’s pre-boot config. It will get executed when your first installing the OS. We’ll explain what our cloud-init configs do in the next section.

After the cloud init, the Ansible playbook is copied inside the VM and run locally there. It will be explained in the third section.

cloud-init

network-config

version: 2
ethernets:
  eth0:
    dhcp4: yes
    nameservers:
      addresses:
        - 8.8.8.8
        - 8.8.4.4
  ens3:
    dhcp4: yes
    nameservers:
      addresses:
        - 8.8.8.8
        - 8.8.4.4

Without this, we won’t be able to connect to the internet because we won’t have a default network that would allow it.

user-data

  • Disable root login.
  • Disable SSH password authentication.
  • Create ubuntu user, add it to the sudo group and give it passwordless sudo access. Also add pgeo publickey to its authorized_keys.
  • update apt cache, upgrade, and install python3.
  • Install ansible using pup and install community.general packages.
  • Switch SSH port to 8324.
  • Disable cloud-init so that it won’t be run everytime the server is restarted.
  • sed -ie 's/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 elevator=noop"/' /etc/default/grub couldn’t really understand this one to be honest, but without it the networking doesn’t work.
  • Use Ubuntu TR mirrors.
  • Grow the disk to the VM disk size, which was appointed as 20GB by us via the Packer file.

meta-data

No metadata. But that file should be there. cloud-init just asks for it.

Ansible Playbook

playbook.yml

This is for general use.

  • Install docker and docker-compose.
  • Install basic, necessary software.
  • Set up firewall rules.

example-qemu-image-with-packer's People

Contributors

carbasemin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.