Install SDKMAN on Ubuntu Server through Ansible Playbook

356 Views Asked by At

I've to automate installation of new packages on servers using Ansible. I need some specific versions of packages which can be installed easily using SDKMAN. Installing SDKMAN is also simple, just run

curl -s "https://get.sdkman.io" | bash

and SDKMAN is installed for the current user. But, I have to now use Ansible to automate this setup.

I'm using the following modified command, as suggested in [Ref1][1]

- name: Download and install SDKMAN
  get_url:
    url=https://get.sdkman.io
    dest=/opt
    mode=755

- name: Load SDKMAN environment
  command: source "/opt/.sdkman/bin/sdkman-init.sh"
  changed_when: false

This command is installing index.html of the sdkman's url. I'm new to Ansible, and I know only a little bit of modification is needed to make it working. But, I'm not able to figure it.

So, I modified the above step. I'm now renaming index.html to sdkman-init.sh as I cat the file and found that it is the bash script to install sdkman.

- name: Download and install SDKMAN
  get_url:
    url=https://get.sdkman.io
    dest=/opt
    mode=755

- name: change dir
  file:
    path: /opt/.sdkman/bin
    state: directory

- name: rename file
  command: mv /opt/index.html /opt/.sdkman/bin/sdkman-init.sh

- name: make script executable
  command: chmod 777 /opt/.sdkman/bin/sdkman-init.sh

- name: now, run scipt for sdkman
  command: /opt/.sdkman/bin/sdkman-init.sh

- name: Load SDKMAN environment
  command: source "${HOME}/.sdkman/bin/sdkman-init.sh"
  changed_when: false
  environment:
    HOME: /home/myotheruser  # Use the appropriate variable for the user's home directory

I'm using admin user to perform the task, and I have to install for myotheruser. admin user has root privilege, and I'm not able to understand what to do so that running the script will install sdkman for myotheruser.

If I' manually doing the steps I'm following through ansible, I'm able to install sdkman for admin user. But, ansible is installing it for root (not even for admin or my target myotheruser). And my target is to install for myotheruser who does not have root privilege. [1]: Failed to run curl command in Ansible task

1

There are 1 best solutions below

1
isccarrasco On

This could be managed in two different ways, the one is using one of the roles developed for managing the installation of the java version using SDK Man directly, you only need to specify some variables in your playbook, there are the two options:

You can check the documentation and see how could be used.

In the other hand, what I do for using it directly in my playbook is in this way.

---
- hosts: all
  vars:
    - home: /home/myuser
    - java_version: 21

  pre_tasks:
    - name: Installing SDKMAn dependencies
      apt:
        state: latest
        name:
          - zip
          - unzip

  tasks:
    - name: Installing SDK Man
      become: no
      shell: "curl -s 'https://get.sdkman.io' | bash"
      args:
          executable: /bin/bash

    - name: Adding SDK Man to bash_profile
      lineinfile: path={{ home }}/.bash_profile
        line={{ item }}
        state=present
        create=yes
      with_items:
        - 'source "$HOME/.sdkman/bin/sdkman-init.sh"'

    - name: Installing Java
      shell: source ~/.bash_profile && sdk install java {{ java_version }}
      become: no
      args:
        executable: /bin/bash

This works for me, hopefully it could be useful for you!