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:
- Create a reusable packer template with tomcat installed
- create a reusable packer template with nginx installed
Like this:
Like Loading...