Apache Tomcat is often used as an application server for strictly web-based applications. It basically executes Java servlets and renders web pages that include JSP coding.
Manual installation on Tomcat is time-consuming. Using Ansible we can easily automate the installation process.
In this tutorial we learn how to install Tomcat on Ubuntu 20.04 using Ansible.
Here we are going to use:
- One control node - where we are going to install and configure the ansible. From the control node, we are going to start the Ansible playbook to install Tomcat 9.
- You need to have an open SSH connection to the remote host.
- One host node - where ansible will install and start Tomcat 9.
- Both the control node and host have private IP addresses and both machines are Ubuntu 20.04
Installing Ansible on Control Node on Ubuntu 20.04
First, update you system with the standard command:
$ sudo apt update
After update install ansible running the following command:
$ sudo apt install ansible -y
To verify where the ansible is installed and located run the command:
$ which ansible
You should see something like this:
If you list a directory in /usr/bin
$ ls /usr/bin
You will find Ansible installed packages:
To verify the version of installed Ansible run the command:
$ ansible --version
You would get output something like this:
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
As you can see besides the ansible version you will also get:
- Where is placed the ansible config file
- Where is placed configured search path and modules
- Where is the ansible python module location
- Executable location and python version
The main configuration file is stored in /etc/ansible/hosts file.
You need to add your localhost or server IP to the file, or if you have a private IP address, you need to add it.
Open the hosts file with your favorite editor:
$ sudo nano /etc/anisble/hosts
You will see the hosts configuration file, something like this:
As you can see there is a section that belongs in the #Ex 1 section, add there your remote host IP address without # expression.
Next, to verify that you have host with this IP's address run the following command:
$ sudo ansible all -i hosts --list-hosts
You will get the list of hosts, something like this output in the image:
The next step is to add the Public SSH key from Ansible control node to the remote host machine.
This step is essential to allow Ansible to install Tomcat on a remote server.
Generate your ssh keys by the following command:
$ sudo ssh-keygen
After generating keys you will see where are keys stored. For us is now only important the public key.
Now we need to copy the public key to a server. Remember that we are doing this because we need to allow Ansible to install Tomcat to the remote server from the Control node.
NOTE: Only the public key is copied to the server. The private key should never be copied to another machine.
Use a command like the following to copy SSH key:
$ sudo ssh-copy-id -i ~/.ssh/id_rsa.pub email@example.com
Where are the user=linoxide and host=IP address for SSH remote login.
After copy the key to the remote machine you will need to enter your password for remote ssh connection.
Now everything is set and ready to run ansible-playbook.
Install Tomcat 9 on remote host node using Ansible
Now go to /etc/ansible and create a file there called tomcat-setup.yaml. You can see and copy the file from the Github repo:
--- - name: install tomcat server hosts: all become: true tasks: - name: Install OpenJDK apt: name: openjdk-11-jre-headless - name: download tomcat server packages get_url: url: http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.44/bin/apache-tomcat-9.0.44.tar.gz dest: /usr/local - name: extract tomcat packages unarchive: src: /usr/local/apache-tomcat-9.0.44.tar.gz dest: /usr/local remote_src: yes - name: start tomcat services shell: nohup /usr/local/apache-tomcat-9.0.44/bin/startup.sh
As you can see from the tomcat-setup.yaml file there are four tasks:
1) Install OpenJDK
2) Download Tomcat Server packages and puts them in the destination place.
3) Extract Tomcat packages, from source to destination place.
4) Start Tomcat service with help of startup.sh script
Start the ansible-playbook with the command:
$ sudo ansible-playbook -i hosts tomcat-setup.yaml
You will see something like this output:
After checking that everything is okay. Go to your remote host machine and verify that JAVA is installed and check if Tomcat service is running with these commands:
$ sudo java -version
And check to see if Tomcat 9 is properly downloaded and started with the command:
$ ps -ef | grep tomcat
Visit the server URL on port 8080 to test Tomcat 9 installation and configuration.
Tomcat web server:
Tomcat web application manager dashboard:
Tomcat virtual host manager dashboard:
You should now be able to deploy your application to the Tomcat 9 server running on Ubuntu 20.04 Linux machine.
In this tutorial, we learned how to install Tomcat 9 using Ansible on the remote node. This was an example of how to install a Tomcat server on a single host node from the control node. With Ansible you can install as many Tomcat servers as you want on as many host nodes as you want.