Automation of Tomcat installation – using Ansible

Automation of Tomcat installation – using Ansible

Last Updated on April 19, 2023 by cscontents

Introduction

Tomcat is a popular open-source web server & servlet container. Web applications are deployed in the Tomcat web server.

This article will show how to automate tomcat installation using Ansible. We will be installing Tomcat 10 in the article. If you need to install any other version of tomcat then please edit the Ansible playbook accordingly.

This kind of automated installation is very demanding. Here we will be using ansible to automate the installation steps.

Briefly speaking, Ansible is an open-source IT automation tool. Please refer to the article below for a high-level quick understanding of ansible.

https://cscontents.com/introduction-to-ansible/

Prerequisite

To complete this tutorial, you need the below prerequisites.

Prerequisite Knowledge

  • Before going to the automated installation of Tomcat, you need to know the step-by-step manual; installation process. Refer to the below article for step by step guide to installing Tomcat manually.

https://cscontents.com/how-to-install-apache-tomcat-on-ubuntu/

  • Hands-on experience in using Ansible.
  • Knowledge of basic Linux commands.
  • Before starting the automation process using ansible we would suggest please go through the below article to get a basic understanding of automation using Ansible.

https://cscontents.com/basics-of-automation-using-ansible/

Prerequisite set up

You would need two machines.

  • One machine will work as an ansible host. Ansible should be installed on this machine.
  • Another machine will be your target, where tomcat will be installed using Ansible. On this machine, you need to have a user which has sudo access & this user will be used by Ansible to perform the tasks.
  • Network port 22 should be open between these two machines since ansible uses port 22 to communicate with the remote/target machine.

Automation of Tomcat installation

Before starting the automation, let’s have a look at the below diagram which explains the flow of the automation process by ansible.

Tomcat installation using ansible

Tomcat requires Java runtime environment. So, we need to ensure Java is installed on the target machine.

There are mainly two tasks that need to be automated.

  • Automated installation of JDK
  • Automated installation of Tomcat

Automated Installation of JDK using ansible

To automate the installation of JDK please follow the below-detailed article.

https://cscontents.com/automation-of-java-installation-using-ansible/

From the above JDK installation please note down the JAVA_HOME path it will be required in the ansible playbook which will install Tomcat.

Automated installation of Tomcat using ansible

Ansible is a very powerful automation tool, it provides a module for each task. We can use those modules to accomplish our automation.

Below are the ansible modules which will be used in the playbook.

Ansible playbook for tomcat installation

Create a yaml file and enter the below content. For example, in our case the file name is tomcat-install.yml

- name: Tomcat 10 installation
  hosts: tomcat-host
  become: true
  tasks:
    - name: Download tomcat 10 binaries
      get_url:
         url: https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.7/bin/apache-tomcat-10.1.7.tar.gz
         dest: /opt
    - name: Extract the binaries
      unarchive:
        src: /opt/apache-tomcat-10.1.7.tar.gz
        dest: /opt
        remote_src: yes

    - name: Rename the apache-tomcat-10.1.7 directory
      command:
        chdir: /opt
        cmd: mv apache-tomcat-10.1.7 tomcat

    - name: Create tomcat group
      group:
        name: tomcat
        system: yes
        state: present

    - name: Create tomcat user
      user:
        name: tomcat
        group: tomcat
        password: pa$$w0rd
        home: /opt/tomcat
        shell: "/bin/bash"

    - name: Change owner & group of tomcat directory
      file:
        path: /opt/tomcat
        owner: tomcat
        group: tomcat
        recurse: yes

    - name: Create empty Tomcat service file /etc/systemd/system/tomcat.service
      file:
        path: /etc/systemd/system/tomcat.service
        state: touch

    - name: Add tomcat service details in /etc/systemd/system/tomcat.service
      blockinfile:
        path: /etc/systemd/system/tomcat.service
        block: |
          [Unit]
          Description=Tomcat 10
          After=network.target
          [Service]
          Type=forking
          User=tomcat
          Group=tomcat
          Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
          Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
          Environment="CATALINA_BASE=/opt/tomcat"
          Environment="CATALINA_HOME=/opt/tomcat"
          Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
          Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
          ExecStart=/opt/tomcat/bin/startup.sh
          ExecStop=/opt/tomcat/bin/shutdown.sh
          [Install]
          WantedBy=multi-user.target
    - name: Start tomcat Service
      service:
        name: tomcat
        state: started
        enabled: yes
Inventory file

To execute the above playbook, we need an inventory file, where we need to pass the target/remote server details. Let’s name the file inventory.txt.

tomcat-host ansible_host=28.65.172.123 ansible_user=ubuntu ansible_ssh_pass=pa$$w0rd

In the above inventory.txt file,

  1. Replace the IP with your target/remote machine’s IP.
  2. Replace the value of ansible_user with your target/remote machine’s user which will be used by ansible to execute the tasks. This user must have sudo privileges.
  3. Replace the value of ansible_ssh_pass with the correct password of the user (which is mentioned in point number 2)

Below are the files present in our ansible host machine.

ubuntu@ubuntu01:~/tomcat-install$ tree
.
├── inventory.txt
└── tomcat-install.yml
0 directories, 2 files
Ansible playbook execution

To execute the above playbook, we need to run the below command.

ansible-playbook tomcat-install.yml -i inventory.txt --extra-vars "ansible_sudo_pass=sudo_password_of_user_mentioned_in_inventory_file" -vv

In the above command, enter the sudo password of the user which is mentioned in the inventory.txt file.

OR

ansible-playbook tomcat-install.yml -i inventory.txt --ask-become-pass -vv

when you execute the above command it will ask for the sudo password, and you need to enter the sudo password.

Conclusion

In this article, we have discussed the automation of Tomcat web server installation. Here we are installing Tomcat 10, but you do the same for any other version of Tomcat. And you can easily learn the method or principle of doing such automation and apply it to other software installations. It is suggested to go through the ansible modules first before implementing it.

 

Thank You.

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.