How to generate custom images using packer

I want to create the custom image based out of packer

suppose I wish to install java software and uninstall some software, in that case, how do I add and what exactly need to add in the below highlighted powershell provisioner section?

FYI
I will have to create custom images out of existing images , please suggest highlevel to do so

Hello! any chance you can put the snippet of the packer template in here, not the screenshot - it’s kinda hard to read :slight_smile:

Hi Bruce,

Here is the sameple template

{
  "builders": [{
    "type": "azure-arm",

    "client_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
    "client_secret": "ppppppp-pppp-pppp-pppp-ppppppppppp",
    "tenant_id": "zzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz",
    "subscription_id": "yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyy",

    "managed_image_resource_group_name": "myPackerGroup",
    "managed_image_name": "myPackerImage",

    "os_type": "Windows",
    "image_publisher": "MicrosoftWindowsServer",
    "image_offer": "WindowsServer",
    "image_sku": "2016-Datacenter",

    "communicator": "winrm",
    "winrm_use_ssl": true,
    "winrm_insecure": true,
    "winrm_timeout": "5m",
    "winrm_username": "packer",

    "azure_tags": {
        "dept": "Engineering",
        "task": "Image deployment"
    },

    "build_resource_group_name": "myPackerGroup",
    "vm_size": "Standard_D2_v2"
  }],
  "provisioners": [{
    "type": "powershell",
    "inline": [
      "Add-WindowsFeature Web-Server",
      "while ((Get-Service RdAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
      "while ((Get-Service WindowsAzureGuestAgent).Status -ne 'Running') { Start-Sleep -s 5 }",
      "& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /quiet /quit",
      "while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10  } else { break } }"
    ]
  }]
}

Hi @sk8228402 and thanks for the snippet - much easier to read now :smiling_face:

I think the answer to this question lies in your knowledge of powershell. The provisioner part of a Packer template will log into the machine you’re configuring and execute the shell script. It’s not specific to Packer, it’s just a regular script which Packer executes while making the image.

If you want java installed for example, then how would you do that with a regular Powershell script? Once you know that, you can include it in the inline parts of the provisioner – there’s really not that much magic to it.

I hope I’m answering the question here, but perhaps there’s something I misunderstood?

1 Like

HI Bruce,

Can you please give me high level steps on how to implement automating the creation of custom images from existing image.

I have images in build gallery with 1.0.3 version in Azure.
now I want to install software and upate in to that 1.0.3 version and create 1.0.4 using packer

Please suggest where to start.

2nd question: I want to install following jave software and add env variable to image 1.0.3 and generate 1.0.4

It would be great if you could help me with this, thanks

3rd question:
as i have highlighted more about provsioners in the code of snipped I shared earlier.