template := `{ "builders": [{ "type": "amazon-ebs", "region": "{{ .Region }}", "source_ami_filter": { "filters": { "virtualization-type": "hvm", "name": "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*", "root-device-type": "ebs" }, "most_recent": true, "owners": ["099720109477"] }, "ami_name": "example-{{.Timestamp}}", "ami_description": "Example image for {{.Region}}", "instance_type": "t3.micro", "ssh_username": "ubuntu", "user_data_file": "./user-data.sh" }] }` t, err := packer.NewConfigTemplate() if err != nil { log.Fatalf("Error creating ConfigTemplate: %s", err) } output, err := t.Process(template, map[string]string{ "Region": "us-east-1", "Timestamp": time.Now().Format("2006-01-02"), }) if err != nil { log.Fatalf("Error processing template: %s", err) } fmt.Println(output)
templateFile := "template.json" t, err := packer.NewConfigTemplateFromFile(templateFile) if err != nil { log.Fatalf("Error creating ConfigTemplate: %s", err) } output, err := t.Process(map[string]string{ "Region": "us-west-2", "Timestamp": time.Now().Format("2006-01-02"), }) if err != nil { log.Fatalf("Error processing template: %s", err) } fmt.Println(output)This example uses the `NewConfigTemplateFromFile` method to create a `ConfigTemplate` from a JSON file. The method processes the template using specific values for `Region` and `Timestamp` and prints the output to the console. Overall, the `github.com/mitchellh/packer/packer` package in Go provides a powerful tool for configuring Packer templates programmatically.