To install Java 8 on Amazon EC2 Linux using Ansible Playbook

0

Hi AWS, I have written an Ansible Playbook to install Java8 on EC2 Linux instance but while doing so I am facing this error fatal: [11.111.111.111]: FAILED! => {"changed": false, "failures": ["No package java-1.8.0-openjdk available."], "msg": "Failed to install some of the specified packages", "rc": 1, "results": []}

The Ansible Playbook code is:

---
- hosts: solr
  become: true

  vars_files:
    - vars.yml
  
  pre_tasks:
    - name: Update apt cache if needed.
      yum: 
        update_cache: true 
  
  handlers:
    - name: restart solr
      service: name=solr state=restarted
  
  tasks:
    - name: Install Java.
      yum:
        name: java-1.8.0-openjdk
        state: present

I have tried to install Java using epel package but still facing the error.

Can you please help me in figuring out the issue as I have checked the /etc/ansible/hosts file is updated with right IP adddress.

profile picture
Arjun
asked 9 months ago914 views
2 Answers
0

It seems like the Ansible Playbook is trying to install the OpenJDK 8 package using the yum module, but the package is not found in the available repositories. This can happen if the required repository is not enabled or the package name is incorrect for the particular distribution you are using.

Since you're working with Amazon Linux, you can try the following steps to resolve the issue:

  • Update the Package Name: The package name might be different based on the distribution you are using. If you are using Amazon Linux 2, the correct package name might be java-1.8.0-openjdk-devel. Update the playbook accordingly:
- name: Install Java.
  yum:
    name: java-1.8.0-openjdk-devel
    state: present
  • Ensure the Correct Repository is Enabled: Amazon Linux 2 uses the amzn2-core repository for OpenJDK 8. You might need to enable this repository if it's not already enabled. You can add a task to enable the repository before installing Java:
pre_tasks:
  - name: Enable amzn2-core repository
    command: amazon-linux-extras enable java-openjdk11
    when: ansible_distribution == "Amazon" and ansible_distribution_major_version == "2"

  - name: Update yum cache if needed.
    yum: 
      update_cache: true 
  • Consider Using the package Module: Instead of using the yum module, you can use the package module, which is more abstract and works with different package managers. This might help if there's something specific to the yum module causing the issue:
- name: Install Java.
  package:
    name: java-1.8.0-openjdk-devel
    state: present
  • Debugging: If the issue persists, you can add a debug task to print the available repositories and packages. This might give you more insights into what's going wrong:
    - name: List yum repos
      command: yum repolist
      register: repos
    - debug: var=repos.stdout_lines

    - name: Search for Java packages
      command: yum search java-1.8.0-openjdk
      register: java_packages
    - debug: var=java_packages.stdout_lines

Make sure to adapt the package name or repository based on the exact version and distribution of Amazon Linux you are using. Testing the commands manually on the EC2 instance might help you determine the correct package name and repository.

profile picture
answered 9 months ago
0

The solution provided above didn't work, however the Amazon Linux version which I am having is 2023 and it uses Corretto 8. Here is the link: https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/amazon-linux-install.html

profile picture
Arjun
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions