I aim to install Scala and SBT on a GCP VM running Ubuntu 20.04, utilizing Ansible for automation. Despite multiple attempts, I haven't managed to achieve this goal
First Attempt: Using SDKMAN
Initially, I followed the instructions outlined on the SDKMAN website to install Scala. Below is the Ansible playbook I used:
- name: Setup environment on machines
hosts: machines
become: true
vars:
username: "{{ user_name }}"
tasks:
- name: Install git
apt:
name: git
state: present
update_cache: yes
- name: Install unzip and zip
apt:
name:
- unzip
- zip
state: present
update_cache: yes
- name: Install sdkman and its dependencies
become: true
shell: |
curl -s "https://get.sdkman.io" | bash
args:
executable: /bin/bash
- name: Install Scala 2.12.8 using sdkman
become: true
shell: |
source "/home/{{ username }}/.sdkman/bin/sdkman-init.sh"
sdk install scala 2.12.8
args:
executable: /bin/bash
After running the ansible-playbook command, the following error appeared:
"stderr": "/bin/bash: /home/my_user_name/.sdkman/bin/sdkman-init.sh: No such file or directory
Remarkably, when I SSH into the machine, executing these commands poses no issue.
Second Attempt: Modified SDKMAN Approach
A similar approach was taken by following a gist. I encountered the same error as in my first attempt.
Third Attempt: Manual Installation
Next, I tried a manual installation as described here. The Ansible playbook for this method is as follows:
tasks:
- name: Install git
apt:
name: git
state: present
update_cache: yes
- name: Install unzip and zip
apt:
name:
- unzip
- zip
state: present
update_cache: yes
- name: Install Scala
apt:
name: scala
state: present
- name: Add SBT repository key
shell: sudo apt-key adv --keyserver "hkp://keyserver.ubuntu.com:80" --recv 0x2EE0EA64E40A89B84B2DF73499E82A75642AC823
- name: Add SBT repository
shell: echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
- name: Update apt cache
apt:
update_cache: yes
- name: Install SBT
apt:
name: sbt
state: present
In this case, I got:
FAILED! => {"changed": false, "msg": "Failed to update apt cache: W:Updating from such a repository can't be done securely, and is therefore disabled by default., W:See apt-secure(8) manpage for repository creation and user configuration details
Fourth attempt - use a dedicated role
Lastly, I tried using a specialized role with the following configuration:
become: true
roles:
- role: Comcast.sdkman
sdkman_user: sudo
sdkman_auto_answer: true
sdkman_update: true
sdkman_install_packages:
- { candidate: scala, version: '2.12.8' }
- { candidate: sbt, version: '1.9.4' }
But I'm getting:
FAILED! => {"changed": false, "cmd": "set -o pipefail\ngetent passwd sudo | cut -d: -f6\n", "delta": "0:00:00.007774", "end": "2023-09-28 19:27:22.770244", "msg": "non-zero return code", "rc": 2, "start": "2023-09-28 19:27:22.762470", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}