|
|
# Some useful Ansible Playbooks
|
|
|
|
|
|
## Create Digital Ocean Droplet
|
|
|
|
|
|
You need to have a **digital_ocean_token** and **ssh_key_id** ready. Both are saved in an **ini** file for the playbook below.
|
|
|
|
|
|
*create_droplet.yml*
|
|
|
```yml
|
|
|
---
|
|
|
|
|
|
- name: Creating Digital Ocean Droplet
|
|
|
hosts: localhost
|
|
|
remote_user: root
|
|
|
gather_facts: true
|
|
|
|
|
|
vars:
|
|
|
do_api_token: "{{ lookup('ini','do_api_token', section='DO', file='do_settings.ini') }}"
|
|
|
ssh_key: "{{ lookup('ini', 'ssh_key_id', section='DO', file='do_settings.ini') }}"
|
|
|
|
|
|
tasks:
|
|
|
- name: Spin up droplet
|
|
|
community.digitalocean.digital_ocean_droplet:
|
|
|
state: present # represents desired state (will be create if not present "absent" would delete droplet)
|
|
|
oauth_token: "{{do_api_token}}"
|
|
|
name: "datapool"
|
|
|
unique_name: true
|
|
|
project: Testing
|
|
|
size: s-1vcpu-1gb # droplet size: https://slugs.do-api.dev/
|
|
|
region: fra1 # droplet region: https://slugs.do-api.dev/
|
|
|
image: ubuntu-22-04-x64 # droplet images: https://slugs.do-api.dev/
|
|
|
wait_timeout: 500
|
|
|
ssh_keys: ["{{ ssh_key }}"] # copy this ssh_key to server
|
|
|
register: created
|
|
|
|
|
|
- name: Show Droplet info
|
|
|
ansible.builtin.debug:
|
|
|
msg: "{{ created.data.droplet.networks.v4[0].ip_address }}"
|
|
|
```
|
|
|
|
|
|
Getting a the digital_ocean ssh_key_id
|
|
|
```sh
|
|
|
curl -X GET "https://api.digitalocean.com/v2/account/keys" -H "Authorization: Bearer $(awk -F "=" '/do_api_token/ {print $2}' do_settings.ini)"
|
|
|
```
|
|
|
|
|
|
|
|
|
## Install DataPool
|
|
|
|
|
|
*install_datapool.yml*
|
|
|
```yml
|
|
|
---
|
|
|
- name: Installing Datapool
|
|
|
hosts: <DROPLET-IP-HERE>
|
|
|
remote_user: root
|
|
|
gather_facts: False
|
|
|
|
|
|
vars:
|
|
|
path_datapool: '/opt/datapool'
|
|
|
path_data: '/data'
|
|
|
|
|
|
tasks:
|
|
|
- name: Update and upgrade packages
|
|
|
ansible.builtin.apt:
|
|
|
upgrade: True
|
|
|
force_apt_get: True
|
|
|
update_cache: True
|
|
|
|
|
|
- name: Install dependencies
|
|
|
ansible.builtin.apt:
|
|
|
pkg:
|
|
|
- htop
|
|
|
- tmux
|
|
|
- git
|
|
|
- docker.io
|
|
|
- docker-compose
|
|
|
|
|
|
- name: Clone repository
|
|
|
ansible.builtin.git:
|
|
|
repo: https://sissource.ethz.ch/sispub/datapool2/datapool.git
|
|
|
dest: '{{ path_datapool }}'
|
|
|
clone: True
|
|
|
update: True
|
|
|
|
|
|
- name: Preparing .env
|
|
|
ansible.builtin.copy:
|
|
|
src: '{{ path_datapool }}/docker/.env.example'
|
|
|
dest: '{{ path_datapool }}/docker/.env'
|
|
|
|
|
|
- name: Create docker directories
|
|
|
ansible.builtin.file:
|
|
|
path: '{{ path_data }}/{{ item }}'
|
|
|
state: directory
|
|
|
group: docker
|
|
|
mode: g+ws
|
|
|
loop:
|
|
|
- landing_zone
|
|
|
- backup_landing_zone
|
|
|
- development
|
|
|
- extra
|
|
|
|
|
|
- name: Create docker directories
|
|
|
ansible.builtin.file:
|
|
|
path: '{{ path_data }}/{{ item }}'
|
|
|
state: directory
|
|
|
loop:
|
|
|
- postgres
|
|
|
- logs
|
|
|
|
|
|
- name: Create swapfile
|
|
|
include_tasks: swap.yml
|
|
|
|
|
|
- name: Build datapool from containers
|
|
|
docker_compose:
|
|
|
project_src: '{{ path_datapool }}/docker'
|
|
|
build: True
|
|
|
|
|
|
- name: Creating datapool service
|
|
|
ansible.builtin.copy:
|
|
|
src: '{{ path_datapool }}/docker/datapool.service'
|
|
|
dest: '/etc/systemd/system/'
|
|
|
|
|
|
- name: Enable and start datapool service
|
|
|
ansible.builtin.systemd:
|
|
|
name: datapool
|
|
|
enabled: True
|
|
|
state: started
|
|
|
daemon_reload: True
|
|
|
|
|
|
- name: Install pool CLI
|
|
|
ansible.builtin.file:
|
|
|
src: '{{ path_datapool }}/docker/pool'
|
|
|
dest: '/usr/local/bin/pool'
|
|
|
state: link
|
|
|
group: docker
|
|
|
mode: o-x
|
|
|
|
|
|
- name: Reboot
|
|
|
reboot:
|
|
|
reboot_timeout: 120
|
|
|
```
|
|
|
|
|
|
*swap.yml*
|
|
|
```yml
|
|
|
- name: set swap_file variable
|
|
|
set_fact:
|
|
|
swap_file: /swapfile
|
|
|
|
|
|
- name: check if swap file exists
|
|
|
stat:
|
|
|
path: /swapfile
|
|
|
register: swap_file_check
|
|
|
|
|
|
- name: create swap file
|
|
|
command: fallocate -l 2G /swapfile
|
|
|
when: not swap_file_check.stat.exists
|
|
|
|
|
|
- name: Create swap space
|
|
|
command: dd if=/dev/zero of=/swapfile bs=1024 count=2097152
|
|
|
when: not swap_file_check.stat.exists
|
|
|
|
|
|
- name: set permissions on swap file
|
|
|
file:
|
|
|
path: /swapfile
|
|
|
mode: 0600
|
|
|
|
|
|
- name: format swap file
|
|
|
command: mkswap /swapfile
|
|
|
when: not swap_file_check.stat.exists
|
|
|
|
|
|
- name: add to fstab
|
|
|
lineinfile:
|
|
|
dest: /etc/fstab
|
|
|
regexp: /swapfile
|
|
|
line: "/swapfile none swap sw 0 0"
|
|
|
|
|
|
#- name: turn on swap
|
|
|
# command: swapon -a
|
|
|
|
|
|
- name: set swapiness
|
|
|
sysctl:
|
|
|
name: vm.swappiness
|
|
|
value: "1"
|
|
|
```
|
|
|
|
|
|
## Deployment
|
|
|
|
|
|
|
|
|
```sh
|
|
|
ansible-playbook create_droplet.yml
|
|
|
```
|
|
|
|
|
|
Add droplet ip to install_datapool.yml, then run:
|
|
|
```
|
|
|
ansible-playbook -i <DROPLET-IP-HERE>, install_datapool.yml
|
|
|
``` |