Objective:
Use Ansible to automate the installation and configuration of Apache Tomcat on Ubuntu servers.
Project Structure
ansible-tomcat/
├── hosts
├── tomcat-install.yml
└── templates
└── tomcat.service.j2
Step 1: Create Inventory File
Create hosts file:
[tomcat]
localhost ansible_connection=local
Step 2: Create Tomcat Service Template
Create templates/tomcat.service.j2
[Unit]
Description=Apache Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=always
[Install]
WantedBy=multi-user.target
Step 3: Create Ansible Playbook
Create tomcat-install.yml
---
- name: Install and Configure Tomcat
hosts: tomcat
become: yes
vars:
tomcat_version: "10.1.42"
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Java
apt:
name: openjdk-17-jdk
state: present
- name: Create Tomcat Group
group:
name: tomcat
state: present
- name: Create Tomcat User
user:
name: tomcat
group: tomcat
home: /opt/tomcat
shell: /bin/false
system: yes
- name: Create Tomcat Directory
file:
path: /opt/tomcat
state: directory
- name: Download Tomcat
get_url:
url: "https://downloads.apache.org/tomcat/tomcat-10/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz"
dest: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"
- name: Extract Tomcat
unarchive:
src: "/tmp/apache-tomcat-{{ tomcat_version }}.tar.gz"
dest: /opt/tomcat
remote_src: yes
extra_opts:
- --strip-components=1
- name: Set Ownership
file:
path: /opt/tomcat
owner: tomcat
group: tomcat
recurse: yes
- name: Make Scripts Executable
shell: chmod +x /opt/tomcat/bin/*.sh
- name: Deploy Tomcat Service File
template:
src: tomcat.service.j2
dest: /etc/systemd/system/tomcat.service
- name: Reload Systemd
systemd:
daemon_reload: yes
- name: Start and Enable Tomcat
systemd:
name: tomcat
state: started
enabled: yes
- name: Open Port 8082
ufw:
rule: allow
port: "8082"
proto: tcp
ignore_errors: yes
Step 4: Verify Ansible Connectivity
ansible tomcat -i hosts -m ping
Step 5: Run Playbook
ansible-playbook -i hosts tomcat-install.yml


Step 6: Verify Tomcat Service
sudo systemctl status tomcat

Step 7: Verify Port
sudo ss -tulpn | grep 8082
Step 8: Access Tomcat
http://localhost:8082

Latest posts by Mahesh Wabale (see all)
- Install and Configure Apache Tomcat Using Ansible - June 16, 2026
- Docker Container Monitoring Dashboard using Prometheus and Grafana - June 15, 2026
- Kubernetes Cluster Monitoring using Grafana & Prometheus - June 13, 2026