Compare commits
16 Commits
main
...
6a900ecbc4
Author | SHA1 | Date | |
---|---|---|---|
6a900ecbc4
|
|||
400ce1590e
|
|||
00cad09f37
|
|||
d005032c97
|
|||
69e609d522
|
|||
46303021f5
|
|||
2506402fae
|
|||
51a0ca3938
|
|||
e1c0eb6265
|
|||
2bba6dfa85
|
|||
04e660c838
|
|||
97995a95dd
|
|||
f75abf978a
|
|||
0a479039e2
|
|||
edbd1cd9f9
|
|||
72d3bded74
|
30
.github/workflows/infra.yaml
vendored
30
.github/workflows/infra.yaml
vendored
@@ -4,14 +4,15 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
paths:
|
paths:
|
||||||
- infra/**-playbook.yaml
|
- infra/**playbook.yaml
|
||||||
- .github/workflows/infra.yaml
|
- .github/workflows/infra.yaml
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
env:
|
env:
|
||||||
DEPLOY: ${{ github.ref == 'refs/heads/main' && ((startsWith(github.event.head_commit.message, '[deploy-all]') && 'all') || ('some')) || 'none' }}
|
# DEPLOY: ${{ github.ref == 'refs/heads/main' && ((startsWith(github.event.head_commit.message, '[deploy-all]') && 'all') || ('some')) || 'none' }}
|
||||||
|
DEPLOY: all
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
ansible-playbooks:
|
ansible-playbooks:
|
||||||
@@ -23,20 +24,17 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: ${{ env.DEPLOY == 'some' && 2 || 1 }}
|
fetch-depth: ${{ env.DEPLOY == 'some' && 2 || 1 }}
|
||||||
|
|
||||||
- name: Setup Python
|
|
||||||
uses: actions/setup-python@v4
|
|
||||||
with:
|
|
||||||
python-version: "3.11"
|
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
|
apt update
|
||||||
|
apt install -y python3-pip
|
||||||
pip3 install -r requirements.txt
|
pip3 install -r requirements.txt
|
||||||
ansible-galaxy collection install community.general
|
ansible-galaxy collection install community.general
|
||||||
|
|
||||||
- name: Check playbooks
|
- name: Check playbooks
|
||||||
run: |
|
run: |
|
||||||
for file in $(find . -wholename "*/infra/*-playbook.yaml" -type f); do
|
for file in $(find . -wholename "*/infra/*playbook.yaml" -type f); do
|
||||||
ansible-playbook --inventory ./inventory --check "$file"
|
ansible-playbook --inventory ./inventory --syntax-check "$file"
|
||||||
done
|
done
|
||||||
|
|
||||||
- name: Get changed playbooks
|
- name: Get changed playbooks
|
||||||
@@ -44,7 +42,7 @@ jobs:
|
|||||||
if: env.DEPLOY == 'some'
|
if: env.DEPLOY == 'some'
|
||||||
uses: tj-actions/changed-files@v38
|
uses: tj-actions/changed-files@v38
|
||||||
with:
|
with:
|
||||||
files: infra/**/*-playbook.yaml
|
files: infra/**/*playbook.yaml
|
||||||
|
|
||||||
- name: Get playbooks
|
- name: Get playbooks
|
||||||
id: playbooks
|
id: playbooks
|
||||||
@@ -53,12 +51,19 @@ jobs:
|
|||||||
if [[ "${{ env.DEPLOY }}" == "some" ]]; then
|
if [[ "${{ env.DEPLOY }}" == "some" ]]; then
|
||||||
export TO_RUN="${{ steps.files.outputs.all_changed_files }}"
|
export TO_RUN="${{ steps.files.outputs.all_changed_files }}"
|
||||||
else
|
else
|
||||||
export TO_RUN="$(find . -wholename './infra/*-playbook.yaml' -type f)"
|
export TO_RUN="$(find . -wholename './infra/*playbook.yaml' -type f)"
|
||||||
fi
|
fi
|
||||||
export TO_RUN="$( echo -n $TO_RUN | tr ' ' '\n' | sort | tr '\n' ' ' )" # run things in order :)
|
export TO_RUN="$( echo -n $TO_RUN | tr ' ' '\n' | sort | tr '\n' ' ' )" # run things in order :)
|
||||||
echo "will run playbooks: $TO_RUN"
|
echo "will run playbooks: $TO_RUN"
|
||||||
echo "to_run=$TO_RUN" >> "$GITHUB_OUTPUT"
|
echo "to_run=$TO_RUN" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Setup environment
|
||||||
|
if: env.DEPLOY != 'none' && steps.playbooks.outputs.to_run != ''
|
||||||
|
run: |
|
||||||
|
mkdir -p -m 700 ~/.ssh
|
||||||
|
echo "${{ secrets.SSH_PRIVATE }}" > ~/.ssh/id_rsa
|
||||||
|
chmod 600 ~/.ssh/id_rsa
|
||||||
|
|
||||||
- name: Run playbooks
|
- name: Run playbooks
|
||||||
if: env.DEPLOY != 'none' && steps.playbooks.outputs.to_run != ''
|
if: env.DEPLOY != 'none' && steps.playbooks.outputs.to_run != ''
|
||||||
env:
|
env:
|
||||||
@@ -66,4 +71,5 @@ jobs:
|
|||||||
PROXMOX_USER: ${{ secrets.PROXMOX_USER }}
|
PROXMOX_USER: ${{ secrets.PROXMOX_USER }}
|
||||||
PROXMOX_TOKEN_ID: ${{ secrets.PROXMOX_TOKEN_ID }}
|
PROXMOX_TOKEN_ID: ${{ secrets.PROXMOX_TOKEN_ID }}
|
||||||
PROXMOX_TOKEN_SECRET: ${{ secrets.PROXMOX_TOKEN_SECRET }}
|
PROXMOX_TOKEN_SECRET: ${{ secrets.PROXMOX_TOKEN_SECRET }}
|
||||||
run: ansible-playbook --inventory ./inventory ${{ steps.playbooks.outputs.to_run }}
|
SSH_PUBLIC: ${{ secrets.SSH_PUBLIC }}
|
||||||
|
run: ansible-playbook --inventory ./inventory ${{ steps.playbooks.outputs.to_run }} -vv
|
||||||
|
@@ -7,4 +7,4 @@ This repository contains any automations used in deploying *.koval.net services.
|
|||||||
All 'managed' infrastructure is deployed and provisioned with [Ansible](https://www.ansible.com/).
|
All 'managed' infrastructure is deployed and provisioned with [Ansible](https://www.ansible.com/).
|
||||||
However, some 'unmanaged' (manually managed) resources also exist - primarily everything required for this repository to work (I don't want to make a dependency loop).
|
However, some 'unmanaged' (manually managed) resources also exist - primarily everything required for this repository to work (I don't want to make a dependency loop).
|
||||||
|
|
||||||
Ansible playbooks are ran in alphanumerical order and are expected idempotent.
|
Ansible playbooks are ran in alphanumerical order and are expected to be idempotent.
|
||||||
|
110
infra/cloud/0000_proxmox_playbook.yaml
Normal file
110
infra/cloud/0000_proxmox_playbook.yaml
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
- name: Provision cloud Proxmox VM
|
||||||
|
hosts: cloud
|
||||||
|
connection: ansible.builtin.local
|
||||||
|
gather_facts: false
|
||||||
|
vars:
|
||||||
|
api_user: "{{ lookup('ansible.builtin.env', 'PROXMOX_USER') }}"
|
||||||
|
api_host: "{{ lookup('ansible.builtin.env', 'PROXMOX_HOST' ) }}"
|
||||||
|
api_token_id: "{{ lookup('ansible.builtin.env', 'PROXMOX_TOKEN_ID') }}"
|
||||||
|
api_token_secret: "{{ lookup('ansible.builtin.env', 'PROXMOX_TOKEN_SECRET') }}"
|
||||||
|
ssh_public: "{{ lookup('ansible.builtin.env', 'SSH_PUBLIC') }}"
|
||||||
|
vmname: "{{ inventory_hostname | regex_replace('^([^\\.]+)\\..+$', '\\1') }}"
|
||||||
|
node: pve
|
||||||
|
module_defaults:
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
api_user: "{{ api_user }}"
|
||||||
|
api_host: "{{ api_host }}"
|
||||||
|
api_token_id: "{{ api_token_id }}"
|
||||||
|
api_token_secret: "{{ api_token_secret }}"
|
||||||
|
name: "{{ vmname }}"
|
||||||
|
node: "{{ node }}"
|
||||||
|
community.general.proxmox_nic:
|
||||||
|
api_user: "{{ api_user }}"
|
||||||
|
api_host: "{{ api_host }}"
|
||||||
|
api_token_id: "{{ api_token_id }}"
|
||||||
|
api_token_secret: "{{ api_token_secret }}"
|
||||||
|
name: "{{ vmname }}"
|
||||||
|
community.general.proxmox_disk:
|
||||||
|
api_user: "{{ api_user }}"
|
||||||
|
api_host: "{{ api_host }}"
|
||||||
|
api_token_id: "{{ api_token_id }}"
|
||||||
|
api_token_secret: "{{ api_token_secret }}"
|
||||||
|
name: "{{ vmname }}"
|
||||||
|
tasks:
|
||||||
|
# Initial setup
|
||||||
|
- name: Create VM
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
clone: "{{ node }}-debian-12"
|
||||||
|
storage: nvme
|
||||||
|
register: create
|
||||||
|
- name: Wait for status
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
state: current
|
||||||
|
register: vm
|
||||||
|
retries: 30
|
||||||
|
delay: 10
|
||||||
|
until: vm.status is defined
|
||||||
|
|
||||||
|
# Networking and initial config
|
||||||
|
- name: Add HOME NIC
|
||||||
|
community.general.proxmox_nic:
|
||||||
|
interface: net0
|
||||||
|
firewall: false
|
||||||
|
bridge: HOME
|
||||||
|
- name: Add SRV NIC
|
||||||
|
community.general.proxmox_nic:
|
||||||
|
interface: net1
|
||||||
|
firewall: false
|
||||||
|
bridge: SRV
|
||||||
|
- name: Configure cloud-init
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
update: true
|
||||||
|
ciuser: debian
|
||||||
|
sshkeys: "{{ ssh_public }}"
|
||||||
|
ipconfig:
|
||||||
|
ipconfig0: ip=dhcp,ip6=auto
|
||||||
|
ipconfig1: ip=dhcp
|
||||||
|
|
||||||
|
# Initial boot
|
||||||
|
# For some reason debian cloud images don't use
|
||||||
|
# cloud-init for networking on first boot (cloud-init files
|
||||||
|
# are regenerated AFTER networking starts). But we need the
|
||||||
|
# hostname to be registered with DHCP later on so ¯\_(ツ)_/¯
|
||||||
|
- name: Initial boot
|
||||||
|
when: create.changed is true
|
||||||
|
block:
|
||||||
|
- name: Start
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
state: started
|
||||||
|
register: start
|
||||||
|
- name: Wait 1 min # Initial apt update, apt upgrade, cloud-init
|
||||||
|
ansible.builtin.wait_for:
|
||||||
|
timeout: 60
|
||||||
|
|
||||||
|
# VM Configuration
|
||||||
|
- name: Resize root disk
|
||||||
|
community.general.proxmox_disk:
|
||||||
|
disk: scsi0
|
||||||
|
size: 16
|
||||||
|
state: resized
|
||||||
|
- name: Create data disk
|
||||||
|
community.general.proxmox_disk:
|
||||||
|
disk: scsi1
|
||||||
|
backup: true
|
||||||
|
storage: nvme
|
||||||
|
size: 2048
|
||||||
|
- name: Update VM
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
update: true
|
||||||
|
agent: enabled=1
|
||||||
|
tags:
|
||||||
|
- debian-12
|
||||||
|
- managed
|
||||||
|
onboot: true
|
||||||
|
cores: 2
|
||||||
|
memory: 2048
|
||||||
|
|
||||||
|
- name: Retart VM
|
||||||
|
community.general.proxmox_kvm:
|
||||||
|
state: restarted
|
||||||
|
timeout: 60
|
47
infra/cloud/0001_initialise_playbook.yaml
Normal file
47
infra/cloud/0001_initialise_playbook.yaml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
- name: Initialise VM
|
||||||
|
hosts: cloud
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Wait for connection
|
||||||
|
ansible.builtin.wait_for_connection:
|
||||||
|
timeout: 300
|
||||||
|
- name: Install system packages
|
||||||
|
ansible.builtin.apt:
|
||||||
|
update_cache: true
|
||||||
|
pkg:
|
||||||
|
- qemu-guest-agent
|
||||||
|
- parted
|
||||||
|
become: true
|
||||||
|
- name: Enable qemu-guest-agent
|
||||||
|
ansible.builtin.systemd:
|
||||||
|
name: qemu-guest-agent
|
||||||
|
state: started
|
||||||
|
enabled: true
|
||||||
|
become: true
|
||||||
|
|
||||||
|
- name: Create data partition
|
||||||
|
community.general.parted:
|
||||||
|
device: /dev/disk/by-path/pci-0000:00:05.0-scsi-0:0:0:1
|
||||||
|
label: gpt
|
||||||
|
name: data
|
||||||
|
number: 1
|
||||||
|
state: present
|
||||||
|
become: true
|
||||||
|
- name: Create data filesystem
|
||||||
|
community.general.filesystem:
|
||||||
|
dev: /dev/disk/by-path/pci-0000:00:05.0-scsi-0:0:0:1-part1
|
||||||
|
fstype: ext4
|
||||||
|
become: true
|
||||||
|
- name: Mount data partition
|
||||||
|
ansible.posix.mount:
|
||||||
|
src: /dev/disk/by-path/pci-0000:00:05.0-scsi-0:0:0:1-part1
|
||||||
|
path: /mnt/data
|
||||||
|
fstype: ext4
|
||||||
|
state: mounted
|
||||||
|
become: true
|
||||||
|
- name: Set data partition permissions
|
||||||
|
ansible.builtin.file:
|
||||||
|
path: /mnt/data
|
||||||
|
owner: debian
|
||||||
|
group: debian
|
||||||
|
become: true
|
47
infra/cloud/0002_docker_playbook.yaml
Normal file
47
infra/cloud/0002_docker_playbook.yaml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
- name: Install software
|
||||||
|
hosts: cloud
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: Wait for connection
|
||||||
|
ansible.builtin.wait_for_connection:
|
||||||
|
timeout: 300
|
||||||
|
- name: Install dependencies
|
||||||
|
ansible.builtin.apt:
|
||||||
|
update_cache: true
|
||||||
|
pkg:
|
||||||
|
- curl
|
||||||
|
- python3-apt
|
||||||
|
- gpg
|
||||||
|
become: true
|
||||||
|
- name: Add docker key
|
||||||
|
ansible.builtin.apt_key:
|
||||||
|
url: https://download.docker.com/linux/debian/gpg
|
||||||
|
keyring: /etc/apt/keyrings/docker.gpg
|
||||||
|
become: true
|
||||||
|
- name: Add docker repo
|
||||||
|
ansible.builtin.apt_repository:
|
||||||
|
repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable
|
||||||
|
become: true
|
||||||
|
- name: Install docker
|
||||||
|
ansible.builtin.apt:
|
||||||
|
update_cache: true
|
||||||
|
pkg:
|
||||||
|
- docker-ce
|
||||||
|
- docker-ce-cli
|
||||||
|
- containerd.io
|
||||||
|
- docker-buildx-plugin
|
||||||
|
- docker-compose-plugin
|
||||||
|
become: true
|
||||||
|
- name: Add user to docker group
|
||||||
|
ansible.builtin.user:
|
||||||
|
user: debian
|
||||||
|
groups:
|
||||||
|
- docker
|
||||||
|
append: true
|
||||||
|
become: true
|
||||||
|
- name: Enable docker
|
||||||
|
ansible.builtin.systemd:
|
||||||
|
name: docker
|
||||||
|
state: started
|
||||||
|
enabled: true
|
||||||
|
become: true
|
24
infra/cloud/0003_app_playbook.yaml
Normal file
24
infra/cloud/0003_app_playbook.yaml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
- name: Deploy app
|
||||||
|
hosts: cloud
|
||||||
|
gather_facts: false
|
||||||
|
vars:
|
||||||
|
project_src: /home/debian/cloud
|
||||||
|
module_defaults:
|
||||||
|
community.docker.docker_compose:
|
||||||
|
project_src: "{{ project_src }}"
|
||||||
|
tasks:
|
||||||
|
- name: Wait for connection
|
||||||
|
ansible.builtin.wait_for_connection:
|
||||||
|
timeout: 300
|
||||||
|
- name: Copy project
|
||||||
|
ansible.builtin.copy:
|
||||||
|
directory_mode: true
|
||||||
|
src: ./cloud
|
||||||
|
mode: "0744"
|
||||||
|
dest: "{{ project_src }}"
|
||||||
|
- name: Docker compose down
|
||||||
|
community.docker.docker_compose:
|
||||||
|
state: absent
|
||||||
|
- name: Docker compose up
|
||||||
|
community.docker.docker_compose:
|
||||||
|
state: present
|
5
infra/cloud/cloud/docker-compose.yaml
Normal file
5
infra/cloud/cloud/docker-compose.yaml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
version: "3"
|
||||||
|
|
||||||
|
services:
|
||||||
|
test:
|
||||||
|
image: nginx
|
@@ -7,6 +7,13 @@ proxmox:
|
|||||||
pve.mgmt.home.local.koval.net:
|
pve.mgmt.home.local.koval.net:
|
||||||
pve2.mgmt.home.local.koval.net:
|
pve2.mgmt.home.local.koval.net:
|
||||||
managed:
|
managed:
|
||||||
|
children:
|
||||||
|
cloud:
|
||||||
hosts:
|
hosts:
|
||||||
|
cloud.srv.home.local.koval.net:
|
||||||
|
vars:
|
||||||
|
ansible_user: debian
|
||||||
|
ansible_ssh_private_key_file: ~/.ssh/id_rsa
|
||||||
|
ansible_ssh_common_args: -o StrictHostKeyChecking=accept-new # TODO: Improve this
|
||||||
unmanaged:
|
unmanaged:
|
||||||
hosts:
|
hosts:
|
||||||
|
Reference in New Issue
Block a user