-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packer): add configs for DigitalOcean
- Loading branch information
1 parent
facb834
commit 8b1f531
Showing
4 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
- name: Install common packages on Ubuntu | ||
hosts: all | ||
|
||
tasks: | ||
- name: Update apt package index and install common packages | ||
ansible.builtin.apt: | ||
name: | ||
- build-essential | ||
- software-properties-common | ||
- curl | ||
- git | ||
- tar | ||
- unzip | ||
- zip | ||
- vim | ||
- neovim | ||
- htop | ||
- glances | ||
- ncdu | ||
state: present | ||
update_cache: true | ||
cache_valid_time: 3600 | ||
autoclean: true | ||
autoremove: true | ||
|
||
- name: Wait for apt lock to be released | ||
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done | ||
changed_when: false | ||
|
||
- name: Remove unattended-upgrades | ||
ansible.builtin.apt: | ||
name: unattended-upgrades | ||
state: absent | ||
|
||
- name: Wait for apt lock to be released | ||
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done | ||
changed_when: false | ||
|
||
- name: Upgrade all packages | ||
ansible.builtin.apt: | ||
upgrade: full | ||
autoremove: true | ||
autoclean: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
- name: Install Docker and Docker Compose on Ubuntu | ||
hosts: all | ||
become: yes | ||
|
||
tasks: | ||
- name: Update apt cache | ||
ansible.builtin.apt: | ||
update_cache: yes | ||
cache_valid_time: 3600 | ||
|
||
- name: Install prerequisites | ||
ansible.builtin.apt: | ||
name: | ||
- apt-transport-https | ||
- ca-certificates | ||
- curl | ||
- gnupg | ||
- lsb-release | ||
state: present | ||
|
||
- name: Wait for apt lock to be released | ||
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done | ||
changed_when: false | ||
|
||
- name: Add Docker GPG key | ||
ansible.builtin.apt_key: | ||
url: https://download.docker.com/linux/ubuntu/gpg | ||
state: present | ||
|
||
- name: Add Docker repository | ||
ansible.builtin.apt_repository: | ||
repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" | ||
state: present | ||
|
||
- name: Update apt cache (forced) | ||
ansible.builtin.apt: | ||
update_cache: yes | ||
force_apt_get: yes | ||
|
||
- name: Debug - List available Docker packages | ||
ansible.builtin.shell: apt-cache search docker-ce | ||
register: docker_packages | ||
changed_when: false | ||
|
||
- name: Debug - Show available Docker packages | ||
ansible.builtin.debug: | ||
var: docker_packages.stdout_lines | ||
|
||
- name: Install Docker CE, CLI, Containerd and Compose | ||
ansible.builtin.apt: | ||
name: | ||
- docker-ce | ||
- docker-ce-cli | ||
- containerd.io | ||
- docker-compose-plugin | ||
state: present | ||
|
||
- name: Install Docker Module for Python | ||
ansible.builtin.pip: | ||
name: docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
- name: Reboot | ||
hosts: all | ||
|
||
tasks: | ||
- name: Reboot | ||
ansible.builtin.reboot: | ||
connect_timeout: 5 | ||
reboot_timeout: 300 | ||
pre_reboot_delay: 30 | ||
post_reboot_delay: 180 | ||
test_command: uptime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
packer { | ||
required_plugins { | ||
digitalocean = { | ||
version = ">= 1.4.0" | ||
source = "github.com/digitalocean/digitalocean" | ||
} | ||
ansible = { | ||
source = "github.com/hashicorp/ansible" | ||
version = ">= 1.1.0" | ||
} | ||
} | ||
} | ||
|
||
variable "scripts_dir" { default = "digitalocean/scripts" } | ||
|
||
locals { image_version = "${formatdate("YYYYMMDD.hhmm", timestamp())}" } | ||
variable "do_api_token" { | ||
type = string | ||
default = env("DO_API_TOKEN") | ||
|
||
validation { | ||
condition = length(var.do_api_token) > 0 | ||
error_message = "The DO_API_TOKEN environment variable must be set or the -var do_api_token=xxxxx must be used to set the token value." | ||
} | ||
} | ||
|
||
variable "do_size" { default = "s-2vcpu-2gb" } | ||
variable "do_region" { default = "nyc3" } | ||
variable "do_image" { default = "ubuntu-24-04-x64" } | ||
variable "do_image_description" { default = "Ubuntu 24.04 LTS" } | ||
variable "do_os_version" { default = "24.04" } | ||
variable "do_os_flavor" { default = "ubuntu" } | ||
|
||
source "digitalocean" "ubuntu" { | ||
api_token = "${var.do_api_token}" | ||
image = var.do_image | ||
region = var.do_region | ||
size = var.do_size | ||
snapshot_name = "ami-${var.do_os_flavor}-${var.do_os_version}-${local.image_version}" | ||
ssh_username = "root" | ||
} | ||
|
||
build { | ||
name = "ubuntu" | ||
sources = ["source.digitalocean.ubuntu"] | ||
|
||
provisioner "ansible" { | ||
playbook_file = "${var.scripts_dir}/ansible/install-common.yml" | ||
user = "root" | ||
use_proxy = false | ||
ansible_env_vars = [ | ||
"ANSIBLE_HOST_KEY_CHECKING=False", | ||
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3", | ||
"ANSIBLE_STDOUT_CALLBACK=yaml" | ||
] | ||
extra_arguments = [ | ||
"-v" | ||
] | ||
} | ||
|
||
provisioner "ansible" { | ||
playbook_file = "${var.scripts_dir}/ansible/reboot.yml" | ||
user = "root" | ||
use_proxy = false | ||
ansible_env_vars = [ | ||
"ANSIBLE_HOST_KEY_CHECKING=False", | ||
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3", | ||
"ANSIBLE_STDOUT_CALLBACK=yaml" | ||
] | ||
extra_arguments = [ | ||
"-v" | ||
] | ||
} | ||
|
||
provisioner "ansible" { | ||
playbook_file = "${var.scripts_dir}/ansible/install-docker.yml" | ||
user = "root" | ||
use_proxy = false | ||
ansible_env_vars = [ | ||
"ANSIBLE_HOST_KEY_CHECKING=False", | ||
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3", | ||
"ANSIBLE_STDOUT_CALLBACK=yaml" | ||
] | ||
extra_arguments = [ | ||
"-v" | ||
] | ||
} | ||
|
||
post-processor "manifest" { | ||
output = "manifest.json" | ||
strip_path = true | ||
} | ||
|
||
hcp_packer_registry { | ||
bucket_name = "digitalocean-ubuntu" | ||
|
||
description = <<EOT | ||
An Ubuntu LTS - Server image with Docker installed. | ||
EOT | ||
|
||
bucket_labels = { | ||
"do_size" = var.do_size | ||
"do_region" = var.do_region | ||
"os_flavor" = var.do_os_flavor | ||
"os_version" = var.do_os_version | ||
} | ||
|
||
build_labels = { | ||
"os_ami_id" = "ami-${var.do_os_flavor}-${var.do_os_version}-${local.image_version}" | ||
"os_base_image" = var.do_image | ||
"os_flavor" = var.do_os_flavor | ||
"os_version" = var.do_os_version | ||
} | ||
} | ||
} |