DevOps Classroom Series – Packer – 04/Jan/2020

Provisioning in Packer

  • Shell Provisioning:
    • Executes Linux commands or shell script(s) once the VM is created.
  • Ansible Provisioning:
  • Chef Provisioning

Applying Shell Provisioner with script

  • Create a file called as installtomcat.sh in the same directory as aws.json
  • Add the following content
#!/bin/bash
sudo apt-get update
sleep 10
sudo apt-get install openjdk-8-jdk -y
sleep 10
sudo apt-get install tomcat8 -y
sleep 10
  • Change the shell provisioner from inline to script as shown below
{
    "builders": [
        {
            "type": "amazon-ebs",
            "ami_name": "tomcatfrompacker",
            "ami_description": "tomcat from packer",
            "access_key": "AKIA3TXJQGAJFDVFHD7C",
            "secret_key": "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",
            "region": "us-west-2",
            "instance_type": "t2.micro",
            "source_ami": "ami-06d51e91cea0dac8d",
            "ssh_username": "ubuntu"

    }],
    "provisioners": [
        {
            "type": "shell",
            "script": "./installtomcat.sh"
        }
    ]
}
  • Note: Refer Here for complete info on shell provisioner.

Variables in Packer

  • Variables in Packer give the flexibility of changing values used in builders/provisioners
  • For variables a section called as varaibles is available in Packer.
  • Variables defined in the Packer Template are called as user variables
  • Basic Structure of packer template with variables is as shown below
{
    "variables": {

    },
    "builders": [],
    "provisioners": []
}
  • To define a variables simple add a key pair/name pair to json
{
    "variables": {
        "myvar": "hello"
    },
    "builders": [],
    "provisioners": []
}
  • To use myvar variable in bulders or provisioners section use the following syntax
"{{user `myvar`}}"
  • To the above packer template if we add variables for access, secret keys and region, source ami the template looks as follows
{
    "variables": {
        "aws_access_key": "",
        "aws_secret_key": "",
        "source_image_id": "ami-06d51e91cea0dac8d",
        "image_region": "us-west-2"

    },
    "builders": [
        {
            "type": "amazon-ebs",
            "ami_name": "tomcatfrompacker",
            "ami_description": "tomcat from packer",
            "access_key": "{{user `aws_access_key`}}",
            "secret_key": "{{user `aws_secret_key`}}",
            "region": "{{user `image_region`}}",
            "instance_type": "t2.micro",
            "source_ami": "{{user `source_image_id`}}",
            "ssh_username": "ubuntu"

    }],
    "provisioners": [
        {
            "type": "shell",
            "script": "./installtomcat.sh"
        }
    ]
}

  • To validate the template use the -var argument
packer validate -var 'aws_access_key=<accesskey>' -var 'aws_secret_key=<secretkey>' .\aws.json
  • To build the image from template
packer build -var 'aws_access_key=<accesskey>' -var 'aws_secret_key=<secretkey>' .\aws.json

  • If you are using many variables -var-file option is available. Create a Json file with all variable names and values and use that file rather than individual -var parameters
packer validate -var-file myvariables.json ./aws.json

packer build -var-file myvariables.json ./aws.json

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner