Skip to content

Commit

Permalink
Added ansible config
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Jun 28, 2024
1 parent 047488d commit 389c85c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion automation/new_droplet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ echo "SSH is available!"

# Run the Ansible playbook once SSH is up
echo "Running Ansible playbook..."
fetch_inventory | ansible-playbook -i /dev/stdin playbooks/initalize_droplet.yml --extra-vars "droplet_name=$1 ansible_host=$droplet_priv_ipv4 public_ipv4=$droplet_pub_ipv4 ansible_user=root"
fetch_inventory | ansible-playbook -i /dev/stdin playbooks/initalize_worker.yml --extra-vars "droplet_name=$1 ansible_host=$droplet_priv_ipv4 public_ipv4=$droplet_pub_ipv4 ansible_user=root"


notify "Droplet $1 is provisioned and configured!"
Expand Down
86 changes: 86 additions & 0 deletions automation/playbooks/initialize_worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
- hosts: workers
become: yes
become_user: root
become_method: sudo

vars:
# permissions on windows with wsl is not fun
ansible_ssh_private_key_file: ~/.ssh/id_rsa
ansible_python_interpreter: /usr/bin/python3

docker_edition: 'ce'
docker_package_state: present

tasks:
- name: 'APT Upgrade'
apt:
upgrade: true
update_cache: true

- name: 'Install nfs'
apt:
pkg:
- nfs-common

- name: 'Change timezone to Toronto time'
shell: timedatectl set-timezone America/Toronto

- name: 'Installing docker'
include_role:
name: geerlingguy.docker

- name: Join Docker Swarm
hosts: all
become: yes
tasks:
- name: Get Swarm join token from manager
shell: docker swarm join-token worker
register: join_token
delegate_to: "{{ groups['managers'] | random }}" # Run on the first manager node

- name: Join the Swarm as a worker
community.docker.docker_swarm:
state: join
join_token: "{{ join_token.stdout_lines[0] }}"
advertise_addr: "{{ groups['managers'][0] }}" # Use Docker factP



- name: 'Mount problem data nfs share'
file:
path: /var/share
state: directory

mount:
path: /var/share
src: 10.137.0.5:/var/share
state: mounted
fstype: nfs4
opts: auto,nofail,noatime,nolock,intr,tcp,actimeo=1800,timeo=600

- name: 'Installing unattended-upgrades'
include_role:
name: jnv.unattended-upgrades

- name: 'Install UFW'
apt:
name: ufw

- name: 'UFW: internal network'
ufw:
rule: allow
src: '10.137.0.0/16'
dest: '10.137.0.0/16'


- name: 'UFW: enable and by default drop'
ufw:
state: enabled
policy: deny

- name: 'Disable password for root'
shell: passwd -d root

- name: 'Reboot server'
reboot:
reboot_timeout: 0
12 changes: 9 additions & 3 deletions automation/swarm_info
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/bin/bash

# Function to get all ips of all nodes in the swarm
get_node_ips() {
get_worker_ips() {
docker node ls --filter role=worker --format "{{.Hostname}}" | \
xargs docker node inspect --format '{{ .Status.Addr }}'
}
get_manager_ips() {
docker node ls --filter role=manager --format "{{.Hostname}}" | \
xargs docker node inspect --format '{{ .Status.Addr }}'
}

# Function to get IP addresses of running replicas for a given service
get_replica_ips() {
Expand Down Expand Up @@ -50,6 +54,8 @@ get_replica_ips() {
#}

fetch_inventory() {
echo "[nodes]"
get_node_ips
echo "[managers]" # todo: if we have multiple managers, we need to place general first
get_manager_ips
echo "[workers]"
get_worker_ips
}

0 comments on commit 389c85c

Please sign in to comment.