Ansible Rails Toolkit
Ansible roles and playbooks for deploying a Rails application to EC2. This was a learning experiment so keep it in mind if you intend to use it.
The system will configure:
- A Postgres instance
- An instance with Nginx acting as a load balancer for the web servers
- A number of web servers with puma
- Monit for monitoring the different servers
- A Jenkins instance
It also includes a playbook for deploying to different environments using Ansible (without using Capistrano or any other third-party system).
It includes 2 environments:
production: With an EC2-based inventory.
production-vagrant: Which runs on virtual machines orchestrated by Vagrant. It is meant for experimenting with the production environment locally, which is much more suitable for development time.
It would be pretty trivial to add other environments such as staging. Just copy an existing environment and tweak it.
Notice there are some files that are soft links to avoid duplication. For example:
ansible-galaxy install -r requirements.yml
Configure app variables
You can configure basic properties such as your app name and git repo in
Edit and encrypt sensitive data (vault files)
You should use Ansible Vault to encrypt the sensitive data such as passwords and keys in:
The system assumes that you have a file in
~/ vault_password.txt containing your password. That way, it won't ask you for it in every operation.
Configure your EC2 servers
environments/production-vagrant/group_vars/all/vars.yml to configure your EC2 nodes. Example: the type and number of instances you want to use.
To provision EC2 instances:
ansible-playbook -i environments/production provision.yml
Provisioning instances will create the instances in EC2 and configure them with all the artifacts they need.
If you only want to launch the configurations of instances:
ansible-playbook -i environments/production configure.yml
To deploy to a given environment:
ansible-playbook -i environments/production deploy.yml
Working with Vagrant
Vagrant up will create the virtual machines and configure them:
cd environments/production-vagrant vagrant up
You can launch the configuration only with:
ansible-playbook -i environments/production-vagrant configure.yml
Or, alternatively, with
Then you could deploy normally with:
ansible-playbook -i environments/production-vagrant deploy.yml
Provision Jenkins instance
ansible-playbook -i environments/production services.yml
- I learned Ansible (and many other basic devops concepts) reading this book which I totally recommend
- I also learned a lot by reading the code of the many Ansible roles created by Jeff Geerling (the book author)
- I used the approach proposed in this article for managing multiple environments
- I learned a ton about EC2 and VPCs with this article and actually used a bunch of code from it.