new file 100644
# Ansible playbook for basic web server configuration.
#
# Run with:
# ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook deploy/install.yml -i deploy/inventory.ini --verbose
# Notes:
# /etc/apache2 uses OS defaults aside from "site-available", "sites-enabled" and
# "conservancy.conf".
# Current site does not use "django.contrib.staticfiles", so no need to run
# `collectstatic`.
# SQLite database lives at /var/lib/www/database.
# Disabled Rackspace CDN videos.
- name: Configure web server
hosts: web
become: true
vars:
ansible_ssh_pipelining: true
tasks:
- name: Install unattended upgrades
apt:
name: unattended-upgrades
- name: Configure unattended upgrades overrides
# See defaults in 50unattended-upgrades.
copy:
dest: /etc/apt/apt.conf.d/20auto-upgrades
content: |
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
Unattended-Upgrade::Mail "root";
- name: Add extensive history logging
blockinfile:
path: /etc/bash.bashrc
block: |
# Write to history file immediately (rather than only when shell is
# closed). For setting history length see HISTSIZE and HISTFILESIZE in
# bash(1).
shopt -s histappend
PROMPT_COMMAND='history -a'
HISTSIZE=1000000
HISTFILESIZE=1000000
insertafter: EOF
- name: Mount the media volume
# OSUOSL VMs come with fixed storage that's tied to the cores and RAM
# selection. Easier to put this data on an external volume.
ansible.posix.mount:
src: /dev/sdb1
path: /var/www/media
fstype: ext4
state: mounted
boot: false
- name: Install Apache
name: apache2,libapache2-mod-wsgi-py3
- apache2_module:
state: present
name: ssl
name: rewrite
# The proxy and proxy-http modules are required to rewrite /.well-known/
# requests to the mail server if the file doesn't exist. This is use to
# renew Let's Encrypt certificates.
name: proxy
name: proxy-http
- name: Install Postfix
pkg:
- postfix
# libsasl2-modules fixes "SASL authentication failure: No worthy mechs found"
- libsasl2-modules
- mailutils
# # Commented because you only want this on first run ever.
# - name: Add file for SMTP credentials
# copy:
# dest: /etc/postfix/sasl_passwd
# content: |-
# # After updating, run `sudo postmap hash:/etc/postfix/sasl_passwd`.
# [mail.sfconservancy.org]:587 conference@sfconservancy.org:PASSWORD
- name: Configure Postfix for relaying
src: postfix/main.cf
dest: /etc/postfix/main.cf
notify:
- restart postfix
- name: Alias mail to root
dest: /etc/aliases
content: |-
postmaster: root
root: sysadmin@sfconservancy.org, sysadmin@sturm.com.au
- name: Install Certbot
name: certbot, python3-certbot-apache
- name: Install Python dependencies
name: python3-django,python3-bs4,python3-html5lib,python3-django-countries
- name: Install Python essentials
name: python3-venv,python3-pip,python3-wheel
- name: Install Python build dependencies
name: build-essential,python3-dev,libffi-dev
- name: Security settings
name: fail2ban
- name: Disable SSH password authentication
lineinfile:
path: /etc/ssh/sshd_config
line: 'PasswordAuthentication no'
regexp: 'PasswordAuthentication '
- restart sshd
- name: Install utilities
name: tmux,curl,git,magic-wormhole,htop,rsync
- name: Create the project directory
file:
path: /var/www/website
state: directory
owner: www-data
group: www-data
mode: '0755'
# TODO: Needs to force owner to www-data:www-data
- name: Git checkout
ansible.builtin.git:
repo: 'https://k.sfconservancy.org/website'
dest: /var/www/website
version: master
remote: upstream
- name: Create the database directory
path: /var/lib/www/database
- name: Create static dir
path: /var/www/website/conservancy/static
- name: Install `netfilter-persistent` && `iptables-persistent` packages
- iptables-persistent
- netfilter-persistent
- name: Install iptables # May need kernel reload/reboot
name: iptables,iptables-netflow-dkms
- name: Flush existing firewall rules
iptables:
flush: true
- name: Firewall rule - allow all loopback traffic
action: append
chain: INPUT
in_interface: lo
jump: ACCEPT
- name: Firewall rule - allow established connections
ctstate: ESTABLISHED,RELATED
- name: Firewall rule - allow port ping traffic
protocol: icmp
- name: Firewall rule - allow port 22/SSH traffic
destination_port: '22'
protocol: tcp
- name: Firewall rule - allow port 80/HTTP traffic
destination_port: '80'
- name: Firewall rule - allow port 443/HTTPS traffic
destination_port: '443'
- name: Firewall rule - drop any traffic without rule
jump: DROP
ip_version: ipv6
- name: Firewall rule - allow all loopback traffic v6
- name: Firewall rule - allow established connections v6
- name: Firewall rule - allow port ping traffic v6
- name: Firewall rule - allow port 22/SSH traffic v6
- name: Firewall rule - allow port 80/HTTP traffic v6
- name: Firewall rule - allow port 443/HTTPS traffic v6
- name: Firewall rule - drop any traffic without rule v6
handlers:
- name: restart sshd
service:
name: ssh
state: reloaded
- name: restart postfix
name: postfix