Fast confguration server and deploy with Ansible and Capistrano
###Info This example shows how to make a simple initial configuration server based on Ubuntu 14.04 x64 to deploy Rails application with
- Ruby on Rails on back-end
- ReactJs (es6) on front-end
- Postgress on database
- Nginx and puma on web-server
The efficiency was tested
- DigitalOcean Ubuntu 14.04 x64
- Ruby 2.3.1
- Rails 5.0.0
- Puma 3.4.0
- Node 6.3.0 (webpack for es6)
- Capistrano 3.4.0
- Ansible 2.1.0.0
####1. Change variables to setup Ansible
config/ansible/group_vars/all.yml
- ruby_version
- name - application name (use to create folder)
- node_version - like in package.json
- db:
- name: database name
- user: database user
- password: database password
####2. Run init_user This step will remove access the server like root and will create a new deploy user Command:
$ cd config/ansible
$ ansible-playbook -i SERVER_IP, init_user.yml
Tasks:
- Check ping host
- Create new group for user
- Create new user with group
- Ensure sudoers.d is enabled
- Remove password from user
- Add public keys for SSH
- Remote access with SSH for root user
- Remote password SSH access
- Restart SSH to apply changes
- Update apt-get libraries
####3. Run playbook This step will configure server and includes few parts:
- Install dependencies (relating to you'r rails application)
- Check ping host
- apt-get upgrade
- Install dependencies from /group_vars/all.yml
- Install ruby through rbenv (may take longer)
- Update rbenv repository
- Add rbenv to path
- Copy initialization to profile
- Check if ruby-build installed
- Create temporary folder
- Clone ruby-build repository
- Install ruby-build
- Remove temporary folder
- Check ruby installed
- Install ruby
- Set global verson ruby
- Rehash rbenv
- Copy germs
- Install and configuring Postgres
- Check default user enabled
- Copy auth type
- Restart service
- Create shared folder for database.yml
- Copy database.yml to shared folder
- Create database
- Ensure user has access to database
- Change privilege user
- Install and configuring Nginx and Puma
- Check enabled config
- Create folder
- Copy config file
- Restart Nginx
- Install Node through nvm
- Install dependencies
- Install nvm
- Source nvm in ~/.profile
- Install Node
- Check default Node version
- Set default Node version
- Else small configuring, some relating to DigitalOcean
- Copy secrets.yml for Rails
- set CHMOd for rbenv
- Install bundler
- Run rbenv Rehash
- Install locales package (for DigitalOcean)
- Ensure locale exists (for DigitalOcean)
- Add locale to environment (for DigitalOcean)
Command:
$ ansible-playbook -i SERVER_IP, playbook.yml
####4. Change variables to setup Capistrano config/deploy.rb Server is ready for deploying through Capistrano But after initial setup Capistrano, you need to change the variables according to the ansible variables
:application - the same name: in all.yml :user - the same remote_user: in playbook.yml and user: in all.yml :rbenv_ruby - the same ruby_version: in all.yml :nvm_node, - the same nvm.node_version: in all.yml
####5. Run deploy
$ cd ../..
$ cap staging deploy