Add roles and variables
This commit is contained in:
17
Makefile
17
Makefile
@@ -1,13 +1,22 @@
|
|||||||
list:
|
list:
|
||||||
vagrant status
|
vagrant status
|
||||||
make up:
|
up:
|
||||||
cd vagrant && vagrant up
|
cd vagrant && vagrant up
|
||||||
make destroy:
|
destroy:
|
||||||
cd vagrant && vagrant destroy -f
|
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
|
cd ansible && ansible-playbook -i ../vagrant/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory master.yml
|
||||||
|
|
||||||
make setup-keys:
|
setup-keys:
|
||||||
mkdir -p ./keys
|
mkdir -p ./keys
|
||||||
ssh-keygen -t ed25519 -f ./keys/access -N "" -q
|
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
|
||||||
|
|||||||
11
ansible/ansible-requirements.yml
Normal file
11
ansible/ansible-requirements.yml
Normal 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
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
[defaults]
|
[defaults]
|
||||||
host_key_checking=False
|
host_key_checking=False
|
||||||
|
# stdout_callback = minimal
|
||||||
|
color = true
|
||||||
|
|||||||
14
ansible/group_vars/all.yml
Normal file
14
ansible/group_vars/all.yml
Normal 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
|
||||||
|
|
||||||
@@ -1,31 +1,49 @@
|
|||||||
---
|
---
|
||||||
- hosts: all
|
- hosts: swarm_master
|
||||||
become: true
|
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:
|
tasks:
|
||||||
# write hello world to a file in the home directory
|
- name: Check memory and swap usage
|
||||||
- name: write hello
|
command: free -m
|
||||||
copy:
|
register: memory_info
|
||||||
content: "hello ansible from ansible-manager!"
|
changed_when: false
|
||||||
dest: /home/vagrant/hello2.txt
|
|
||||||
mode: 0644
|
|
||||||
become: true
|
|
||||||
|
|
||||||
- name: Echo hello
|
- name: Ensure Python pip is installed
|
||||||
shell: echo 'hello ansible!'
|
package:
|
||||||
args:
|
name: python3-pip
|
||||||
chdir: $HOME
|
state: present
|
||||||
# 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 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
|
# - name: Initialize the cluster
|
||||||
# shell: docker swarm init --advertise-addr 192.168.56.10 >> cluster_initialized.txt
|
# shell: docker swarm init --advertise-addr 192.168.56.10 >> cluster_initialized.txt
|
||||||
|
|||||||
7
ansible/roles/common/tasks/main.yml
Normal file
7
ansible/roles/common/tasks/main.yml
Normal 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
16
ansible/swarm-node.yml
Normal 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
99
vagrant/Vagrantfile
vendored
@@ -1,56 +1,51 @@
|
|||||||
|
num_workers = 1
|
||||||
|
|
||||||
nodes = [
|
nodes = [
|
||||||
{ :hostname => 'ansible-manager', :ip => '192.168.56.9', :ram => 256, :cpus => 1 },
|
{ hostname: 'swarm-master', ip: '192.168.56.10', ram: 256, cpus: 1, groups: ['swarm_master'] }
|
||||||
{ :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|
|
(1..num_workers).each do |i|
|
||||||
# config.ssh.insert_key = false
|
nodes << {
|
||||||
# config.ssh.forward_agent = true
|
hostname: "swarm-worker-#{i}",
|
||||||
config.vm.provision "ansible" do |ansible|
|
ip: "192.168.56.#{10 + i}",
|
||||||
ansible.playbook = "setup.yml"
|
ram: 256,
|
||||||
# ansible.inventory_path = "../ansible/inventory"
|
cpus: 1,
|
||||||
# ansible.verbose = true
|
groups: ['swarm_workers']
|
||||||
# ansible.limit = "all"
|
}
|
||||||
# # ansible.raw_arguments = ["--timeout=60"]
|
end
|
||||||
end
|
|
||||||
nodes.each do |node|
|
Vagrant.configure('2') do |config|
|
||||||
puts "Provisioning node: #{node[:hostname]}"
|
groups = {}
|
||||||
config.vm.define node[:hostname] do |node_config|
|
nodes.each do |node|
|
||||||
node_config.vm.hostname = node[:hostname]
|
node[:groups].each do |group|
|
||||||
node_config.vm.box = "debian/bullseye64"
|
groups[group] ||= []
|
||||||
# node_config.vm.box_version = "20250415.336224"
|
groups[group] << node[:hostname]
|
||||||
|
end
|
||||||
# node_config.ssh.private_key_path = "../keys/access"
|
end
|
||||||
|
|
||||||
# node_config.vm.provision "shell", inline: <<-SHELL
|
config.vm.provision 'ansible' do |ansible|
|
||||||
# mkdir -p /home/vagrant/.ssh
|
ansible.playbook = 'setup.yml'
|
||||||
# echo '$(cat ../keys/access.pub)' >> /home/vagrant/.ssh/authorized_keys
|
ansible.groups = groups
|
||||||
# chown -R vagrant:vagrant /home/vagrant/.ssh
|
|
||||||
# chmod 700 /home/vagrant/.ssh
|
# ansible.verbose = true
|
||||||
# chmod 600 /home/vagrant/.ssh/authorized_keys
|
# ansible.limit = "all"
|
||||||
# SHELL
|
# ansible.raw_arguments = ["--timeout=60"]
|
||||||
# config.vm.provision "shell" do |s|
|
end
|
||||||
# ssh_pub_key = File.readlines("../keys/access.pub").first.strip
|
|
||||||
# s.inline = <<-SHELL
|
nodes.each do |node|
|
||||||
# echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
|
puts "Provisioning node: #{node[:hostname]}"
|
||||||
# echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
|
config.vm.define node[:hostname] do |node_config|
|
||||||
# SHELL
|
node_config.vm.hostname = node[:hostname]
|
||||||
# end
|
node_config.vm.box = 'debian/bullseye64'
|
||||||
# node_config.vm.network "private_network", ip: node[:ip]
|
# node_config.vm.box = 'generic/archlinux64'
|
||||||
# node_config.vm.provider "virtualbox" do |vb|
|
# node_config.vm.box_version = "20250415.336224"
|
||||||
# vb.name = node[:hostname]
|
|
||||||
# vb.memory = node[:ram]
|
# node_config.vm.network "private_network", ip: node[:ip]
|
||||||
# vb.cpus = node[:cpus]
|
node_config.vm.provider "virtualbox" do |vb|
|
||||||
# end
|
vb.name = node[:hostname]
|
||||||
end
|
vb.memory = node[:ram]
|
||||||
end
|
vb.cpus = node[:cpus]
|
||||||
|
end
|
||||||
# config.vm.define "ansible-manager" do |ansible|
|
end
|
||||||
# ansible.vm.provision "file", source: "../ansible", destination: "$HOME"
|
end
|
||||||
# ansible.vm.provision "shell", path: "ansible.sh"
|
|
||||||
# ansible.vm.provision "shell", inline: "echo 'hello ansible!'"
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user