| 1 |
packer { |
| 2 |
required_plugins { |
| 3 |
digitalocean = { |
| 4 |
version = ">= 1.0.0" |
| 5 |
source = "github.com/hashicorp/digitalocean" |
| 6 |
} |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
variable "token" { |
| 11 |
type = string |
| 12 |
default = env("DIGITALOCEAN_TOKEN") |
| 13 |
} |
| 14 |
|
| 15 |
# "timestamp" template function replacement |
| 16 |
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") } |
| 17 |
|
| 18 |
# All locals variables are generated from variables that uses expressions |
| 19 |
# that are not allowed in HCL2 variables. |
| 20 |
# Read the documentation for locals blocks here: |
| 21 |
# https://www.packer.io/docs/templates/hcl_templates/blocks/locals |
| 22 |
locals { |
| 23 |
image_name = "marketplace-snapshot-${local.timestamp}" |
| 24 |
ssh_user = "root" |
| 25 |
} |
| 26 |
|
| 27 |
source "digitalocean" "caddy_image" { |
| 28 |
api_token = var.token |
| 29 |
image = "ubuntu-20-04-x64" |
| 30 |
region = "nyc3" |
| 31 |
size = "s-1vcpu-1gb" |
| 32 |
snapshot_name = local.image_name |
| 33 |
ssh_username = local.ssh_user |
| 34 |
} |
| 35 |
|
| 36 |
build { |
| 37 |
sources = ["source.digitalocean.caddy_image"] |
| 38 |
|
| 39 |
provisioner "shell" { |
| 40 |
scripts = [ |
| 41 |
"scripts/00-base.sh", |
| 42 |
"scripts/10-firewall.sh", |
| 43 |
"scripts/20-caddy.sh" |
| 44 |
] |
| 45 |
} |
| 46 |
|
| 47 |
provisioner "file" { |
| 48 |
destination = "/etc/fail2ban/jail.local" |
| 49 |
source = "files/fail2ban/jail.local" |
| 50 |
} |
| 51 |
|
| 52 |
provisioner "ansible" { |
| 53 |
galaxy_file = "ansible/requirements.yml" |
| 54 |
playbook_file = "ansible/ansible-playbook.yml" |
| 55 |
user = local.ssh_user |
| 56 |
} |
| 57 |
|
| 58 |
provisioner "shell" { |
| 59 |
scripts = [ |
| 60 |
"scripts/50-services.sh", |
| 61 |
"scripts/90-cleanup.sh", |
| 62 |
"scripts/95-prep.sh", |
| 63 |
"scripts/99-img_check.sh" |
| 64 |
] |
| 65 |
} |
| 66 |
} |