»The source block

The top-level source block defines reusable builder configuration blocks:

# sources.pkr.hcl
source "amazon-ebs" "example-1" {
    // ...
}
# sources.pkr.hclsource "amazon-ebs" "example-1" {    // ...}

You can start builders by refering to those source blocks from a build block, for example :

build {
  sources = [
    # Here Packer will use a default ami_name when saving the image.
    "source.amazon-ebs.example",
    "source.amazon-ebs.foo",
  ]
}
build {  sources = [    # Here Packer will use a default ami_name when saving the image.    "source.amazon-ebs.example",    "source.amazon-ebs.foo",  ]}

The build-level source block allows to set specific source fields.

build {
  source "source.amazon-ebs.example" {
    # Here Packer will use the provided ami_name instead of defaulting it.
    # Note that fields cannot be overwritten, in other words, you cannot
    # set the 'ami_name' field in the top-level source block.
    ami_name = "specific"
  }
}
build {  source "source.amazon-ebs.example" {    # Here Packer will use the provided ami_name instead of defaulting it.    # Note that fields cannot be overwritten, in other words, you cannot    # set the 'ami_name' field in the top-level source block.    ami_name = "specific"  }}

»Source Variables

It is possible to access the name and type of your source from provisioners and post-processors:

source "null" "first-example" {
  communicator = "none"
}

build {
  name = "roles"

  source "null.first-example" {
    name = "consul"
  }
  source "null.first-example" {
    name = "nomad"
  }
  source "null.first-example" {
    name = "vault"
  }
  sources = ["null.first-example"]

  provisioner "shell-local" {
    inline = ["echo ${source.name} and ${source.type}"]
  }
}

# This will echo something like:
#
# roles.null.consul: consul and null
# roles.null.nomad: nomad and null
# roles.null.vault: vault and null
# roles.null.first-example: first-example and null
source "null" "first-example" {  communicator = "none"}
build {  name = "roles"
  source "null.first-example" {    name = "consul"  }  source "null.first-example" {    name = "nomad"  }  source "null.first-example" {    name = "vault"  }  sources = ["null.first-example"]
  provisioner "shell-local" {    inline = ["echo ${source.name} and ${source.type}"]  }}
# This will echo something like:## roles.null.consul: consul and null# roles.null.nomad: nomad and null# roles.null.vault: vault and null# roles.null.first-example: first-example and null

»Related