Howto: Installing Rbenv with Ansible

This article is about how to install rbenv automatically with ansible on remote servers and users. Suitable for beginners.

Install ansible and rbenv role plugin.

# apt-get install ansible
brew install ansible
ansible-galaxy install zzet.rbenv

Define hosts where it should run.

# ./hosts
[web]
myrtana.sk

YAML configuration file with Rbenv version and Ruby version to be installed and user where it should switch to. This file is called "playbook".

# rbenv.yml
- hosts: web
  gather_facts: true # https://github.com/zzet/ansible-rbenv-role/issues/37
  vars:
    rbenv:
      env: user
      version: v1.1.2
      default_ruby: 2.6.5
      rubies:
        - version: 2.6.5
          env:
            RUBY_CONFIGURE_OPTS: "--enable-shared --with-jemalloc"
    rbenv_extra_depends:
      - libjemalloc1
      - libjemalloc-dev
  roles:
    - role: zzet.rbenv
      rbenv_users:
        - myrtana_sk

Ansible needs to use root user, because jemalloc is going to be installed. Create a SHH key protected by a password.

ssh-keygen -t ed25519 -b 4096 -C 'my key'

Add the key.pub into .ssh/authorized_keys on server(s) file or create the directory and file in case it doesn't exist.

Tell SSH to use the key.

# .ssh/config
Host *myrtana.sk
  User root
  IdentityFile ~/.ssh/mykey

Finally, run it.

ansible-playbook -i hosts rbenv.yml

You should see lots of output and at the end everything passed.

PLAY RECAP ***************
myrtana.sk                 : ok=19   changed=9    unreachable=0    failed=0    skipped=24   rescued=0    ignored=0

P.S. root login needs to be allowed in my way, but you could use become_user config in the playbook to switch to root user from regular one.