Ansible Java maven playbook
Install Maven, and Java along with it, on all managed nodes with a short playbook, verify both versions, and learn how to pin a specific JDK instead of relying on dependencies.
A Java build server needs two things: a JDK to compile the code and Maven to run the build. On Amazon Linux you do not even have to ask for Java separately. The maven package depends on a Java runtime, so the package manager installs Java automatically when it installs Maven. That makes this playbook almost identical to the Git one, which is exactly the point: once you know the pattern, installing new software is a two-line change.
Reuse the previous playbook
There is no need to type everything again. Print the Git playbook, copy it, and paste it into a new file:
sudo su - ansible
cat git-install.yml
vi maven-java.ymlChange only the task name and the package name:
---
- hosts: all
become: true
tasks:
- name: maven java installation
yum:
name: maven
state: latest
...| Line | Why it is there |
|---|---|
hosts: all | Install on every managed node. Use hosts: webservers if only the application servers should get Java and Maven |
become: true | Installing packages needs root, as explained in the Git playbook |
yum: with name: maven | Install the maven package. Amazon Linux uses yum/dnf, Ubuntu uses apt |
state: latest | Install, or upgrade to the newest version in the repository |
Run it
ansible-playbook maven-java.yml --syntax-check
ansible-playbook maven-java.ymlTASK [maven java installation] *************************************************
changed: [172.31.9.250]
changed: [172.31.12.18]Maven brings quite a few dependencies with it, so this run takes longer than the Git one. changed on both hosts means Maven and Java are now installed everywhere.
Verify Maven and Java
On a managed node:
mvn --versionApache Maven 3.8.4 (Red Hat 3.8.4-3.amzn2023.0.5)
Maven home: /usr/share/maven
Java version: 17.0.15, vendor: Amazon.com Inc., runtime: /usr/lib/jvm/java-17-amazon-corretto.x86_64java --versionopenjdk 17.0.15 2025-04-15 LTS
OpenJDK Runtime Environment Corretto-17.0.15.6.1 (build 17.0.15+6-LTS)Two things to notice:
- The package is called
maven, but the command ismvn. Typingmaven -versiongivescommand not found, which also happens in the lecture - Java arrived as a dependency: Amazon Corretto 17, Amazon's free build of OpenJDK
Or check every node at once from the control node:
ansible all -a "mvn -v"
ansible all -a "java -version"Pinning the Java version you actually want
Getting Java "for free" through Maven is convenient, but you do not control which Java you get. It is whatever the maven package depends on today. If your application needs Java 21, say so explicitly. The package modules accept a list of names:
---
- name: Prepare Java build servers
hosts: all
become: true
tasks:
- name: Install JDK 21 and Maven
ansible.builtin.dnf:
name:
- java-21-amazon-corretto-devel
- maven
state: present| Package | What it contains |
|---|---|
java-21-amazon-corretto-devel | The full JDK 21 (compiler javac included), not just the runtime |
maven | Maven itself |
When several JDKs are installed, java -version shows the one selected by the system alternatives setting, and you can switch with sudo alternatives --config java. On Ubuntu the equivalent packages are openjdk-21-jdk and maven, installed with ansible.builtin.apt.
Why the build tools matter
These are the same tools the Maven course runs by hand on one machine. With this playbook you can prepare ten identical build servers, or rebuild one from scratch in a few minutes, instead of repeating the installation steps on each machine.
Try It Yourself
- Change
hosts: alltohosts: webserversand run it. Confirm withansible dbservers -a "mvn -v"that nothing new happened on the database node. - Run the playbook twice in a row and compare the
changedcounts. - Install
java-21-amazon-corretto-develalongside Java 17 and check which onejava -versionreports. - Write a playbook that removes Maven with
state: absent. Does Java get removed too? Why or why not?
Official references
- ansible.builtin.dnf module
- ansible.builtin.yum module (redirect to dnf)
- Amazon Corretto 21 installation on Amazon Linux
Written By: Shramik Masti
How is this guide?
Last updated on
Ansible Git Installation Playbook
Install Git on every managed node with a playbook, understand become for root privileges, choose between yum, dnf, apt and package, and verify the result from the control node.
Ansible Playbook: Static Website Hosting
Write a three-task playbook that installs the Apache httpd web server, copies an index.html from the control node and starts the service, then open the site in a browser.
