Add roles and variables

This commit is contained in:
lif
2025-10-12 10:13:05 +01:00
parent 78ed25a25a
commit 6368588bce
8 changed files with 150 additions and 78 deletions

View File

@@ -1,13 +1,22 @@
list:
vagrant status
make up:
up:
cd vagrant && vagrant up
make destroy:
destroy:
cd vagrant && vagrant destroy -f
make ansible-master:
ansible-setup:
ansible-galaxy install -r ansible/ansible-requirements.yml
ansible-master:
cd ansible && ansible-playbook -i ../vagrant/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory master.yml
make setup-keys:
setup-keys:
mkdir -p ./keys
ssh-keygen -t ed25519 -f ./keys/access -N "" -q
ansible-watch:
find ./ansible -type f | entr -p make ansible-master
full-reset:
make destroy
make up && make ansible-master

View File

@@ -0,0 +1,11 @@
---
# Ansible Galaxy Requirements
# Install with: ansible-galaxy install -r ansible-requirements.yml
# https://github.com/geerlingguy/ansible-role-swap
- name: geerlingguy.swap
version: 1.1.1
# https://github.com/geerlingguy/ansible-role-docker
- name: geerlingguy.docker
version: 7.6.0

View File

@@ -1,2 +1,4 @@
[defaults]
host_key_checking=False
# stdout_callback = minimal
color = true

View File

@@ -0,0 +1,14 @@
---
swap_file_size_mb: 256
swap_file_state: present
swap_file_existing_size_mb: 0
docker_edition: 'ce'
docker_packages:
- "docker-{{ docker_edition }}"
- "docker-{{ docker_edition }}-cli"
- "docker-{{ docker_edition }}-rootless-extras"
docker_packages_state: present
docker_users:
- vagrant

View File

@@ -1,31 +1,49 @@
---
- hosts: all
- hosts: swarm_master
become: true
vars_files:
- group_vars/all.yml
vars:
swap_file_state: present
swap_file_existing_size_mb: 0
roles:
- role: geerlingguy.swap
- role: geerlingguy.docker
- role: common
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: Check memory and swap usage
command: free -m
register: memory_info
changed_when: false
- 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: Ensure Python pip is installed
package:
name: python3-pip
state: present
- name: Ensure Docker SDK for Python is installed
pip:
name: docker>=5.0.0
executable: pip3
- name: Init a new swarm with default parameters
community.docker.docker_swarm:
state: present
- name: Debug all variables
debug:
msg: "swap_file_size_mb: {{ swap_file_size_mb }}, swap_file_state: {{ swap_file_state }}"
- name: Debug Docker variables
debug:
msg: "docker_edition: {{ docker_edition }}, docker_packages: {{ docker_packages }}"
# - name: Debug all host variables
# debug:
# var: hostvars[inventory_hostname]
# - name: Initialize the cluster
# shell: docker swarm init --advertise-addr 192.168.56.10 >> cluster_initialized.txt

View File

@@ -0,0 +1,7 @@
- name: Check memory and swap usage
command: free -m
register: memory_info
changed_when: false
- name: Run the equivalent of "apt-get update" as a separate step
ansible.builtin.apt:
update_cache: yes

16
ansible/swarm-node.yml Normal file
View File

@@ -0,0 +1,16 @@
---
- hosts: swarm_workers
become: true
vars:
roles:
- role: geerlingguy.swap
swap_file_size_mb: 1024
- role: geerlingguy.docker
- role: common
tasks:
- name: Check memory and swap usage
command: free -m
register: memory_info
changed_when: false

99
vagrant/Vagrantfile vendored
View File

@@ -1,56 +1,51 @@
num_workers = 1
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 }
{ hostname: 'swarm-master', ip: '192.168.56.10', ram: 256, cpus: 1, groups: ['swarm_master'] }
]
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
(1..num_workers).each do |i|
nodes << {
hostname: "swarm-worker-#{i}",
ip: "192.168.56.#{10 + i}",
ram: 256,
cpus: 1,
groups: ['swarm_workers']
}
end
Vagrant.configure('2') do |config|
groups = {}
nodes.each do |node|
node[:groups].each do |group|
groups[group] ||= []
groups[group] << node[:hostname]
end
end
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'setup.yml'
ansible.groups = groups
# 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 = 'generic/archlinux64'
# node_config.vm.box_version = "20250415.336224"
# 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
end