fix: Initial ansible setup

This commit is contained in:
lif
2025-10-12 08:21:43 +01:00
parent 069ac709e1
commit 78ed25a25a
15 changed files with 124 additions and 1572 deletions

4
.gitignore vendored
View File

@@ -35,3 +35,7 @@ join-command.sh
# Helm
charts/
*.tgz
# Keys
keys/
old/

121
Makefile
View File

@@ -1,112 +1,13 @@
# Bare Bones Vagrant Makefile
list:
vagrant status
make up:
cd vagrant && vagrant up
make destroy:
cd vagrant && vagrant destroy -f
.PHONY: help start stop destroy status ssh-manager ssh-worker1 ssh-worker2 ssh-worker3 clean \
ansible-ping ansible-setup ansible-deploy ansible-list ansible-facts \
reset-full reset-destroy reset-start reset-test reset-ssh reset-ansible reset-setup reset-deploy
make ansible-master:
cd ansible && ansible-playbook -i ../vagrant/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory master.yml
# Default target
help: ## Show this help message
@echo "Bare Bones Vagrant Management"
@echo "============================"
@echo ""
@echo "Available targets:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
start: ## Start all machines
@echo "Starting all machines..."
./manage.sh start
stop: ## Stop all machines
@echo "Stopping all machines..."
./manage.sh stop
destroy: ## Destroy all machines (permanent)
@echo "Destroying all machines..."
./manage.sh destroy
status: ## Show machine status
@echo "Showing machine status..."
./manage.sh status
ssh-manager: ## Access swarm manager via SSH
@echo "Accessing swarm manager..."
./manage.sh ssh swarm-manager
ssh-worker1: ## Access swarm worker1 via SSH
@echo "Accessing swarm worker1..."
./manage.sh ssh swarm-worker1
ssh-worker2: ## Access swarm worker2 via SSH
@echo "Accessing swarm worker2..."
./manage.sh ssh swarm-worker2
ssh-worker3: ## Access swarm worker3 via SSH
@echo "Accessing swarm worker3..."
./manage.sh ssh swarm-worker3
clean: ## Clean up temporary files
@echo "Cleaning up temporary files..."
rm -rf backup-* *.log *.tmp *.temp
@echo "Cleanup complete!"
# Quick access targets
manager: ssh-manager ## Alias for ssh-manager
w1: ssh-worker1 ## Alias for ssh-worker1
w2: ssh-worker2 ## Alias for ssh-worker2
w3: ssh-worker3 ## Alias for ssh-worker3
# Ansible targets
ansible-ping: ## Test Ansible connectivity to all hosts
@echo "Testing Ansible connectivity..."
ansible all -i inventory -m ping
ansible-setup: ## Run setup playbook to install dependencies
@echo "Installing Ansible roles..."
ansible-galaxy install -r ansible-requirements.yml --force
@echo "Running setup playbook..."
ansible-playbook -i inventory setup-playbook.yml
ansible-deploy: ## Run Docker Swarm deployment playbook
@echo "Running Docker Swarm deployment playbook..."
ansible-playbook -i inventory deploy-playbook.yml
ansible-list: ## List all hosts in inventory
@echo "Listing all hosts..."
ansible all -i inventory --list-hosts
ansible-facts: ## Gather facts from all hosts
@echo "Gathering facts from all hosts..."
ansible all -i inventory -m setup
# Reset and Test targets
reset-full: ## Full destroy/recreate/test cycle
@echo "Running full reset and test cycle..."
./reset-and-test.sh full-reset
reset-destroy: ## Only destroy all machines
@echo "Destroying all machines..."
./reset-and-test.sh destroy-only
reset-start: ## Only start all machines
@echo "Starting all machines..."
./reset-and-test.sh start-only
reset-test: ## Only run tests (assumes machines are running)
@echo "Running tests..."
./reset-and-test.sh test-only
reset-ssh: ## Only test SSH connectivity
@echo "Testing SSH connectivity..."
./reset-and-test.sh ssh-test
reset-ansible: ## Only test Ansible connectivity
@echo "Testing Ansible connectivity..."
./reset-and-test.sh ansible-test
reset-setup: ## Only run setup playbook
@echo "Running setup playbook..."
./reset-and-test.sh setup-only
reset-deploy: ## Only run deployment playbook
@echo "Running deployment playbook..."
./reset-and-test.sh deploy-only
make setup-keys:
mkdir -p ./keys
ssh-keygen -t ed25519 -f ./keys/access -N "" -q

267
README.md
View File

@@ -1,267 +0,0 @@
# Docker Swarm Vagrant Cluster
A **production-ready** Docker Swarm cluster with 1 manager and 3 worker nodes for container orchestration and deployment.
## 🐳 Docker Swarm Features
- **1 Swarm Manager** - Cluster orchestration and management
- **3 Swarm Workers** - Container execution and scaling
- **Overlay Networking** - Secure multi-host container communication
- **Service Discovery** - Built-in DNS and load balancing
- **High Availability** - Automatic failover and service recovery
- **Portainer UI** - Web-based cluster management interface
- **Traefik** - Reverse proxy with automatic service discovery
## 🏗️ Architecture
```
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ swarm-manager │ │ swarm-worker1 │ │ swarm-worker2 │ │ swarm-worker3 │
│ 192.168.56.10 │ │ 192.168.56.11 │ │ 192.168.56.12 │ │ 192.168.56.13 │
│ │ │ │ │ │ │ │
│ - Swarm Manager │ │ - Swarm Worker │ │ - Swarm Worker │ │ - Swarm Worker │
│ - Portainer UI │ │ - Container │ │ - Container │ │ - Container │
│ - Traefik Proxy │ │ Execution │ │ Execution │ │ Execution │
│ - Service │ │ - Load │ │ - Load │ │ - Load │
│ Discovery │ │ Balancing │ │ Balancing │ │ Balancing │
└─────────────────┘ └─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │ │
└───────────────────────┼───────────────────────┼───────────────────────┘
│ │
┌─────────────┴───────────────────────┴─────────────┐
│ Docker Swarm Overlay Network │
│ - Service Discovery │
│ - Load Balancing │
│ - Secure Communication │
└─────────────────────────────────────────────────────┘
```
## 📋 Prerequisites
- **Vagrant** 2.2+
- **VirtualBox** 6.0+ or **libvirt** (KVM)
- **Ansible** 2.9+
- **2GB+ RAM** (512MB per machine + 1GB swap)
- **6GB+ free disk space**
## 🚀 Quick Start
1. **Start all machines:**
```bash
make start
```
2. **Check status:**
```bash
make status
```
3. **Access a machine:**
```bash
make ssh-manager
make ssh-worker1
```
## 🎛️ Management Commands
### Using Make
```bash
make start # Start all machines
make stop # Stop all machines
make destroy # Destroy all machines
make status # Show machine status
make ssh-manager # Access swarm manager
make ssh-worker1 # Access swarm worker1
make ssh-worker2 # Access swarm worker2
make ssh-worker3 # Access swarm worker3
```
### Using Management Script
```bash
./manage.sh start # Start all machines
./manage.sh stop # Stop all machines
./manage.sh destroy # Destroy all machines
./manage.sh status # Show machine status
./manage.sh ssh swarm-manager # Access swarm manager
./manage.sh ssh swarm-worker1 # Access swarm worker1
```
### Using Vagrant Directly
```bash
vagrant up # Start all machines
vagrant halt # Stop all machines
vagrant destroy -f # Destroy all machines
vagrant status # Show machine status
vagrant ssh swarm-manager # Access swarm manager
vagrant ssh swarm-worker1 # Access swarm worker1
```
## 🌐 Network Configuration
- **Swarm Manager**: 192.168.56.10
- **Swarm Worker 1**: 192.168.56.11
- **Swarm Worker 2**: 192.168.56.12
- **Swarm Worker 3**: 192.168.56.13
All machines are connected via a private network and communicate through Docker Swarm overlay networking.
## 🔧 Machine Specifications
- **OS**: Debian 11 (Bullseye)
- **RAM**: 512MB per machine + 1GB swap
- **CPU**: 1 core per machine
- **Disk**: 8GB per machine
## 📁 Project Structure
```
test-vagrant/
├── Vagrantfile # Debian Linux cluster configuration
├── manage.sh # Management script
├── Makefile # Make targets
├── inventory # Ansible inventory file
├── setup-playbook.yml # Setup playbook (dependencies, Python, swap)
├── deploy-playbook.yml # Deployment playbook (apps, services)
├── README.md # This file
└── .gitignore # Git ignore rules
```
## 🛠️ Troubleshooting
### Common Issues
1. **Machines not starting:**
```bash
vagrant status
vagrant up --debug
```
2. **Network issues:**
```bash
vagrant ssh host -c "ping 192.168.56.10"
```
3. **SSH issues:**
```bash
vagrant ssh-config
```
### Useful Commands
```bash
# Check machine status
vagrant status
# View machine details
vagrant ssh-config
# Reload machines
vagrant reload
# Provision machines
vagrant provision
```
## 🐧 Debian Linux Notes
### **Package Management**
```bash
# Update package index
apt update
# Install packages
apt install package-name
# Search packages
apt search keyword
```
### **Common Debian Commands**
```bash
# Check system info
uname -a
cat /etc/os-release
# Check memory usage
free -m
# Check disk usage
df -h
```
## 🎭 Ansible Integration
### **Prerequisites**
```bash
# Install Ansible (on your host machine)
pip install ansible
# Or on Ubuntu/Debian
sudo apt install ansible
```
### **Ansible Commands**
```bash
# Test connectivity to all hosts
make ansible-ping
# Install dependencies (Python, tools, swap)
make ansible-setup
# Deploy applications and services
make ansible-deploy
# List all hosts
make ansible-list
# Gather system facts
make ansible-facts
```
### **Using Management Script**
```bash
# Test Ansible connectivity
./manage.sh ansible ping
# Install dependencies
./manage.sh ansible setup
# Deploy applications
./manage.sh ansible deploy
```
### **Direct Ansible Commands**
```bash
# Test connectivity
ansible all -i inventory -m ping
# Run setup playbook
ansible-playbook -i inventory setup-playbook.yml
# Run deployment playbook
ansible-playbook -i inventory deploy-playbook.yml
# Run on specific hosts
ansible-playbook -i inventory deploy-playbook.yml --limit machines
# Run with verbose output
ansible-playbook -i inventory deploy-playbook.yml -v
```
## 🚀 Next Steps
This is a bare-bones Debian setup. You can extend it by:
1. **Adding provisioning scripts** to install Debian packages
2. **Setting up networking** between machines
3. **Installing Docker** (Debian has excellent Docker support)
4. **Adding lightweight services** (nginx, redis, etc.)
5. **Setting up monitoring** with lightweight tools
## 📄 License
This project is licensed under the MIT License.
---
**Ultra-Lightweight Debian! 🐧⚡**

107
Vagrantfile vendored
View File

@@ -1,107 +0,0 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Global configuration
config.vm.box = "debian/bookworm64"
config.vm.box_version = ">= 12.12.0"
# Disable automatic box update checking
config.vm.box_check_update = false
# Configure SSH
config.ssh.insert_key = true
# Configure shared folders
config.vm.synced_folder ".", "/vagrant", disabled: true
# Configure provider-specific settings
config.vm.provider "virtualbox" do |vb|
vb.name = "bare-bones-cluster"
vb.memory = "512"
vb.cpus = 1
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
# Configure libvirt provider
config.vm.provider "libvirt" do |libvirt|
libvirt.memory = 512
libvirt.cpus = 1
libvirt.driver = "kvm"
libvirt.connect_via_ssh = false
end
# Swarm Manager
config.vm.define "swarm-manager" do |manager|
manager.vm.hostname = "swarm-manager"
manager.vm.network "private_network", ip: "192.168.56.10"
# Port forwarding for Docker Swarm services
manager.vm.network "forwarded_port", guest: 9000, host: 19000, id: "portainer"
manager.vm.network "forwarded_port", guest: 8080, host: 18080, id: "traefik"
manager.vm.network "forwarded_port", guest: 80, host: 18081, id: "webapp"
manager.vm.provider "virtualbox" do |vb|
vb.name = "swarm-manager"
vb.memory = "512"
vb.cpus = 1
end
manager.vm.provider "libvirt" do |libvirt|
libvirt.memory = 512
libvirt.cpus = 1
end
end
# Swarm Worker 1
config.vm.define "swarm-worker1" do |worker1|
worker1.vm.hostname = "swarm-worker1"
worker1.vm.network "private_network", ip: "192.168.56.11"
worker1.vm.provider "virtualbox" do |vb|
vb.name = "swarm-worker1"
vb.memory = "512"
vb.cpus = 1
end
worker1.vm.provider "libvirt" do |libvirt|
libvirt.memory = 512
libvirt.cpus = 1
end
end
# Swarm Worker 2
config.vm.define "swarm-worker2" do |worker2|
worker2.vm.hostname = "swarm-worker2"
worker2.vm.network "private_network", ip: "192.168.56.12"
worker2.vm.provider "virtualbox" do |vb|
vb.name = "swarm-worker2"
vb.memory = "512"
vb.cpus = 1
end
worker2.vm.provider "libvirt" do |libvirt|
libvirt.memory = 512
libvirt.cpus = 1
end
end
# Swarm Worker 3
config.vm.define "swarm-worker3" do |worker3|
worker3.vm.hostname = "swarm-worker3"
worker3.vm.network "private_network", ip: "192.168.56.13"
worker3.vm.provider "virtualbox" do |vb|
vb.name = "swarm-worker3"
vb.memory = "512"
vb.cpus = 1
end
worker3.vm.provider "libvirt" do |libvirt|
libvirt.memory = 512
libvirt.cpus = 1
end
end
end

View File

@@ -1,6 +0,0 @@
---
# Ansible Galaxy Requirements
# Install with: ansible-galaxy install -r ansible-requirements.yml
- name: geerlingguy.swap
version: 1.1.1

2
ansible/ansible.cfg Normal file
View File

@@ -0,0 +1,2 @@
[defaults]
host_key_checking=False

34
ansible/master.yml Normal file
View File

@@ -0,0 +1,34 @@
---
- hosts: all
become: true
tasks:
# write hello world to a file in the home directory
- name: write hello
copy:
content: "hello ansible from ansible-manager!"
dest: /home/vagrant/hello2.txt
mode: 0644
become: true
- name: Echo hello
shell: echo 'hello ansible!'
args:
chdir: $HOME
# Echo the contents of the hello.txt file
- name: Echo hello
shell: cat /home/vagrant/hello.txt
args:
chdir: $HOME
# Echo the contents of the hello2.txt file
- name: Echo hello2
shell: cat /home/vagrant/hello2.txt
args:
chdir: $HOME
# - name: Initialize the cluster
# shell: docker swarm init --advertise-addr 192.168.56.10 >> cluster_initialized.txt
# args:
# chdir: $HOME
# creates: cluster_initialized.txt

View File

@@ -1,159 +0,0 @@
---
# Docker Swarm Deployment Playbook
# This playbook initializes Docker Swarm cluster and deploys services
- name: Initialize Docker Swarm Manager
hosts: swarm_managers
become: yes
gather_facts: yes
tasks:
- name: Check if Docker Swarm is already initialized
command: docker info --format "{{ '{{' }}.Swarm.LocalNodeState{{ '}}' }}"
register: swarm_status_check
changed_when: false
failed_when: false
- name: Initialize Docker Swarm
command: docker swarm init --advertise-addr 192.168.56.10
register: swarm_init_result
changed_when: swarm_init_result.rc == 0
failed_when: swarm_init_result.rc not in [0, 1]
when: swarm_status_check.stdout != "active"
- name: Get worker join token
command: docker swarm join-token worker
register: worker_token_result
changed_when: false
- name: Extract worker join command
set_fact:
worker_join_token: "{{ worker_token_result.stdout_lines[2] }}"
- name: Display worker join command
debug:
msg: "Worker join command: {{ worker_join_token }}"
- name: Get manager join token
command: docker swarm join-token manager
register: manager_token_result
changed_when: false
- name: Display manager join command
debug:
msg: "Manager join command: {{ manager_token_result.stdout_lines[2] }}"
- name: Copy Docker Compose stack file
copy:
src: docker-stack.yml
dest: /home/vagrant/docker-stack.yml
mode: '0644'
- name: Deploy Docker Swarm stack
command: docker stack deploy -c docker-stack.yml swarm-stack
register: stack_result
changed_when: stack_result.rc == 0
failed_when: stack_result.rc not in [0, 1]
- name: Check Docker Swarm status
command: docker node ls
register: swarm_status
changed_when: false
- name: Display Swarm status
debug:
msg: "{{ swarm_status.stdout_lines }}"
- name: Check Docker stack services
command: docker stack services swarm-stack
register: services_status
changed_when: false
- name: Display stack services status
debug:
msg: "{{ services_status.stdout_lines }}"
- name: Join Docker Swarm Workers
hosts: swarm_workers
become: yes
gather_facts: no
tasks:
- name: Join Docker Swarm as worker
command: "{{ hostvars[groups['swarm_managers'][0]]['worker_join_token'] | replace('10.0.2.15:2377', '192.168.56.10:2377') }}"
register: join_result
changed_when: join_result.rc == 0
failed_when: join_result.rc not in [0, 1]
- name: Verify node joined successfully
command: docker node ls
register: node_status
changed_when: false
ignore_errors: yes
- name: Display node status
debug:
msg: "{{ node_status.stdout_lines if node_status.rc == 0 else 'Node not accessible' }}"
- name: Verify Docker Swarm Cluster
hosts: swarm_managers
become: yes
gather_facts: no
tasks:
- name: Wait for all nodes to be ready
command: docker node ls
register: nodes_check
until: nodes_check.stdout_lines | length >= 5 # Header + 4 nodes
retries: 10
delay: 5
changed_when: false
- name: Check all nodes are active
command: docker node ls --format "{{ '{{' }}.Status{{ '}}' }}"
register: node_statuses
changed_when: false
- name: Verify all nodes are ready
assert:
that:
- "'Ready' in node_statuses.stdout"
- "'Active' in node_statuses.stdout"
fail_msg: "Not all nodes are ready and active"
- name: Check stack service health
command: docker stack services swarm-stack --format "table {{.Name}}\t{{.Replicas}}"
register: service_replicas
changed_when: false
- name: Display stack service replicas
debug:
msg: "{{ service_replicas.stdout_lines }}"
- name: Create cluster info file
copy:
content: |
Docker Swarm Cluster Information
================================
Manager: {{ groups['swarm_managers'][0] }}
Workers: {{ groups['swarm_workers'] | join(', ') }}
Total Nodes: {{ groups['swarm_nodes'] | length }}
Services Deployed:
- Portainer (Management UI): http://{{ ansible_default_ipv4.address }}:9000
- Traefik Dashboard: http://{{ ansible_default_ipv4.address }}:8080
- Web Application: http://{{ ansible_default_ipv4.address }}
Network: swarm-network (overlay)
Created: {{ ansible_date_time.iso8601 }}
dest: /opt/swarm-cluster-info.txt
mode: '0644'
- name: Display cluster information
command: cat /opt/swarm-cluster-info.txt
register: cluster_info
changed_when: false
- name: Show cluster information
debug:
msg: "{{ cluster_info.stdout_lines }}"

View File

@@ -1,74 +0,0 @@
version: '3.8'
services:
portainer:
image: portainer/portainer-ce:latest
ports:
- "9000:9000"
- "9443:9443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
deploy:
replicas: 1
placement:
constraints:
- node.role == manager
restart_policy:
condition: on-failure
networks:
- swarm-network
traefik:
image: traefik:v2.10
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command:
- --api.dashboard=true
- --api.insecure=true
- --providers.docker=true
- --providers.docker.exposedbydefault=false
- --entrypoints.web.address=:80
deploy:
replicas: 1
placement:
constraints:
- node.role == manager
restart_policy:
condition: on-failure
networks:
- swarm-network
web-app:
image: nginx:alpine
deploy:
replicas: 3
restart_policy:
condition: on-failure
labels:
- traefik.enable=true
- traefik.http.routers.webapp.rule=Host(`192.168.56.10`)
- traefik.http.services.webapp.loadbalancer.server.port=80
networks:
- swarm-network
hello-world:
image: hello-world:latest
deploy:
replicas: 2
restart_policy:
condition: on-failure
networks:
- swarm-network
volumes:
portainer_data:
driver: local
networks:
swarm-network:
driver: overlay
attachable: true

View File

@@ -1,21 +0,0 @@
# Ansible Inventory for Docker Swarm Cluster
# This file defines the hosts and groups for Ansible playbooks
[all:vars]
ansible_user=vagrant
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
[swarm_managers]
swarm-manager ansible_host=127.0.0.1 ansible_port=2204 ansible_ssh_private_key_file=.vagrant/machines/swarm-manager/virtualbox/private_key
[swarm_workers]
swarm-worker1 ansible_host=127.0.0.1 ansible_port=2205 ansible_ssh_private_key_file=.vagrant/machines/swarm-worker1/virtualbox/private_key
swarm-worker2 ansible_host=127.0.0.1 ansible_port=2206 ansible_ssh_private_key_file=.vagrant/machines/swarm-worker2/virtualbox/private_key
swarm-worker3 ansible_host=127.0.0.1 ansible_port=2207 ansible_ssh_private_key_file=.vagrant/machines/swarm-worker3/virtualbox/private_key
[swarm_nodes:children]
swarm_managers
swarm_workers
[swarm_nodes:vars]
ansible_python_interpreter=/usr/bin/python3

163
manage.sh
View File

@@ -1,163 +0,0 @@
#!/bin/bash
# Bare Bones Vagrant Management Script
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}=== $1 ===${NC}"
}
# Function to start all machines
start_all() {
print_header "Starting All Machines"
vagrant up
print_status "All machines started successfully!"
}
# Function to stop all machines
stop_all() {
print_header "Stopping All Machines"
vagrant halt
print_status "All machines stopped successfully!"
}
# Function to destroy all machines
destroy_all() {
print_header "Destroying All Machines"
print_warning "This will permanently delete all machines!"
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
vagrant destroy -f
print_status "All machines destroyed successfully!"
else
print_status "Operation cancelled."
fi
}
# Function to show status
show_status() {
print_header "Machine Status"
vagrant status
}
# Function to access a specific machine
access_machine() {
local machine=${1:-host}
print_header "Accessing $machine"
vagrant ssh "$machine"
}
# Function to run Ansible commands
run_ansible() {
local command=${1:-ping}
print_header "Running Ansible $command"
case "$command" in
ping)
ansible all -i inventory -m ping
;;
setup)
ansible-playbook -i inventory setup-playbook.yml
;;
deploy)
ansible-playbook -i inventory deploy-playbook.yml
;;
list)
ansible all -i inventory --list-hosts
;;
facts)
ansible all -i inventory -m setup
;;
*)
print_error "Unknown Ansible command: $command"
print_status "Available commands: ping, setup, deploy, list, facts"
;;
esac
}
# Function to show help
show_help() {
echo "Bare Bones Vagrant Management Script"
echo ""
echo "Usage: $0 [COMMAND] [MACHINE]"
echo ""
echo "Commands:"
echo " start Start all machines"
echo " stop Stop all machines"
echo " destroy Destroy all machines (permanent)"
echo " status Show machine status"
echo " ssh MACHINE Access machine via SSH"
echo " ansible COMMAND Run Ansible command (ping, play, docker, list, facts)"
echo " help Show this help message"
echo ""
echo "Machines:"
echo " swarm-manager Swarm Manager (192.168.56.10)"
echo " swarm-worker1 Swarm Worker 1 (192.168.56.11)"
echo " swarm-worker2 Swarm Worker 2 (192.168.56.12)"
echo " swarm-worker3 Swarm Worker 3 (192.168.56.13)"
echo ""
echo "Ansible Commands:"
echo " ping Test connectivity to all hosts"
echo " setup Install dependencies (Python, Docker, swap)"
echo " deploy Deploy Docker Swarm cluster"
echo " list List all hosts"
echo " facts Gather system facts"
echo ""
echo "Examples:"
echo " $0 start # Start all machines"
echo " $0 ssh swarm-manager # Access swarm manager"
echo " $0 ansible ping # Test Ansible connectivity"
echo " $0 ansible setup # Install dependencies"
echo " $0 ansible deploy # Deploy Docker Swarm cluster"
}
# Main script logic
case "${1:-help}" in
start)
start_all
;;
stop)
stop_all
;;
destroy)
destroy_all
;;
status)
show_status
;;
ssh)
access_machine "$2"
;;
ansible)
run_ansible "$2"
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac

View File

@@ -1,541 +0,0 @@
#!/bin/bash
# Reset and Test Script for Debian Vagrant Cluster
# This script fully destroys and recreates the entire stack, then runs all tests
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_header() {
echo -e "${BLUE}=== $1 ===${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_info() {
echo -e "${BLUE} $1${NC}"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to wait for user confirmation (disabled for automation)
confirm() {
print_info "Auto-confirming operation (automation mode)"
return 0
}
# Function to check prerequisites
check_prerequisites() {
print_header "Checking Prerequisites"
local missing_deps=()
if ! command_exists vagrant; then
missing_deps+=("vagrant")
fi
if ! command_exists ansible; then
missing_deps+=("ansible")
fi
if ! command_exists make; then
missing_deps+=("make")
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
print_info "Please install the missing dependencies and try again"
exit 1
fi
print_success "All prerequisites are installed"
}
# Function to destroy everything
destroy_all() {
print_header "Destroying All Machines"
print_info "Auto-confirming destruction (automation mode)"
print_info "Stopping all machines..."
vagrant halt 2>/dev/null || true
print_info "Destroying all machines..."
vagrant destroy -f
print_info "Cleaning up Vagrant files..."
rm -rf .vagrant/
print_success "All machines destroyed and cleaned up"
}
# Function to start all machines
start_all() {
print_header "Starting All Machines"
print_info "Starting Vagrant cluster..."
vagrant up
print_info "Waiting for machines to be ready..."
sleep 10
print_info "Checking machine status..."
vagrant status
print_success "All machines started successfully"
}
# Function to test SSH connectivity
test_ssh() {
print_header "Testing SSH Connectivity"
local machines=("swarm-manager" "swarm-worker1" "swarm-worker2" "swarm-worker3")
local failed_machines=()
for machine in "${machines[@]}"; do
print_info "Testing SSH to $machine..."
if vagrant ssh "$machine" -c "echo 'SSH test successful'" >/dev/null 2>&1; then
print_success "SSH to $machine: OK"
else
print_error "SSH to $machine: FAILED"
failed_machines+=("$machine")
fi
done
if [ ${#failed_machines[@]} -ne 0 ]; then
print_error "SSH failed for: ${failed_machines[*]}"
return 1
fi
print_success "All SSH connections working"
return 0
}
# Function to test Ansible connectivity
test_ansible() {
print_header "Testing Ansible Connectivity"
print_info "Running Ansible ping test..."
if ansible all -i inventory -m ping; then
print_success "Ansible connectivity test passed"
return 0
else
print_error "Ansible connectivity test failed"
return 1
fi
}
# Function to run setup playbook
run_setup() {
print_header "Running Setup Playbook"
print_info "Installing Ansible roles..."
if ansible-galaxy install -r ansible-requirements.yml --force; then
print_success "Ansible roles installed successfully"
else
print_error "Failed to install Ansible roles"
return 1
fi
print_info "Installing dependencies and creating swap..."
if ansible-playbook -i inventory setup-playbook.yml; then
print_success "Setup playbook completed successfully"
return 0
else
print_error "Setup playbook failed"
return 1
fi
}
# Function to run deployment playbook
run_deployment() {
print_header "Running Deployment Playbook"
print_info "Deploying applications and services..."
if ansible-playbook -i inventory deploy-playbook.yml; then
print_success "Deployment playbook completed successfully"
return 0
else
print_error "Deployment playbook failed"
return 1
fi
}
# Function to run comprehensive tests
run_tests() {
print_header "Running Comprehensive Tests"
local test_results=()
# Test 1: SSH Connectivity
if test_ssh; then
test_results+=("SSH: ✅ PASS")
else
test_results+=("SSH: ❌ FAIL")
fi
# Test 2: Ansible Connectivity
if test_ansible; then
test_results+=("Ansible: ✅ PASS")
else
test_results+=("Ansible: ❌ FAIL")
fi
# Test 3: Setup Playbook
if run_setup; then
test_results+=("Setup: ✅ PASS")
else
test_results+=("Setup: ❌ FAIL")
fi
# Test 4: Deployment Playbook
if run_deployment; then
test_results+=("Deployment: ✅ PASS")
else
test_results+=("Deployment: ❌ FAIL")
fi
# Test 5: Verify swap is active
print_info "Verifying swap is active..."
if ansible all -i inventory -m shell -a "cat /proc/swaps" | grep -q "swapfile"; then
test_results+=("Swap: ✅ PASS")
else
test_results+=("Swap: ❌ FAIL")
fi
# Test 6: Verify Docker is running
print_info "Verifying Docker is running..."
if ansible all -i inventory -m shell -a "docker --version" >/dev/null 2>&1; then
test_results+=("Docker: ✅ PASS")
else
test_results+=("Docker: ❌ FAIL")
fi
# Test 7: Verify Docker Swarm is initialized
print_info "Verifying Docker Swarm cluster..."
if ansible swarm_managers -i inventory -m shell -a "docker node ls" >/dev/null 2>&1; then
test_results+=("Swarm: ✅ PASS")
else
test_results+=("Swarm: ❌ FAIL")
fi
# Display test results
print_header "Test Results Summary"
for result in "${test_results[@]}"; do
echo " $result"
done
# Count failures
local failures=$(printf '%s\n' "${test_results[@]}" | grep -c "❌ FAIL" || true)
if [ "$failures" -eq 0 ]; then
print_success "All tests passed! 🎉"
return 0
else
print_error "$failures test(s) failed"
return 1
fi
}
# Function to show help
show_help() {
echo "Reset and Test Script for Debian Vagrant Cluster"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " full-reset Destroy everything and run full test cycle"
echo " destroy-only Only destroy all machines"
echo " start-only Only start all machines"
echo " test-only Only run tests (assumes machines are running)"
echo " ssh-test Only test SSH connectivity"
echo " ansible-test Only test Ansible connectivity"
echo " setup-only Only run setup playbook"
echo " deploy-only Only run deployment playbook"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 full-reset # Complete destroy/recreate/test cycle"
echo " $0 test-only # Run tests on existing machines"
echo " $0 ssh-test # Quick SSH connectivity check"
echo ""
echo "This script will:"
echo " 1. Check prerequisites (vagrant, ansible, make)"
echo " 2. Destroy all VMs and clean up"
echo " 3. Start all VMs fresh"
echo " 4. Test SSH connectivity"
echo " 5. Test Ansible connectivity"
echo " 6. Run setup playbook (dependencies, swap)"
echo " 7. Run deployment playbook (Docker, services)"
echo " 8. Verify everything is working"
}
# Main script logic
main() {
local command=${1:-help}
case "$command" in
full-reset)
print_header "Full Reset and Test Cycle"
check_prerequisites
destroy_all
start_all
run_tests
;;
destroy-only)
print_header "Destroy Only"
check_prerequisites
destroy_all
;;
start-only)
print_header "Start Only"
check_prerequisites
start_all
;;
test-only)
print_header "Test Only"
check_prerequisites
run_tests
;;
ssh-test)
print_header "SSH Test Only"
check_prerequisites
test_ssh
;;
ansible-test)
print_header "Ansible Test Only"
check_prerequisites
test_ansible
;;
setup-only)
print_header "Setup Only"
check_prerequisites
run_setup
;;
deploy-only)
print_header "Deploy Only"
check_prerequisites
run_deployment
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"
else
print_error "Setup playbook failed"
return 1
fi
}
# Function to run deployment playbook
run_deployment() {
print_header "Running Deployment Playbook"
print_info "Deploying applications and services..."
if ansible-playbook -i inventory deploy-playbook.yml; then
print_success "Deployment playbook completed successfully"
return 0
else
print_error "Deployment playbook failed"
return 1
fi
}
# Function to run comprehensive tests
run_tests() {
print_header "Running Comprehensive Tests"
local test_results=()
# Test 1: SSH Connectivity
if test_ssh; then
test_results+=("SSH: ✅ PASS")
else
test_results+=("SSH: ❌ FAIL")
fi
# Test 2: Ansible Connectivity
if test_ansible; then
test_results+=("Ansible: ✅ PASS")
else
test_results+=("Ansible: ❌ FAIL")
fi
# Test 3: Setup Playbook
if run_setup; then
test_results+=("Setup: ✅ PASS")
else
test_results+=("Setup: ❌ FAIL")
fi
# Test 4: Deployment Playbook
if run_deployment; then
test_results+=("Deployment: ✅ PASS")
else
test_results+=("Deployment: ❌ FAIL")
fi
# Test 5: Verify swap is active
print_info "Verifying swap is active..."
if ansible all -i inventory -m shell -a "cat /proc/swaps" | grep -q "swapfile"; then
test_results+=("Swap: ✅ PASS")
else
test_results+=("Swap: ❌ FAIL")
fi
# Test 6: Verify Docker is running
print_info "Verifying Docker is running..."
if ansible all -i inventory -m shell -a "docker --version" >/dev/null 2>&1; then
test_results+=("Docker: ✅ PASS")
else
test_results+=("Docker: ❌ FAIL")
fi
# Test 7: Verify Docker Swarm is initialized
print_info "Verifying Docker Swarm cluster..."
if ansible swarm_managers -i inventory -m shell -a "docker node ls" >/dev/null 2>&1; then
test_results+=("Swarm: ✅ PASS")
else
test_results+=("Swarm: ❌ FAIL")
fi
# Display test results
print_header "Test Results Summary"
for result in "${test_results[@]}"; do
echo " $result"
done
# Count failures
local failures=$(printf '%s\n' "${test_results[@]}" | grep -c "❌ FAIL" || true)
if [ "$failures" -eq 0 ]; then
print_success "All tests passed! 🎉"
return 0
else
print_error "$failures test(s) failed"
return 1
fi
}
# Function to show help
show_help() {
echo "Reset and Test Script for Debian Vagrant Cluster"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " full-reset Destroy everything and run full test cycle"
echo " destroy-only Only destroy all machines"
echo " start-only Only start all machines"
echo " test-only Only run tests (assumes machines are running)"
echo " ssh-test Only test SSH connectivity"
echo " ansible-test Only test Ansible connectivity"
echo " setup-only Only run setup playbook"
echo " deploy-only Only run deployment playbook"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 full-reset # Complete destroy/recreate/test cycle"
echo " $0 test-only # Run tests on existing machines"
echo " $0 ssh-test # Quick SSH connectivity check"
echo ""
echo "This script will:"
echo " 1. Check prerequisites (vagrant, ansible, make)"
echo " 2. Destroy all VMs and clean up"
echo " 3. Start all VMs fresh"
echo " 4. Test SSH connectivity"
echo " 5. Test Ansible connectivity"
echo " 6. Run setup playbook (dependencies, swap)"
echo " 7. Run deployment playbook (Docker, services)"
echo " 8. Verify everything is working"
}
# Main script logic
main() {
local command=${1:-help}
case "$command" in
full-reset)
print_header "Full Reset and Test Cycle"
check_prerequisites
destroy_all
start_all
run_tests
;;
destroy-only)
print_header "Destroy Only"
check_prerequisites
destroy_all
;;
start-only)
print_header "Start Only"
check_prerequisites
start_all
;;
test-only)
print_header "Test Only"
check_prerequisites
run_tests
;;
ssh-test)
print_header "SSH Test Only"
check_prerequisites
test_ssh
;;
ansible-test)
print_header "Ansible Test Only"
check_prerequisites
test_ansible
;;
setup-only)
print_header "Setup Only"
check_prerequisites
run_setup
;;
deploy-only)
print_header "Deploy Only"
check_prerequisites
run_deployment
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"

View File

@@ -1,124 +0,0 @@
---
# Setup Playbook for Docker Swarm Cluster
# This playbook installs essential dependencies including Python, Docker, and creates swap
- name: Setup Docker Swarm nodes
hosts: swarm_nodes
become: yes
gather_facts: no
vars:
swap_file_size_mb: 1024
swap_file_state: present
swap_file_existing_size_mb: 0
roles:
- role: geerlingguy.swap
swap_file_size_mb: 1024
swap_file_state: present
tasks:
- name: Update apt package index
apt:
update_cache: yes
cache_valid_time: 3600
- name: Install essential packages
apt:
name:
- python3
- python3-pip
- ansible
- curl
- wget
- sudo
- util-linux
- apt-transport-https
- ca-certificates
- gnupg
- lsb-release
state: present
- name: Create sudoers entry for vagrant user
lineinfile:
path: /etc/sudoers.d/vagrant
line: "vagrant ALL=(ALL) NOPASSWD:ALL"
create: yes
mode: '0440'
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/debian/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/debian bookworm stable"
state: present
update_cache: yes
- name: Install Docker CE
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
- name: Add vagrant user to docker group
user:
name: vagrant
groups: docker
append: yes
- name: Start and enable Docker service
systemd:
name: docker
state: started
enabled: yes
- name: Verify Python installation
command: python3 --version
register: python_version
changed_when: false
- name: Show Python version
debug:
msg: "{{ python_version.stdout }}"
- name: Verify pip installation
command: pip3 --version
register: pip_version
changed_when: false
- name: Show pip version
debug:
msg: "{{ pip_version.stdout }}"
- name: Create test directory
file:
path: /home/vagrant/test
state: directory
owner: vagrant
group: vagrant
mode: '0755'
- name: Display system information
command: uname -a
register: system_info
changed_when: false
- name: Show system information
debug:
msg: "{{ system_info.stdout }}"
- name: Check memory and swap usage
command: free -m
register: memory_info
changed_when: false
- name: Show memory and swap usage
debug:
msg: "{{ memory_info.stdout_lines }}"

56
vagrant/Vagrantfile vendored Normal file
View File

@@ -0,0 +1,56 @@
nodes = [
{ :hostname => 'ansible-manager', :ip => '192.168.56.9', :ram => 256, :cpus => 1 },
{ :hostname => 'swarm-master-1', :ip => '192.168.56.10', :ram => 256, :cpus => 1 },
# { :hostname => 'swarm-master-2', :ip => '192.168.56.11', :ram => 256, :cpus => 1 },
# { :hostname => 'swarm-worker-1', :ip => '192.168.56.12', :ram => 256, :cpus => 1 },
# { :hostname => 'swarm-worker-2', :ip => '192.168.56.13', :ram => 1024, :cpus => 1 }
]
Vagrant.configure("2") do |config|
# config.ssh.insert_key = false
# config.ssh.forward_agent = true
config.vm.provision "ansible" do |ansible|
ansible.playbook = "setup.yml"
# ansible.inventory_path = "../ansible/inventory"
# ansible.verbose = true
# ansible.limit = "all"
# # ansible.raw_arguments = ["--timeout=60"]
end
nodes.each do |node|
puts "Provisioning node: #{node[:hostname]}"
config.vm.define node[:hostname] do |node_config|
node_config.vm.hostname = node[:hostname]
node_config.vm.box = "debian/bullseye64"
# node_config.vm.box_version = "20250415.336224"
# node_config.ssh.private_key_path = "../keys/access"
# node_config.vm.provision "shell", inline: <<-SHELL
# mkdir -p /home/vagrant/.ssh
# echo '$(cat ../keys/access.pub)' >> /home/vagrant/.ssh/authorized_keys
# chown -R vagrant:vagrant /home/vagrant/.ssh
# chmod 700 /home/vagrant/.ssh
# chmod 600 /home/vagrant/.ssh/authorized_keys
# SHELL
# config.vm.provision "shell" do |s|
# ssh_pub_key = File.readlines("../keys/access.pub").first.strip
# s.inline = <<-SHELL
# echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
# echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
# SHELL
# end
# node_config.vm.network "private_network", ip: node[:ip]
# node_config.vm.provider "virtualbox" do |vb|
# vb.name = node[:hostname]
# vb.memory = node[:ram]
# vb.cpus = node[:cpus]
# end
end
end
# config.vm.define "ansible-manager" do |ansible|
# ansible.vm.provision "file", source: "../ansible", destination: "$HOME"
# ansible.vm.provision "shell", path: "ansible.sh"
# ansible.vm.provision "shell", inline: "echo 'hello ansible!'"
# end
end

17
vagrant/setup.yml Normal file
View File

@@ -0,0 +1,17 @@
---
- hosts: all
become: true
tasks:
# write hello world to a file in the home directory
- name: write hello
copy:
content: "hello ansible!"
dest: /home/vagrant/hello.txt
mode: 0644
become: true
- name: Echo hello
shell: echo 'hello ansible!'
args:
chdir: $HOME