A role to deploy new env vars for configuring a rails app by interpolating values into template files and uploading them to servers.
Using ansible-galaxy:
$ ansible-galaxy install oozou.deploy-rails-env-vars
Using requirements.yml
:
- src: oozou.deploy-rails-env-vars
path: roles/
name: oozou.deploy-rails-env-vars
In the spirit of 12 Factor Apps recommendation for configuring apps, this ansible role provides mechanisms to manage the following commonly used rails configuration files. You can use ansible-vault to encrypt configuration key/values.
This is created to be used with capistrano and symlinked config files from a shared directory for config files.
- secrets.yml (rails secrets.yml file)
- application.yml (used in conjunction with Figaro)
- database.yml (default rails database configuration file)
- newrelic.yml (newrelic configuration file)
- .rbenv-vars (used in conjunction with rbenv-vars)
- /etc/environment (debian system wide environment)
- name: Playbook to deploy rails related configuration files to rails app servers
hosts: rails_app_servers
roles:
- role: oozou.deploy-rails-env-vars
deploy_rails_config_dir: /path/to/shared/capistrano/config/dir
etc_environment:
deploy: true
template: /local/path/to/your/etc/environment.j2
dest: /etc/environment
secrets_yml:
deploy: true
template: /local/path/to/your/secrets.yml.j2
dest: /server/path/to/put/secrets.yml
application_yml:
deploy: true
template: /local/path/to/your/application.yml.j2
dest: /server/path/to/put/application.yml
dot_rbenv_vars:
deploy: true
template: /local/path/to/your/dot.rbenv-vars.j2
dest: /server/path/to/put/.rbenv-vars
create_symlink: true
symlink_dest: /server/path/to/create/symlink/to/.rbenv-vars
symlink_owner: username
symlink_group: group
database_yml:
deploy: true
template: /local/path/to/your/database.yml.j2
dest: /server/path/to/put/database.yml
newrelic_yml:
deploy: true
template: /local/path/to/your/newrelic.yml.j2
dest: /server/path/to/put/newrelic.yml
If you really want to use just one file to config your app, you can probably get away with using .rbenv-vars and only deploy this one file. In order for this to work, you will have to setup rbenv on your server for the user that puma (and sidekiq) run as. You will have to have to also write all your configuration files (application.yml, database.yml, etc) to refer to ENV.
The main benefit of doing it this way is that you only have to deploy one file and you can check in all the other configuration files (assuming they refer to the ENV variables) into the repository.
You can deploy config files that have the configuration values interpolated into configuration files.
Be sure to setup capistrano to use :linked_files
and :linked_dirs
to symlink
the files deployed by ansible and this role to the rails app deploy directory.
The reason that .rbenv-vars can be configured to be symlinked is because the author usually has the destination be the shared directory so it is in the same directory as all the other configuration files. However, capistrano cannot symlink the .rben-vars to the $HOME directory because it's not in the app directory. Ansible is used to create the symlink from the shared directory to the home directory.
For example:
$ ln -s /home/deploy_user/apps/your_app/shared/dot.rbenv-vars /home/deploy_user/.rbenv-var
- Twilio blog post on configuring rails apps
- 12 Factor Apps
- How to Set and Unset Local, User and System Wide Environment Variables in Linux
- rbenv-vars
- THE MARRIAGE OF FIGARO… AND RAILS
- Figaro gem
- dotenv gem
- envyable gem
Rick Apichairuk