adding playbooks
This commit is contained in:
0
.ansible/.lock
Normal file
0
.ansible/.lock
Normal file
65
bootstrap.sh
65
bootstrap.sh
@@ -1,65 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------
|
||||
# firstrun.sh – One‑shot VM bootstrap script.
|
||||
#
|
||||
# Run this via a systemd unit on first boot. The script will:
|
||||
# • Check /apps/firstrun (marker file) to skip if already ran
|
||||
# • Clone a repo into /apps/$HOSTNAME
|
||||
# • Execute an Ansible playbook that installs Docker
|
||||
# • Restore data from /data if it is empty/missing
|
||||
# • Create a systemd service that runs docker‑compose
|
||||
# • Log everything to /apps/firstrun.log
|
||||
# • Optionally send a Discord webhook (placeholder)
|
||||
#
|
||||
# ------------------------------------------------------------
|
||||
set -euo pipefail
|
||||
|
||||
# ---------- CONFIG ----------
|
||||
REPO_URL="https://github.com/tolerryan/Watari-Ansible.git" # <-- change this!
|
||||
DISCORD_WEBHOOK="" # <-- empty for now
|
||||
|
||||
LOG_FILE="/apps/firstrun.log"
|
||||
MARKER_FILE="/apps/firstrun"
|
||||
|
||||
HOSTNAME="$(hostname -s)"
|
||||
APP_DIR="/apps/"
|
||||
DATA_DIR="/data"
|
||||
|
||||
# ---------- HELPER ----------
|
||||
log() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "${LOG_FILE}"
|
||||
}
|
||||
|
||||
error_exit() {
|
||||
log "ERROR: $*"
|
||||
# Placeholder for Discord failure webhook
|
||||
if [[ -n "$DISCORD_WEBHOOK" ]]; then
|
||||
curl -s -X POST "$DISCORD_WEBHOOK" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "{\"content\": \"Firstrun failed on ${HOSTNAME}: $*\"}"
|
||||
fi
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ---------- 1. Marker check ----------
|
||||
if [[ -f "${MARKER_FILE}" && -s "${MARKER_FILE}" ]]; then
|
||||
log "Marker file exists – script has already run. Exiting."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ---------- 2. Prepare logging ----------
|
||||
mkdir -p "$(dirname "${LOG_FILE}")"
|
||||
touch "${LOG_FILE}"
|
||||
chmod 640 "${LOG_FILE}"
|
||||
log "=== Starting firstrun on ${HOSTNAME} ==="
|
||||
|
||||
|
||||
|
||||
# ---------- 3. Clone the repo ----------
|
||||
if [[ ! -d "${APP_DIR}" ]]; then
|
||||
log "Cloning repository to ${APP_DIR}"
|
||||
git clone --depth 1 "${REPO_URL}" "${APP_DIR}" 2>&1 | tee -a "${LOG_FILE}"
|
||||
else
|
||||
log "App directory already exists – pulling latest changes"
|
||||
cd "${APP_DIR}" && git pull origin main 2>&1 | tee -a "${LOG_FILE}"
|
||||
fi
|
||||
@@ -55,23 +55,14 @@ log "=== Starting firstrun on ${HOSTNAME} ==="
|
||||
|
||||
|
||||
|
||||
# ---------- 3. Clone the repo ----------
|
||||
if [[ ! -d "${APP_DIR}" ]]; then
|
||||
log "Cloning repository to ${APP_DIR}"
|
||||
git clone --depth 1 "${REPO_URL}" "${APP_DIR}" 2>&1 | tee -a "${LOG_FILE}"
|
||||
else
|
||||
log "App directory already exists – pulling latest changes"
|
||||
cd "${APP_DIR}" && git pull origin main 2>&1 | tee -a "${LOG_FILE}"
|
||||
fi
|
||||
|
||||
# ---------- 4. Run Ansible playbook (Docker setup) ----------
|
||||
ANSIBLE_PLAYBOOK="${APP_DIR}/ansible/playbooks/setup-docker.yml"
|
||||
# ---------- 4. Run Ansible playbook (lxc setup) ----------
|
||||
ANSIBLE_PLAYBOOK="${APP_DIR}/ansible/playbooks/setup-lxc.yml"
|
||||
|
||||
if [[ ! -f "${ANSIBLE_PLAYBOOK}" ]]; then
|
||||
error_exit "Ansible playbook not found: ${ANSIBLE_PLAYBOOK}"
|
||||
fi
|
||||
|
||||
log "Running Ansible Docker‑setup playbook"
|
||||
log "Running Ansible lxc‑setup playbook"
|
||||
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
|
||||
-i localhost, \
|
||||
--connection=local \
|
||||
|
||||
22
gitpull.sh
Normal file
22
gitpull.sh
Normal file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env bash
|
||||
# ------------------------------------------------------------
|
||||
# firstrun.sh – One‑shot VM bootstrap script.
|
||||
#
|
||||
# Run this via a systemd unit on first boot. The script will:
|
||||
# • Check /apps/firstrun (marker file) to skip if already ran
|
||||
# • Clone a repo into /apps/$HOSTNAME
|
||||
# • Execute an Ansible playbook that installs Docker
|
||||
# • Restore data from /data if it is empty/missing
|
||||
# • Create a systemd service that runs docker‑compose
|
||||
# • Log everything to /apps/firstrun.log
|
||||
# • Optionally send a Discord webhook (placeholder)
|
||||
#
|
||||
# ------------------------------------------------------------
|
||||
set -euo pipefail
|
||||
|
||||
APP_DIR="/apps/"
|
||||
|
||||
cd $APP_DIR
|
||||
GIT_SSH_COMMAND="ssh -i ~/.ssh/github_ro" git pull
|
||||
|
||||
bash ./firstsetup.sh
|
||||
19
playbooks/setup-debcache.yml
Normal file
19
playbooks/setup-debcache.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
- name: Configure apt proxy
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/apt/apt.conf.d/proxy
|
||||
line: 'Acquire::http { Proxy "http://192.168.0.60:3142"; }'
|
||||
state: present
|
||||
create: yes
|
||||
mode: '0644'
|
||||
|
||||
- name: Set up apt proxy configuration
|
||||
copy:
|
||||
content: |
|
||||
Acquire::http::Proxy "http://debcache:3142";
|
||||
dest: /etc/apt/apt.conf.d/00cacher
|
||||
mode: '0644'
|
||||
|
||||
- name: Update apt cache
|
||||
ansible.builtin.apt:
|
||||
update_cache: yes
|
||||
cache_valid_time: 3600
|
||||
20
playbooks/setup-lxc.yaml
Normal file
20
playbooks/setup-lxc.yaml
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
- hosts: localhost
|
||||
become: yes
|
||||
gather_facts: true
|
||||
|
||||
tasks:
|
||||
- name: set-debcache.yml - Setup apt proxy debcache
|
||||
import_tasks: ./setup-debcache.yml
|
||||
|
||||
- name: Upgrade-packages.yml - update packages
|
||||
import_tasks: ./upgrade-packages.yml
|
||||
|
||||
- name: Install-docker.yml - setting up docker
|
||||
import_tasks: ./install-docker.yml
|
||||
|
||||
- name: Setup-netbird.yml - setting up netbird mesh vpn
|
||||
import_tasks: ./setup-netbird.yml
|
||||
|
||||
- name: setup-restic.yml - setup restic
|
||||
import_tasks: ./setup-restic.yml
|
||||
55
playbooks/setup-netbird.yaml
Normal file
55
playbooks/setup-netbird.yaml
Normal file
@@ -0,0 +1,55 @@
|
||||
- name: Install required packages
|
||||
apt:
|
||||
name:
|
||||
- curl
|
||||
- gnupg
|
||||
- ca-certificates
|
||||
state: present
|
||||
update_cache: no
|
||||
|
||||
- name: Configure network buffer sizes
|
||||
ansible.posix.sysctl:
|
||||
name: "{{ item.key }}"
|
||||
value: "{{ item.value }}"
|
||||
state: present
|
||||
reload: yes
|
||||
ignoreerrors: yes
|
||||
loop:
|
||||
- { key: 'net.core.rmem_max', value: '7500000' }
|
||||
- { key: 'net.core.wmem_max', value: '7500000' }
|
||||
|
||||
- name: Add repository with key
|
||||
ansible.builtin.deb822_repository:
|
||||
name: netbird
|
||||
types: [deb]
|
||||
uris: "https://pkgs.netbird.io/debian"
|
||||
components: [main]
|
||||
suites: ["stable"]
|
||||
signed_by: https://pkgs.netbird.io/debian/public.key
|
||||
state: present
|
||||
enabled: true
|
||||
|
||||
- name: Install netbird
|
||||
apt:
|
||||
name:
|
||||
- netbird
|
||||
state: present
|
||||
update_cache: yes
|
||||
|
||||
- name: Enable netbird service
|
||||
systemd:
|
||||
name: netbird
|
||||
state: started
|
||||
enabled: yes
|
||||
|
||||
- name: Check NetBird status
|
||||
ansible.builtin.command: netbird status
|
||||
register: netbird_status
|
||||
ignore_errors: yes
|
||||
changed_when: False
|
||||
|
||||
- name: Start NetBird if needed
|
||||
ansible.builtin.command: netbird up --management-url https://api.netbird.io --setup-key "{{ netbird_setup_key }}"
|
||||
when: netbird_status.stdout is search("NeedsLogin")
|
||||
vars:
|
||||
ansible_command_timeout: 3600
|
||||
216
playbooks/setup-restic.yaml
Normal file
216
playbooks/setup-restic.yaml
Normal file
@@ -0,0 +1,216 @@
|
||||
- name: Create Restic user
|
||||
user:
|
||||
name: restic
|
||||
uid: 2001
|
||||
group: users
|
||||
state: present
|
||||
create_home: yes
|
||||
home: /home/restic
|
||||
shell: /bin/bash
|
||||
|
||||
- name: Add Authorized key for Restic user
|
||||
ansible.posix.authorized_key:
|
||||
user: restic
|
||||
state: present
|
||||
key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOsDIrV6QIMtpJFNpZEgHnkYgFC6OXMJQFc1JqrnpCzY fake@gmail.com"
|
||||
|
||||
- name: Install Restic Private key
|
||||
copy:
|
||||
dest: /home/restic/.ssh/resticuser.ed25519
|
||||
content: "{{ restic_private }}"
|
||||
owner: restic
|
||||
group: users
|
||||
mode: '0600'
|
||||
|
||||
- name: Copy Restic Repo Pass
|
||||
copy:
|
||||
dest: /home/restic/.resticpassword
|
||||
content: "{{ restic_repopass }}"
|
||||
owner: restic
|
||||
group: users
|
||||
mode: '0600'
|
||||
|
||||
- name: Copy restic.sh
|
||||
copy:
|
||||
dest: /home/restic/restic.sh
|
||||
content: |
|
||||
#!/bin/bash
|
||||
cd /home/restic/
|
||||
# Check if a flag was passed
|
||||
if [[ "$#" -lt 2 ]]; then
|
||||
echo "Usage: $0 [--backup] [--forget] [--check] [--init] dirtobackup"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Initialize variables
|
||||
backup=false
|
||||
forget=false
|
||||
check=false
|
||||
init=false
|
||||
password=($cat ./.resticpassword)
|
||||
|
||||
last_arg=""
|
||||
|
||||
# Parse arguments
|
||||
case $1 in
|
||||
--backup)
|
||||
backup=true
|
||||
shift
|
||||
;;
|
||||
--forget)
|
||||
forget=true
|
||||
shift
|
||||
;;
|
||||
--check)
|
||||
check=true
|
||||
shift
|
||||
;;
|
||||
--init)
|
||||
init=true
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [--backup] [--forget] [--check] [--init] dirtobackup"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Check if last argument is valid
|
||||
last_arg=$1
|
||||
if [ -n "$last_arg" ]; then
|
||||
if [ -d "$last_arg" ]; then
|
||||
#Last argument is a directory: $last_arg
|
||||
backupdir=$last_arg
|
||||
elif [ -f "$last_arg" ]; then
|
||||
#Last argument is a file: $last_arg
|
||||
backupdir=$last_arg
|
||||
else
|
||||
echo "Last argument is neither a directory nor a file: $last_arg"
|
||||
fi
|
||||
else
|
||||
echo "No valid argument provided after options"
|
||||
fi
|
||||
|
||||
|
||||
# Execute Restic commands based on flags
|
||||
if $backup; then
|
||||
echo "Backing up... to misamisa"
|
||||
echo "Date: $(date '+%Y-%m-%d_%H-%M-%S')" # Add your Restic backup command here
|
||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) backup --exclude="*lost+found*" $backupdir \
|
||||
| tee backup.out
|
||||
status=$?
|
||||
if [ $status -eq 0 ]; then
|
||||
# If the exit status is 0 (success), send a success message
|
||||
./discord.sh "$(hostname) backup complete"
|
||||
elif [ $status -eq 3 ]; then
|
||||
./discord.sh "$(hostname) backup complete"
|
||||
else
|
||||
# If the exit status is not 0 (failure), send a failure message
|
||||
./discord.sh "$(hostname) Backup has failed"
|
||||
fi
|
||||
echo "Backup completed $(date '+%Y-%m-%d_%H-%M-%S')"
|
||||
fi
|
||||
|
||||
if $forget; then
|
||||
echo "Removing old backups..."
|
||||
# Add your Restic forget command here
|
||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) forget --keep-within-daily 7d --keep-within-weekly 1m --keep-within-monthly 1y
|
||||
if [ $? -eq 0 ]; then
|
||||
# If the exit status is 0 (success), send a success message
|
||||
./discord.sh "$(hostname) forget command completed successfully"
|
||||
else
|
||||
# If the exit status is not 0 (failure), send a failure message
|
||||
./discord.sh "$(hostname) forget command has failed"
|
||||
fi
|
||||
fi
|
||||
|
||||
if $check; then
|
||||
echo "Checking backups..."
|
||||
# Add your Restic check command here
|
||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) check --read-data
|
||||
if [ $? -eq 0 ]; then
|
||||
# If the exit status is 0 (success), send a success message
|
||||
./discord.sh "$(hostname) Restic Verification complete"
|
||||
else
|
||||
# If the exit status is not 0 (failure), send a failure message
|
||||
./discord.sh "$(hostname) Restic Verification failed!! there is an issue"
|
||||
fi
|
||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) unlock
|
||||
|
||||
fi
|
||||
|
||||
if $init; then
|
||||
echo "Init backup..."
|
||||
# Generate password
|
||||
if [[ -z $(grep '[^[:space:]]' ./.resticpassword) ]] ; then
|
||||
echo "Password file empty. generating passwordwq"
|
||||
tr -dc A-Za-z0-9 </dev/urandom | head -c 25 > ./.resticpassword
|
||||
fi
|
||||
restic --password-file ./.resticpassword -r sftp:misamisa://home/restic/$(hostname) init
|
||||
if [ $? -eq 0 ]; then
|
||||
# If the exit status is 0 (success), send a success message
|
||||
./discord.sh "$(hostname) Restic Init complete for $(hostname)"
|
||||
else
|
||||
# If the exit status is not 0 (failure), send a failure message
|
||||
./discord.sh "$(hostname) Restic init failed!! there is an issue on $(hostname)"
|
||||
fi
|
||||
fi
|
||||
owner: restic
|
||||
group: users
|
||||
mode: '0755'
|
||||
|
||||
- name: Copy discord.sh
|
||||
copy:
|
||||
dest: /home/restic/discord.sh
|
||||
content: "{{ discord_webhook }}"
|
||||
owner: restic
|
||||
group: users
|
||||
mode: '0755'
|
||||
|
||||
- name: Create SSH config file
|
||||
copy:
|
||||
dest: /root/.ssh/config
|
||||
content: |
|
||||
Hostname misamisa.netbird.cloud
|
||||
Port 25456
|
||||
User restic
|
||||
IdentityFile /home/restic/.ssh/resticuser.ed25519
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
|
||||
- name: Add known host entry for misamisa
|
||||
become: yes
|
||||
shell: "ssh-keyscan -p 25456 -H misamisa.netbird.cloud >> ~/.ssh/known_hosts"
|
||||
|
||||
- name: Download restic bz2 file
|
||||
get_url:
|
||||
url: "https://github.com/restic/restic/releases/download/v0.18.1/restic_0.18.1_linux_amd64.bz2"
|
||||
dest: "/tmp/restic.bz2"
|
||||
mode: '0755'
|
||||
force: yes
|
||||
register: restic_download
|
||||
|
||||
- name: Extract restic bz2 file
|
||||
ansible.builtin.command:
|
||||
cmd: bunzip2 /tmp/restic.bz2
|
||||
creates: /tmp/restic
|
||||
when: restic_download.changed
|
||||
|
||||
- name: Move restic to /usr/bin/
|
||||
copy:
|
||||
src: "/tmp/restic"
|
||||
dest: "/usr/bin/restic"
|
||||
mode: "0755"
|
||||
owner: root
|
||||
group: root
|
||||
remote_src: yes
|
||||
when: restic_download.changed
|
||||
|
||||
# This can be removed later if needed. used to cleanup existing crontab entries. Leaving incase any older
|
||||
# configured hosts are brought online.
|
||||
- name: Remove crontab entries containing restic.sh for root
|
||||
become: yes
|
||||
shell: crontab -l -u root | grep -v restic.sh | crontab -u root -
|
||||
register: cron_output
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
24
playbooks/upgrade-packages.yaml
Normal file
24
playbooks/upgrade-packages.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
- name: Update apt cache
|
||||
ansible.builtin.apt:
|
||||
update_cache: yes
|
||||
cache_valid_time: 3600
|
||||
|
||||
- name: Upgrade all packages
|
||||
ansible.builtin.apt:
|
||||
upgrade: dist
|
||||
force_apt_get: yes
|
||||
|
||||
- name: Check if reboot is required
|
||||
ansible.builtin.stat:
|
||||
path: /var/run/reboot-required
|
||||
get_checksum: no
|
||||
register: reboot_required
|
||||
|
||||
- name: Reboot the system if required
|
||||
ansible.builtin.reboot:
|
||||
msg: "{{ reboot_message }}"
|
||||
when: reboot_required.stat.exists
|
||||
|
||||
- name: Clean up old packages
|
||||
ansible.builtin.apt:
|
||||
autoclean: yes
|
||||
Reference in New Issue
Block a user