Since 2017, I’ve been managing & maintaining a growing number of Ruby & Elixir applications for a client. This includes a fairly complete Ansible setup.
If you use Ansible, make sure to check out tools like
ServerSpec and Vagrant, which provide massive help to iteratively implement stuff like what you are about to read!
A recurring task consists of installing and upgrading the ruby versions in use (e.g. MRI Ruby, JRuby), carefully following the “Ruby Maintenance policy”.
A real-life example will have more variants, based on applications and environments (e.g. maybe you’ll want “candidate rubies” available only on staging environments initially).
By default with this setup, though, you will not have the ability to specify the exact version of rubygems you want to install (and same goes for bundler; the role supports rvm1_bundler_install: true, which installs the latest).
As you may know already, tight version control over your dependencies, including rubygems and bundler, is important to get reproducible setups and deployments, and keep a healthy lifestyle!
So here is my current solution for this.
Installing a given Rubygems version
In your variables, add:
In a install_rubygems.yml file, add:
(This algorithm assumes there is always a pre-installed version of rubygems, which appears to be the case on the setups I manage).
You can then invoke it for each specified ruby:
Installing a given Bundler version
In the case of Bundler, the task is a bit more involved, for two reasons:
Installing Ruby may not install Bundler (hence checking its version could lead to errors).
Multiple versions could be installed at once (unlike rubygems) so we must be more careful & verify active version.
In your variables, add:
Then in install_bundler.yml:
A tradeoff of this design is that downgrades cannot be achieved, you will have to create separate tasks for that, but otherwise it works very nicely.
Final words
As mentioned earlier in this post, if you have to implement similar stuff, make sure to check out ServerSpec and Vagrant, both very useful tools to help you create and improve your Ansible tasks and roles!