packer

Name VM during packer parallel builds


I'm using a VSphere iso builder and I would like to have two separate parallel builds. How can I access the build.name when I'm in the source section (where you have to set the VM_name property)

I tried ${build.name} but this returned an error This object does not have an attribute named "name".

Is there a way I can access or set the VM name per build I create easily? I can't even find the default anywhere in the doco.

I have one source block that defines the VSphere VM (including the property VM_Name) and multiple builders based on that source. If I do a parallel build, I need to set a different VM_Name for each one, but I can't see how.


Solution

  • It would be easier if you post your pkr file. If I understand correctly, you have one source block in which you currently set the name of the virtual machine (vm_name) and two build blocks that define steps to build both types of virtual machines using the same source. What you need are different names for both types of VMs when building.

    source "vsphere-iso" "example" {
        # Common settings without vm_name
        ...
    }
    
    build {
      source "vsphere-iso.example" {
        vm_name = "vm-type-1"
        # Other unique settings
      }
      
      # Provisioners for vm-type-1:
    
    }
    
    build {
      source "vsphere-iso.example" {
        vm_name = "vm-type-2"
        # Other unique settings
      }
      
      # Provisioners for vm-type-2:
    
    }
    

    https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/source

    ${build.name} is contextual variable. It can be used in provisioners and post-processors https://developer.hashicorp.com/packer/docs/templates/hcl_templates/contextual-variables

    A simple example of use:

    source "file" "source_name" {
      content =  "Lorem ipsum dolor sit amet"
    }
    
    build {
      name = "build_name_1"
      source "sources.file.source_name" {
        target = "file_1.txt"
      }
    
      provisioner "shell-local" {
        inline = ["echo ${source.name} ${build.name}"]
      }
    }
    
    build {
      name = "build_name_2"
      source "sources.file.source_name" {
        target  = "file_2.txt"
      }
    
      provisioner "shell-local" {
        inline = ["echo ${source.name} ${build.name}"]
      }
    }
    

    Output

    build_name_1.file.source_name: output will be in this color.
    build_name_2.file.source_name: output will be in this color.
    
    ==> build_name_2.file.source_name: Running local shell script: /tmp/packer-shell3796690681
    ==> build_name_1.file.source_name: Running local shell script: /tmp/packer-shell1750990751
        build_name_1.file.source_name: source_name build_name_1
    Build 'build_name_1.file.source_name' finished after 58 milliseconds 798 microseconds.
        build_name_2.file.source_name: source_name build_name_2
    Build 'build_name_2.file.source_name' finished after 79 milliseconds 217 microseconds.
    
    ==> Wait completed after 79 milliseconds 316 microseconds
    
    ==> Builds finished. The artifacts of successful builds are:
    --> build_name_1.file.source_name: Stored file: file_1.txt
    --> build_name_2.file.source_name: Stored file: file_2.txt