Skip to content

Instantly share code, notes, and snippets.

@skinofstars
Last active June 2, 2020 08:16
Show Gist options
  • Save skinofstars/b5bb07d7ae8cb67deb49 to your computer and use it in GitHub Desktop.
Save skinofstars/b5bb07d7ae8cb67deb49 to your computer and use it in GitHub Desktop.
Ansible static site deploy
[live]
live ansible_ssh_host=12.34.56.78 ansible_ssh_user=ubuntu
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root {{ release_path }}/{{ timestamp.stdout }};
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
# ansible-playbook -l live -i hosts deploy.yml
# install nginx
# create/check release folder
# copy public to remote folder
# update nginx config
# restart nginx
- hosts: live
remote_user: ubuntu
vars:
http_port: 80
release_path: "/home/{{ ansible_ssh_user }}/releases"
tasks:
- name: Get time
shell: "date +%s"
register: timestamp
- name: Nginx is present
sudo: true
apt: pkg=nginx state=present
- name: Releases dir exists
file: state=directory path={{ release_path }}
- name: Release path exists
file: state=directory path={{ release_path }}/{{ timestamp.stdout }}/
- name: Copy local to remote
copy: src=public/ dest={{ release_path }}/{{ timestamp.stdout }}/
- name: Update nginx default config
sudo: true
template: src=nginx.default.j2 dest=/etc/nginx/sites-enabled/default
- name: Restart nginx
sudo: true
service: name=nginx enabled=yes state=restarted
- name: Tidy up to last 5 releases
command: bash -lc "cd {{ release_path }} && rm -rf `ls -t | awk 'NR>5'`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment