Packer


Packer is from HashiCorp and is another provisioning tool

a packer folder in where packer.exe resides on Windows will contain

1.variables.json
2.template.json
3.provision.sh

variables.json will contain access key info

{
  "aws_access_key": "",
  "aws_secret_key": ""
}

On Linux :

$ packer build -var-file=variables.json template.json

On Windows :

packer build -var-file variables.json template.json

We are using a debian 9 stretch ami and provisioning it

template.json

{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-003f19e0e687de1cd",
"instance_type": "t2.nano",
"ssh_username": "admin",
"ami_name": "MiddleTier-{{isotime | clean_ami_name}}",
     "ami_description": "EC2 AMI Debian 9.1", 
     "tags": {
       "role": "web_Server"
     },
     "run_tags": {
       "role": "production_Web"
     }
    }
],

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

provision.sh

#!/bin/bash
set -e
#provision.sh
sudo apt-get update
echo "apt-get update done."
sudo apt-get -y upgrade
sudo apt-get install -y python-dev python-pip
sudo pip install ansible
sudo timedatectl set-timezone Australia/Sydney
sudo localectl set-locale LANG=en_US.utf8
sudo wget https://s3.amazonaws.com/packeramidemo/i_playbook.yml
echo "Running build."
sudo ansible-playbook i_playbook.yml

playbook.yml


---
# playbook.yml
- name: 'Provision Image'
  hosts: default
  become: true

  tasks:
    - name: install Apache
      package:
        name: 'httpd'
        state: present

Packer run step which will build a custom ami and will be a private ami

packer build -var-file variables.json template.json
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": "",
    "aws_region": ""
  },
    "builders": [
        {
            "ami_name": "HAProxy - {{user `haproxy_role`}} layer - Docker {{ timestamp }}",
            "instance_type": "t2.nano",
            "access_key": "{{user `aws_access_key`}}",
            "secret_key": "{{user `aws_secret_key`}}",
            "region": "{{user `aws_region`}}",
            "source_ami": "ami-4bf3d731",
            "ssh_username": "centos",
                  "ssh_pty" : true,
            "type": "amazon-ebs",

            "tags": {
                "Billing": "{{user `aws_tag_billing`}}",
                "CreatedBy": "{{user `aws_tag_created_by`}}",
                "CustomerFacing": "{{user `aws_tag_customer_facing`}}",
                "Description": "CentOS 7 w/ENA Support & Hardened - with HAProxy docker container, for {{user `haproxy_role`}} layer with updates installed on {{isotime \"2006-01-02\"}}",
                "Environment": "{{user `aws_tag_environment`}}",
                "Name": "HAProxy - {{user `haproxy_role`}} layer - environment {{user `aws_tag_environment`}} {{isotime \"2006-01-02\"}}",
                "PCI": "{{user `aws_tag_pci`}}"
                }
        }
    ],

    "provisioners": [
        
         {
          "type": "shell",
          "script": "centosprovision.sh"
         },
        
        {
            "type": "ansible-local",
            "playbook_file": "./ansible/main.yml"
        },
        {
            "type": "shell",
            "inline": ["sudo reboot"],
            "expect_disconnect": true,
            "remote_folder": "~/"
        }
        
    ]

}

These writings represent my own personal views alone.
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.