'How to run a Java download command using shell in ansible

I want to download and install Java in one our VMs using Ansible. I tried with yum and that was successful but, as part of requirement I was asked to do it another way.

To do so, I tried

- name: "[version-update] Download Java "
  shell: 
    args:
      chdir: /opt/xcal/apps

Where the command to execute is

wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm

But, while trying this the editor is showing errors.
I tried this command with a double quotes surrounding it and just pasting it but both are throwing errors.

I understand

"Cookie: oraclelicense=accept-securebackup-cookie"

This is the part that's causing the issue. Is there a way to include this in the command as well?



Solution 1:[1]

One working approach with the shell module can be

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Download Java
    shell:
      cmd: 'curl -x "localhost:3128" -O https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm'
      chdir: "/home/{{ ansible_user }}"
      warn: false
    register: result

  - name: Show result
    debug:
      msg: "{{ result }}"

An other one with the get_url module.

  - name: Download Java
    get_url:
      url: "https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm"
      dest: "/home/{{ ansible_user }}"
      use_proxy: true
    register: result
    environment:
      HTTP_PROXY: "localhost:3128"
      HTTPS_PROXY: "localhost:3128"

  - name: Show result
    debug:
      msg: "{{ result }}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 β.εηοιτ.βε