Ansible - Java and Tomcat on Ubuntu server installation

Mon, Mar 15, 2021 2-minute read

This blog post serves as my reminder on how to use Ansible to install JAVA and Tomcat on Ubuntu server. This simple playbook installs Tomcat server + all needed packages to run it. This is also an initial post on using Ansible in general.


  • Ubuntu LTS Server installed somewhere
  • OpenSSH running in Ubuntu server installation, so we can connect to and use ansible
  • ansible installed and running on our client

Steps we have to do using Ansible

  • install OpenJDK 1.8, Tomcat 9 etc.
  • ensure Tomcat is running and starts after restart
  • open Tomcat port in firewall to external access

To run this playbook using sudo just simply call this (assumes we have ‘hosts’ available):

ansible-playbook java.yml -i ./hosts --ask-pass  --ask-become-pass

When running ansible-playbook command we are asked to enter the password for our admin user. become: true assures we are running in sudo environment.

It’s my observation, that default Ubuntu installation has no firewall configured, so enabling Tomcat port only means that we cut off ourselves from OpenSSH, as SSH is not opened here. Means we have to open SSH port explicitly as well!

# java.yml
- hosts: "ubuntu"
  user: admin
  become: true

    - name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
        update_cache: yes
        cache_valid_time: 3600

    - name: Install JAVA related Ubuntu packages
        name: "{{ packages }}"
        state: latest
          - openjdk-8-jdk
          - tomcat9
          - tomcat9-examples
          - tomcat9-docs

    - name: Make sure a service is running
        state: started
        enabled: true
        name: tomcat9

    - name: Open Tomcat port
        state: enabled
        rule: allow
        port: "8080"
        proto: tcp

    - name: Open SSH port
        state: enabled
        rule: allow
        port: "22"
        proto: tcp