├── README.md
├── image
└── logo-packer-padded.svg
├── part01-aws-eks-basic-image
├── README.md
├── build.pkr.hcl
├── source.pkr.hcl
└── variables.pkr.hcl
├── part02-ubuntu-ansible-config
├── README.md
├── build.pkr.hcl
├── data.pkr.hcl
├── install-ansible.sh
├── source.pkr.hcl
└── variables.pkr.hcl
├── part03-parallel-image
├── README.md
├── build.pkr.hcl
├── data.pkr.hcl
├── source.pkr.hcl
└── variables.pkr.hcl
├── part04-vagrant
├── README.md
├── build.pkr.hcl
├── source.pkr.hcl
├── to_upload.txt
└── variables.pkr.hcl
├── part05-virtualbox
├── README.md
├── build.pkr.hcl
├── source.pkr.hcl
└── variables.pkr.hcl
├── part06-vmware-vsphere
├── README.md
├── build.pkr.hcl
├── preseed.cfg
├── source.pkr.hcl
└── variables.pkr.hcl
├── part07-lxc-image
├── README.md
├── config
├── lxc.json
└── scripts
│ └── setup.sh
└── part08-docker
├── README.md
├── build.pkr.hcl
├── source.pkr.hcl
└── variables.pkr.hcl
/README.md:
--------------------------------------------------------------------------------
1 | # Packer-tutorial
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | ## Documentation
10 |
11 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
12 |
13 | ## Published articles:
14 |
15 | - [Packer Tutorial - Part 1 - Create Basic EKS image on AWS](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part01-aws-eks-basic-image)
16 |
17 | - [Packer Tutorial - Part 2 - Create Basic Ubuntu image with Ansible config on AWS](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part02-ubuntu-ansible-config)
18 |
19 | - [Packer Tutorial - Part 3 - Create paraller image with to different provider](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part03-parallel-image)
20 |
21 | - [Packer Tutorial - Part 4 - Create Vagrant image](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part04-vagrant)
22 |
23 | - [Packer Tutorial - Part 5 - Create VirtualBox image](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part05-virtualbox)
24 |
25 | - [Packer Tutorial - Part 6 - Create VMware vSphere image](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part06-vmware-vsphere)
26 |
27 | - [Packer Tutorial - Part 7 - Create LXC image](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part07-lxc-image)
28 |
29 | - [Packer Tutorial - Part 8 - Create Docker image](https://github.com/ahmadalibagheri/packer-tutorial/tree/master/part08-docker)
30 |
31 | - [Packer Tutorial - Part 9 - Create Azure image]() InProgress
32 |
33 |
34 | ## Contributions:
35 |
36 | All contributions are welcomed. Help me to enrich this repository.
37 |
38 | If you find any **bugs** in the examples, please file an issue.
39 |
40 | ### TODO:
41 |
42 | - [ ] Adding Packer Hyper-V
43 | - [ ] Adding Packer Azure
44 | - [ ] Adding Packer Openstack
45 |
--------------------------------------------------------------------------------
/image/logo-packer-padded.svg:
--------------------------------------------------------------------------------
1 |
28 |
--------------------------------------------------------------------------------
/part01-aws-eks-basic-image/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.
7 |
8 | ```shell
9 | $ export AWS_ACCESS_KEY_ID=AAAAAA
10 | $ export AWS_SECRET_ACCESS_KEY=AAAAA
11 | ```
12 |
13 | # packer-aws-eks-image
14 |
15 | Build basic eks image with packer hashicorp
16 |
17 | ## Usage
18 |
19 | Install project dependencies
20 |
21 | ```shell
22 | packer init .
23 | ```
24 |
25 | The packer fmt command updates templates in the current directory for readability and consistency.
26 |
27 | ```shell
28 | packer fmt .
29 | ```
30 |
31 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
32 |
33 | ```shell
34 | packer validate .
35 | ```
36 |
37 | Build the image with the packer build command.
38 |
39 | ```shell
40 | packer build .
41 | ```
42 |
--------------------------------------------------------------------------------
/part01-aws-eks-basic-image/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | amazon = {
4 | version = ">= 0.0.2"
5 | source = "github.com/hashicorp/amazon"
6 | }
7 | }
8 | }
9 |
10 | # Adding script to install in image
11 |
12 | build {
13 | name = "learn-packer"
14 | sources = [
15 | "source.amazon-ebs.eks-image"
16 | ]
17 |
18 | provisioner "shell" {
19 | environment_vars = [
20 | "FOO=hello world",
21 | ]
22 | inline = [
23 | "echo install htop",
24 | "sleep 30",
25 | "sudo yum update",
26 | "sudo yum install htop -y"
27 | ]
28 | }
29 | }
--------------------------------------------------------------------------------
/part01-aws-eks-basic-image/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "amazon-ebs" "eks-image" {
2 | region = var.region
3 | profile = var.profile
4 | ami_name = "EKS-${var.EKS_version}-${local.timestamp}"
5 | instance_type = var.instance_type
6 | encrypt_boot = true
7 | source_ami_filter {
8 | filters = {
9 | name = var.AMI_name
10 | root-device-type = "ebs"
11 | virtualization-type = "hvm"
12 | }
13 | most_recent = true
14 | owners = [var.AMI_owners]
15 | }
16 | ssh_username = "ec2-user"
17 | tags = {
18 | Name = "EKS-${var.EKS_version}-${local.timestamp}"
19 | Base_AMI_ID = "{{ .SourceAMI }}"
20 | Base_AMI_Name = "{{ .SourceAMIName }}"
21 | ENV = var.environment
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/part01-aws-eks-basic-image/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | #Public variables
2 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
3 |
4 | variable "region" {
5 | type = string
6 | default = "us-east-1"
7 | }
8 |
9 | variable "instance_type" {
10 | type = string
11 | default = "t2.micro"
12 | }
13 |
14 | #Image variable
15 | variable "AMI_name" {
16 | type = string
17 | default = "amazon-eks-node-1.21-v20220226"
18 | }
19 |
20 | variable "AMI_owners" {
21 | type = string
22 | default = "602401143452"
23 | }
24 |
25 | #Environment variables
26 | variable "environment" {
27 | type = string
28 | default = "DevOps"
29 | }
30 |
31 | variable "profile" {
32 | type = string
33 | default = "default"
34 | }
35 |
36 | variable "EKS_version" {
37 | type = string
38 | default = "21"
39 | }
40 |
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.
7 |
8 | ```shell
9 | $ export AWS_ACCESS_KEY_ID=AAAAAA
10 | $ export AWS_SECRET_ACCESS_KEY=AAAAA
11 |
12 | # And also if you didn't specify default AWS region:
13 | $ export AWS_DEFAULT_REGION=xx-xxxx-x
14 | ```
15 |
16 | # packer-aws-ubuntu-image
17 |
18 | Build basic eks image with packer hashicorp
19 |
20 | ## Usage
21 |
22 | Install project dependencies
23 |
24 | ```shell
25 | packer init .
26 | ```
27 |
28 | The packer fmt command updates templates in the current directory for readability and consistency.
29 |
30 | ```shell
31 | packer fmt .
32 | ```
33 |
34 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
35 |
36 | ```shell
37 | packer validate .
38 | ```
39 |
40 | Build the image with the packer build command.
41 |
42 | ```shell
43 | packer build .
44 | ```
45 |
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | amazon = {
4 | version = ">= 1.2.2"
5 | source = "github.com/hashicorp/amazon"
6 | }
7 | }
8 | }
9 |
10 | # Adding script to install in image
11 |
12 | build {
13 | name = var.ami_name
14 | sources = [
15 | "source.amazon-ebs.ansible-image"
16 | ]
17 |
18 | provisioner "shell" {
19 | environment_vars = [
20 | "FOO=hello world",
21 | ]
22 | script = "install-ansible.sh"
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/data.pkr.hcl:
--------------------------------------------------------------------------------
1 | data "amazon-ami" "ansible" {
2 | filters = {
3 | virtualization-type = "hvm"
4 | name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
5 | root-device-type = "ebs"
6 | }
7 | owners = ["099720109477"]
8 | most_recent = true
9 | region = var.region
10 | }
11 |
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/install-ansible.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -e
4 |
5 | echo "-----------------Install ansible config-----------------"
6 | sleep 30
7 | sudo apt-get update
8 | sudo apt-get install htop nmon mc -y
9 |
10 | echo "-----------------Adding ansible repository-----------------"
11 | sudo apt-get install software-properties-common
12 | sudo add-apt-repository --yes --update ppa:ansible/ansible
13 |
14 | echo "-----------------Install ansible-----------------"
15 | sudo apt-get -y install ansible && ansible --version
16 |
17 | echo "---------------------------------------------------"
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "amazon-ebs" "ansible-image" {
2 | region = var.region
3 | profile = var.profile
4 | ami_name = "Ansible-${local.timestamp}"
5 | instance_type = var.instance_type
6 | encrypt_boot = true
7 | source_ami = data.amazon-ami.ansible.id
8 | ssh_username = "ubuntu"
9 | tags = {
10 | Name = "Ansible-${local.timestamp}"
11 | Base_AMI_ID = "{{ .SourceAMI }}"
12 | Base_AMI_Name = "{{ .SourceAMIName }}"
13 | ENV = var.environment
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/part02-ubuntu-ansible-config/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | #Public variables
2 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
3 |
4 | variable "region" {
5 | type = string
6 | default = "eu-west-1"
7 | }
8 |
9 | variable "instance_type" {
10 | type = string
11 | default = "t2.micro"
12 | }
13 |
14 | #Environment variables
15 | variable "environment" {
16 | type = string
17 | default = "DevOps"
18 | }
19 |
20 | variable "profile" {
21 | type = string
22 | default = "default"
23 | }
24 |
25 | variable "ami_name" {
26 | type = string
27 | default = "learn-packer"
28 | }
--------------------------------------------------------------------------------
/part03-parallel-image/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | Add your AWS credentials as two environment variables, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, replacing AAAAAA with each respective values.
7 |
8 | ```shell
9 | $ export AWS_ACCESS_KEY_ID=AAAAAA
10 | $ export AWS_SECRET_ACCESS_KEY=AAAAA
11 | ```
12 |
13 | # packer-paraller-aws-ubuntu-image-vs-docker-image
14 |
15 | Build basic eks image with packer hashicorp
16 |
17 | ## Usage
18 |
19 | Install project dependencies
20 |
21 | ```shell
22 | packer init .
23 | ```
24 |
25 | The packer fmt command updates templates in the current directory for readability and consistency.
26 |
27 | ```shell
28 | packer fmt .
29 | ```
30 |
31 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
32 |
33 | ```shell
34 | packer validate .
35 | ```
36 |
37 | Build the image with the packer build command.
38 |
39 | ```shell
40 | packer build .
41 | ```
42 |
--------------------------------------------------------------------------------
/part03-parallel-image/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | amazon = {
4 | version = ">= 0.0.2"
5 | source = "github.com/hashicorp/amazon"
6 | }
7 | docker = {
8 | version = ">= 0.0.7"
9 | source = "github.com/hashicorp/docker"
10 | }
11 | }
12 | }
13 |
14 | # Adding script to install in image
15 |
16 | build {
17 | name = "learn-packer"
18 | sources = [
19 | "source.amazon-ebs.ansible-image",
20 | "source.docker.ubuntu"
21 | ]
22 |
23 | provisioner "shell" {
24 | environment_vars = [
25 | "FOO=hello world",
26 | ]
27 | inline = [
28 | "echo Adding file to Docker Container",
29 | "echo \"FOO is $FOO\" > example.txt",
30 | ]
31 | only = ["docker.ubuntu"]
32 | }
33 |
34 | provisioner "shell" {
35 | inline = ["echo Running ${var.docker_image} Docker image."]
36 | only = ["docker.ubuntu"]
37 | }
38 |
39 | provisioner "shell" {
40 | environment_vars = [
41 | "FOO=hello world",
42 | ]
43 | inline = [
44 | "echo -----------------update ubuntu-----------------",
45 | "sleep 30",
46 | "sudo apt-get update",
47 | ]
48 | only = ["amazon-ebs.ansible-image"]
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/part03-parallel-image/data.pkr.hcl:
--------------------------------------------------------------------------------
1 | data "amazon-ami" "ansible" {
2 | filters = {
3 | virtualization-type = "hvm"
4 | name = "ubuntu/images/*ubuntu-focal-20.04-amd64-server-*"
5 | root-device-type = "ebs"
6 | }
7 | owners = ["099720109477"]
8 | most_recent = true
9 | }
10 |
--------------------------------------------------------------------------------
/part03-parallel-image/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "amazon-ebs" "ansible-image" {
2 | region = var.region
3 | profile = var.profile
4 | ami_name = "Ansible-${local.timestamp}"
5 | instance_type = var.instance_type
6 | encrypt_boot = true
7 | source_ami = data.amazon-ami.ansible.id
8 | ssh_username = "ubuntu"
9 | tags = {
10 | Name = "Ansible-${local.timestamp}"
11 | Base_AMI_ID = "{{ .SourceAMI }}"
12 | Base_AMI_Name = "{{ .SourceAMIName }}"
13 | ENV = var.environment
14 | }
15 | }
16 |
17 | source "docker" "ubuntu" {
18 | image = var.docker_image
19 | commit = true
20 | }
21 |
--------------------------------------------------------------------------------
/part03-parallel-image/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | #Public variables
2 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
3 |
4 | variable "region" {
5 | type = string
6 | default = "us-east-1"
7 | }
8 |
9 | variable "instance_type" {
10 | type = string
11 | default = "t2.micro"
12 | }
13 |
14 | #Environment variables
15 | variable "environment" {
16 | type = string
17 | default = "DevOps"
18 | }
19 |
20 | variable "profile" {
21 | type = string
22 | default = "default"
23 | }
24 |
25 | variable "docker_image" {
26 | type = string
27 | default = "ubuntu:xenial"
28 | }
29 |
--------------------------------------------------------------------------------
/part04-vagrant/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | # packer-vagrant
7 |
8 | Build a basic Ubuntu VM from a vagrant box, upload a file to the defined location and finally, install Redis on the image. Make the desired directory as the machine's storage that's been mapped from host into the VM. You can change the values and boxes in 'variables.yml'. The output box will be named as 'package.box' in the generated directory.
9 |
10 | ## Usage
11 |
12 | Install project dependencies
13 |
14 | ```shell
15 | packer init .
16 | ```
17 |
18 | The packer fmt command updates templates in the current directory for readability and consistency.
19 |
20 | ```shell
21 | packer fmt .
22 | ```
23 |
24 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
25 |
26 | ```shell
27 | packer validate .
28 | ```
29 |
30 | Build the image with the packer build command.
31 |
32 | ```shell
33 | packer build .
34 | ```
35 |
--------------------------------------------------------------------------------
/part04-vagrant/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | vagrant = {
4 | version = ">= 0.0.7"
5 | source = "github.com/hashicorp/vagrant"
6 | }
7 | }
8 | }
9 |
10 | build {
11 | sources = ["source.vagrant.training-example"]
12 | provisioner "file" {
13 | source = var.upload_file
14 | destination = "/home/${var.ssh_username}/${var.upload_file}"
15 | }
16 |
17 | provisioner "shell" {
18 | inline = ["sudo apt update && sudo apt install -y redis-server"]
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/part04-vagrant/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "vagrant" "training-example" {
2 | communicator = "ssh"
3 | ssh_username = var.ssh_username
4 | ssh_password = var.ssh_password
5 | ssh_timeout = "60m"
6 | ssh_wait_timeout = "90s"
7 | insert_key = true
8 | source_path = var.vagrant_box
9 | provider = var.vagrant_provider
10 | add_force = true
11 | synced_folder = var.synced_folder_path
12 | output_dir = var.output_dir
13 | }
14 |
--------------------------------------------------------------------------------
/part04-vagrant/to_upload.txt:
--------------------------------------------------------------------------------
1 | Hi From Vagrant!
2 |
--------------------------------------------------------------------------------
/part04-vagrant/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | variable "ssh_username" {
2 | type = string
3 | default = "vagrant"
4 | }
5 |
6 | variable "ssh_password" {
7 | type = string
8 | default = "vagrant"
9 | }
10 |
11 | variable "upload_file" {
12 | type = string
13 | default = "to_upload.txt"
14 | }
15 |
16 | variable "user" {
17 | type = string
18 | default = "rojin"
19 | }
20 |
21 | variable "vagrant_box" {
22 | type = string
23 | default = "/home/rojin/Downloads/trusty-server-cloudimg-amd64-vagrant-disk1.box"
24 | }
25 |
26 | variable "output_dir" {
27 | type = string
28 | default = "./vagrant-output-box"
29 | }
30 |
31 | variable "vagrant_provider" {
32 | type = string
33 | default = "virtualbox"
34 | }
35 |
36 | variable "synced_folder_path" {
37 | type = string
38 | default = "./vagrant_storage"
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/part05-virtualbox/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | # packer-vsphere-iso
7 |
8 | Build a basic Ubuntu VM from an iso image with packer hashicorp and installs Nginx webserver on it.
9 |
10 | ## Usage
11 |
12 | Install project dependencies
13 |
14 | ```shell
15 | packer init .
16 | ```
17 |
18 | The packer fmt command updates templates in the current directory for readability and consistency.
19 |
20 | ```shell
21 | packer fmt .
22 | ```
23 |
24 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
25 |
26 | ```shell
27 | packer validate .
28 | ```
29 |
30 | Build the image with the packer build command.
31 |
32 | ```shell
33 | packer build .
34 | ```
35 |
--------------------------------------------------------------------------------
/part05-virtualbox/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | virtualbox = {
4 | version = ">= 0.0.1"
5 | source = "github.com/hashicorp/virtualbox"
6 | }
7 | }
8 | }
9 |
10 |
11 |
12 | # Adding script to install in image
13 |
14 | build {
15 | sources = ["source.virtualbox-iso.basic-example"]
16 |
17 | provisioner "shell" {
18 | inline = ["sleep 30", "apt update", "apt install nginx -y"]
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/part05-virtualbox/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "virtualbox-iso" "basic-example" {
2 | guest_os_type = "Ubuntu_64"
3 | iso_url = var.iso_url
4 | iso_checksum = var.iso_checksum
5 | ssh_username = var.ssh_username
6 | ssh_password = var.ssh_password
7 | ssh_timeout = "60m"
8 | disk_size = "15000"
9 | boot_command = [
10 | "",
11 | "",
12 | "",
13 | "",
14 | "",
15 | "",
16 | "",
17 | "",
18 | "",
19 | "",
20 | "",
21 | "/install/vmlinuz ",
22 | "initrd=/install/initrd.gz ",
23 | "net.ifnames=0 ",
24 | "auto-install/enable=true ",
25 | "debconf/priority=critical ",
26 | "preseed/url=http://{{.HTTPIP}}:{{.HTTPPort}}/preseed.cfg",
27 | ""
28 | ]
29 | shutdown_command = "echo 'packer' | sudo -S shutdown -P now"
30 | vboxmanage = [
31 | ["modifyvm", "{{.Name}}", "--memory", "1024"],
32 | ["modifyvm", "{{.Name}}", "--cpus", "2"],
33 | ]
34 | export_opts = [
35 | "--manifest",
36 | "--vsys", "0",
37 | "--description", "${var.vm_description}",
38 | "--version", "${var.vm_version}"
39 | ]
40 | format = "ova"
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/part05-virtualbox/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | #Public variables
2 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
3 |
4 | variable "iso_url" {
5 | type = string
6 | default = "http://releases.ubuntu.com/16.04/ubuntu-16.04.2-server-amd64.iso"
7 | }
8 |
9 | variable "iso_checksum" {
10 | type = string
11 | default = "737ae7041212c628de5751d15c3016058b0e833fdc32e7420209b76ca3d0a535"
12 | }
13 |
14 | variable "ssh_password" {
15 | type = string
16 | default = "PassWord"
17 | }
18 |
19 | variable "ssh_username" {
20 | type = string
21 | default = "UserName"
22 | }
23 |
24 | variable "vm_description" {
25 | type = string
26 | default = "My Nginx Server"
27 | }
28 |
29 | variable "vm_version" {
30 | type = string
31 | default = "1.0"
32 | }
33 |
--------------------------------------------------------------------------------
/part06-vmware-vsphere/README.md:
--------------------------------------------------------------------------------
1 | ## Documentation
2 |
3 | * Explore the Packer for Packer [CLI](https://www.packer.io/downloads) >= v1.8.0+
4 |
5 |
6 | # packer-vsphere-iso
7 |
8 | Build a basic Ubuntu VM from an iso image with packer hashicorp
9 |
10 | ## Usage
11 |
12 | Install project dependencies
13 |
14 | ```shell
15 | packer init .
16 | ```
17 |
18 | The packer fmt command updates templates in the current directory for readability and consistency.
19 |
20 | ```shell
21 | packer fmt .
22 | ```
23 |
24 | You can also make sure your configuration is syntactically valid and internally consistent by using the packer validate command.
25 |
26 | ```shell
27 | packer validate .
28 | ```
29 |
30 | Build the image with the packer build command.
31 |
32 | ```shell
33 | packer build .
34 | ```
35 |
--------------------------------------------------------------------------------
/part06-vmware-vsphere/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | vsphere = {
4 | version = ">= 0.0.1"
5 | source = "github.com/hashicorp/vsphere"
6 | }
7 | }
8 | }
9 |
10 |
11 | # Adding script to install in image
12 |
13 | build {
14 | sources = ["source.vsphere-iso.example"]
15 |
16 | provisioner "shell" {
17 | inline = ["sleep 30", "ls /", "echo 'Done!' " ]
18 | }
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/part06-vmware-vsphere/preseed.cfg:
--------------------------------------------------------------------------------
1 | d-i passwd/user-fullname string Packer_Tutorial
2 | d-i passwd/username string UserName
3 | d-i passwd/user-password password PassWord
4 | d-i passwd/user-password-again password PassWord
5 | d-i user-setup/allow-password-weak boolean true
6 |
7 | d-i partman-auto/disk string /dev/sda
8 | d-i partman-auto/method string regular
9 | d-i partman-partitioning/confirm_write_new_label boolean true
10 | d-i partman/choose_partition select finish
11 | d-i partman/confirm boolean true
12 | d-i partman/confirm_nooverwrite boolean true
13 |
14 | d-i pkgsel/include string open-vm-tools openssh-server
15 |
16 | d-i finish-install/reboot_in_progress note
17 |
--------------------------------------------------------------------------------
/part06-vmware-vsphere/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "vsphere-iso" "example" {
2 | CPUs = 1
3 | RAM = 1024
4 | RAM_reserve_all = true
5 | boot_command = ["",
6 | "",
7 | "",
8 | "",
9 | "",
10 | "",
11 | "",
12 | "",
13 | "",
14 | "", "/install/vmlinuz",
15 | " initrd=/install/initrd.gz", " priority=critical",
16 | " locale=en_US", " file=/media/preseed.cfg",
17 | ""]
18 | disk_controller_type = ["pvscsi"]
19 | floppy_files = ["${path.root}/preseed.cfg"]
20 | guest_os_type = "ubuntu64Guest"
21 | host = "esxi-1.vsphere65.test"
22 | insecure_connection = true
23 | iso_paths = ["[datastore1] ISO/ubuntu-16.04.3-server-amd64.iso"]
24 | network_adapters {
25 | network_card = "vmxnet3"
26 | }
27 | password = var.password
28 | ssh_password = var.ssh_password
29 | ssh_username = var.ssh_username
30 | storage {
31 | disk_size = 32768
32 | disk_thin_provisioned = true
33 | }
34 | username = "root"
35 | vcenter_server = "vcenter.vsphere65.test"
36 | vm_name = "example-ubuntu"
37 | }
38 |
--------------------------------------------------------------------------------
/part06-vmware-vsphere/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | #Public variables
2 | locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
3 |
4 | variable "password" {
5 | type = string
6 | default = "PassWord"
7 | }
8 |
9 | variable "ssh_password" {
10 | type = string
11 | default = "PassWord"
12 | }
13 |
14 | variable "ssh_username" {
15 | type = string
16 | default = "UserName"
17 | }
18 |
--------------------------------------------------------------------------------
/part07-lxc-image/README.md:
--------------------------------------------------------------------------------
1 | ## build LXC container with Packer
2 |
3 | - This code create an Ubuntu 20.04 (focal) image and install a curl in it and export it in output-lxc-focal (rootfs.tar.gz and config)
4 |
5 | - This is written by json format
6 |
7 | - I used the default config to create the image
8 |
9 | - user and password is : ubuntu
10 |
11 | - However, ssh key is also added to VM
12 |
13 | ## Usage
14 | clone project
15 | ```shell
16 | git clone https://github.com/ahmadalibagheri/packer-tutorial.git
17 | cd packer-tutorial/part07-lxc-image
18 | ```
19 | copy your ssh key in current folder
20 |
21 | ```shell
22 | cp $HOME/.ssh/id_rsa.pub authorized_keys
23 | ```
24 |
25 | You can also make sure your configuration is syntactically valid
26 |
27 | ```shell
28 | packer validate lxc.json
29 | ```
30 |
31 | Build the image with the packer build command.
32 |
33 | ```shell
34 | packer build lxc.json
35 | ```
36 |
--------------------------------------------------------------------------------
/part07-lxc-image/config:
--------------------------------------------------------------------------------
1 | lxc.include = /usr/share/lxc/config/ubuntu.common.conf
2 |
3 | # Container specific configuration
4 | lxc.rootfs.path = dir:/var/lib/lxc/u1/rootfs
5 | lxc.uts.name = u1
6 | lxc.arch = amd64
7 |
8 | # Network configuration
9 | lxc.net.0.type = veth
10 | lxc.net.0.link = lxcbr0
11 | lxc.net.0.flags = up
12 | lxc.net.0.hwaddr = 00:16:3e:4f:db:26
13 |
--------------------------------------------------------------------------------
/part07-lxc-image/lxc.json:
--------------------------------------------------------------------------------
1 | {
2 | "variables": {
3 | "HOME": "{{env `HOME`}}"
4 | },
5 | "builders": [
6 | {
7 | "type": "lxc",
8 | "name": "lxc-ubuntu",
9 | "config_file": "./config",
10 | "template_name": "ubuntu",
11 | "template_environment_vars": [
12 | "SUITE=focal"
13 | ]
14 | }
15 | ],
16 | "provisioners": [
17 | {
18 | "type": "shell",
19 | "execute_command": "echo 'ubuntu' | sudo -S -E bash '{{.Path}}'",
20 | "script": "scripts/setup.sh"
21 | },
22 | {
23 | "type": "file",
24 | "source": "./authorized_keys",
25 | "destination": "/home/ubuntu/.ssh/"
26 | }
27 | ]
28 | }
--------------------------------------------------------------------------------
/part07-lxc-image/scripts/setup.sh:
--------------------------------------------------------------------------------
1 | export DEBIAN_FRONTEND=noninteractive
2 | mkdir /home/ubuntu/.ssh/
3 | until ping -c1 www.google.com >/dev/null 2>&1; do :; done
4 | apt-get update
5 | apt-get install -y curl
--------------------------------------------------------------------------------
/part08-docker/README.md:
--------------------------------------------------------------------------------
1 | ## Notes
2 |
3 | ### The packer block contains Packer settings, including in this case the required plugin for this image.
4 |
5 | The source block defines the builder plugin i.e. "ubuntu:zenial" which will be invoked by the build block.
6 |
7 | You will notice that this block includes two labels, the builder type docker and builder name ubuntu.
8 |
9 | Finally the build block defines the tasks that eventually produces an image.
10 |
11 | ### The build block
12 | sources -references the source.docker.ubuntu source defined earlier within the sources block. During build time, Packer will pull the ubuntu:xenial image from the docker registry.
13 |
14 | provisioner - a provisioner is used to automate the modification of the image .
15 |
16 | Provisioners are run against an instance of the image. In this case, a Docker container (an instance) will be started and the provisioner will install the redis-server and create a file called example.txt.
17 |
18 |
19 | post-processors - post-processors only run after the instance has been saved as an image.
20 |
21 | In this example, the post-processors will be run sequentially to tag the image and push it to a repository.
22 |
23 | Post-processors are varied in their function and can for example be used to compress an artefact or push it to the cloud.
24 |
--------------------------------------------------------------------------------
/part08-docker/build.pkr.hcl:
--------------------------------------------------------------------------------
1 | packer {
2 | required_plugins {
3 | docker = {
4 | version = ">= 0.0.7"
5 | source = "github.com/hashicorp/docker"
6 | }
7 | }
8 | }
9 |
10 |
11 | build {
12 | name = var.build_name
13 | sources = [
14 | var.build_source
15 | ]
16 | provisioner "shell" {
17 | environment_vars = [
18 | "FOO=example variable"
19 | ]
20 | inline = [
21 | "echo installing vim package",
22 | "apt-get update",
23 | "apt-get install vim -y",
24 |
25 | ]
26 | }
27 |
28 | post-processors {
29 | post-processor "docker-tag" {
30 | repository = var.build_repo
31 | tag = var.build_tags
32 | }
33 | # Push to docker hub
34 | post-processor "docker-push" {
35 | # dont delete the docker image after pushing it to the cloud.
36 | keep_input_artifact = true
37 | }
38 | # Save to image file
39 | post-processor "docker-save" {
40 | path = "${var.image_name}.tar"
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/part08-docker/source.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "docker" "ubuntu" {
2 | image = var.image_name
3 | # Commit the container to an image
4 | commit = true
5 | }
--------------------------------------------------------------------------------
/part08-docker/variables.pkr.hcl:
--------------------------------------------------------------------------------
1 | variable "image_name" {
2 | type = string
3 | default = "ubuntu:latest"
4 | }
5 |
6 | variable "build_name" {
7 | type = string
8 | default = "my-ubuntu"
9 | }
10 |
11 | variable "build_source" {
12 | type = string
13 | default = "source.docker.ubuntu"
14 | }
15 |
16 | variable "build_repo" {
17 | type = string
18 | default = "hashicorp/packer"
19 | }
20 |
21 | variable "build_tags" {
22 | type = list(string)
23 | default = ["0.6", "anothertag"]
24 | }
25 |
26 | variable "login_server" {
27 | type = string
28 | # when using another servers instead of docker hub put the server address here.
29 | default = ""
30 | }
31 |
32 |
33 |
--------------------------------------------------------------------------------