Packer Classroom Series – 13/Sep/2019

Packer Variables

Purpose

  • Make packer template generic
  • This increases reusability

Packer Variable Types

  • User Variables
  • Environment Variables

Syntax for Creating Variables

{
    "variables": {
        "variable_name_1": "variable value"
    },
    "builders": [{

    }],
    "provisioners": [
        {

        }
    ]

}

Syntax for using variable

  • User Variables: assume you have defined a user variable called as package
{{user `package`}}
  • Environment Variables: assume you want to use PATH
{{env `PATH`}}

First Version of Pet-clinic

{
    "variables": {
        "aws_access_key": "",
        "aws_secret_key": "",
        "aws_region": "us-west-2",
        "aws_source_ami":  "ami-06f2f779464715dc5"      
    },
    "builders":[
        {
            "type": "amazon-ebs",
            "access_key": "{{user `aws_access_key`}}",
            "ami_name": "petclinic--{{isotime | clean_resource_name}}",
            "instance_type": "t2.micro",
            "region": "{{user `aws_region`}}",
            "secret_key": "{{user `aws_secret_key`}}",
            "source_ami": "{{user `aws_source_ami`}}",
            "ssh_username": "ubuntu"
        }
    ],
    "provisioners":[
        {
            "type": "shell",
            "script": "./deploypetclinic.sh"
        }
    ]
}

deploy petclinic.sh

#!/bin/bash
sudo apt-get update
sudo apt-get install openjdk-8-jdk -y
wget https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/spring-petclinic.jar
echo "Spring petclinic is copied at /home/ubuntu/spring-petclinic.jar"

Commands to use

packer build -var 'aws_access_key=<youraccesskey>' -var 'aws_secret_key=<yoursecretkey> ./aws.json

Excercise:

  1. Create a reusable packer template with tomcat installed
  2. create a reusable packer template with nginx installed
Published
Categorized as Uncategorized Tagged ,

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube