Step 1: Update the repository
First you need to update the current ubuntu repository by following the command.
$ sudo apt-get update
Step 2: Add the packer key
Add the packer key by using the command to install packer using apt-get.
$ sudo curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
Step 3: Add the repository
Add the packer repository by following the command.
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Step 4: Install the Packer
Now you need to update the ubuntu repository and install the packer by using the following command.
$ sudo apt-get update
$ sudo apt-get install packer -y
Step 5: Create a json file to build image using Azure
Its time to create json file to build the azure image, Given code will be create the image of nginx with ubuntu OS, You can change the parameter according to your requirement.
$ sudo vim ubuntuVM.json
Note :- Update the Azure credentials.
{
"builders": [{
"type": "azure-arm",
"client_id": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
"client_secret": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
"tenant_id": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
"subscription_id": "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
"managed_image_resource_group_name": "Terrafrom-A",
"managed_image_name": "v1",
"os_type": "Linux",
"image_publisher": "Canonical",
"image_offer": "UbuntuServer",
"image_sku": "16.04-LTS",
"azure_tags": {
"dept": "Engineering",
"task": "Image deployment"
},
"location": "East US",
"vm_size": "Standard_DS2_v2"
}],
"provisioners": [{
"execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'",
"inline": [
"apt-get update",
"apt-get upgrade -y",
"apt-get -y install nginx",
"/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"
],
"inline_shebang": "/bin/sh -x",
"type": "shell"
}]
}
Save and exit.
Step 6: Command to execute the build process.
Now executive the packer build command to create the Azure image.
$ sudo packer build ubuntuVM.json
You should get output like this.
==> Wait completed after 11 minutes 19 seconds
==> Builds finished. The artifacts of successful builds are:
==> azure-arm: Azure.ResourceManagement.VMImage:
OSType: Linux
ManagedImageResourceGroupName: Terrafrom-A
ManagedImageName: v1
ManagedImageId: /subscriptions/xxxxx-xxxxx-xxxxxx-xxxxxx-xxxxxx-xxxxxx/resourceGroups/Terrafrom-A/providers/Microsoft.Compute/images/v1
ManagedImageLocation: East US
Conclusion
We’ve found in this instructional exercise how to introduce and utilize Packer at its most fundamental level. You can peruse more about working with different stages and how to send numerous machines on various stages simultaneously.