Skip to content

Instantly share code, notes, and snippets.

@emilio2hd
Last active January 3, 2023 10:48
Show Gist options
  • Save emilio2hd/c910661bff37246bee17 to your computer and use it in GitHub Desktop.
Save emilio2hd/c910661bff37246bee17 to your computer and use it in GitHub Desktop.
Ansible role to install sdkman and gradle
---
sdkman_script_install_url: http://get.sdkman.io
sdkman_folder: '/home/vagrant/.sdkman'
gradle_version: 2.3
---
- name: Check if sdkman is installed
shell: test -d {{sdkman_folder}} && echo "yes" || echo "no"
register: is_sdkman_installed
- name: Install SDKMAN!
when: is_sdkman_installed.stdout == "no"
shell: curl -s {{sdkman_script_install_url}} | bash executable=/bin/bash
sudo: no
- name: Ensure presence of bashrc
file: state=touch path={{user_home_path}}/.bashrc
sudo: no
- name: Add sdkman in .bashrc
lineinfile: dest={{user_home_path}}/.bashrc regexp="\/.sdkman/bin\/sdkman\-init\.sh" line='[[ -s "{{sdkman_folder}}/bin/sdkman-init.sh" && ! $(which sdkman-init.sh) ]] && source "{{sdkman_folder}}/bin/sdkman-init.sh"'
sudo: no
- name: Installing gradle
# This will read your bash profile file, which would setup sdkman-init.sh.
command: bash -lc "sdk install gradle {{gradle_version}}"
sudo: no
@timogoosen
Copy link

timogoosen commented Jul 29, 2021

I used a modified version of this, this is what worked for me for anyone interested:

playbook/gradle.yml

- name: Check if sdkman is installed 
  shell: test -d {{sdkman_folder}} && echo "yes" || echo "no"
  register: is_sdkman_installed

- name: Install SDKMan Package Manager
  shell: curl -s {{sdkman_script_install_url }} | bash executable=/bin/bash
  when : is_sdkman_installed.stdout == "no"

- name: Ensure presense of .bashrc file
  file: state=touch path={{user_home_path}}/.bashrc

- name: Add SDKMan in .bashrc file
  lineinfile: dest={{user_home_path}}/.bashrc regexp="\/.sdkman/bin\/sdkman\-init\.sh" line='[[ -s "{{sdkman_folder}}/bin/sdkman-init.sh" && ! $(which sdkman-init.sh) ]] && source "{{sdkman_folder}}/bin/sdkman-init.sh"'

- name: Installing gradle with sdkman
  shell: "source /home/admin/.sdkman/bin/sdkman-init.sh && sdk install gradle {{gradle_version}}"
  args:
    executable: /bin/bash

This goes in your vars:


# Vars for gradle
sdkman_script_install_url: http://get.sdkman.io
sdkman_folder: '/home/admin/.sdkman'
gradle_version: 6.5
user_home_path: '/home/admin'

This part depends on your username.

@emilio2hd
Copy link
Author

Hey Timo.
It's been a while since the last time I touched on this.
Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment