How to install Tomcat using Ansible Playbook on Ubuntu

Some time we need to automate the packages installation on multiple server, Here we are about to create Ansible Playbook to install Apache Tomcat server.

We believe, You have already installed Ansible if not you need to click on given link to install Ansible on ubuntu server.

https://www.techbeginner.in/2019/12/how-to-install-ansible-on-ubuntu.html

Step 1: Configure Ansible 

By default ansible host configuration file is /etc/ansible/hosts Here you need to add the tomcat server details like this, Use the following step for the same, In my case i have 1 apache tomcat server.

  • Group Name  – tomcat
  • IP Address     – Your IP Address
  • Username      – example
  • Password       – XXXXXXXXXX

You need to add your server in ansible host file, Syntax is given below, You need to open the /etc/ansible/hosts file with nano or vim text editor.

sudo vim /etc/ansible/hosts

and add the web server entry like this.

[tomcat]
localhost ansible_connection=ssh ansible_ssh_user=ubuntu ansible_ssh_pass=xxxxxxx

Change the credentials as per your environment, Save and exit from the vim text editor and verify the host machine connectivity with ansible by using the given command.

ansible all -m ping

Here our host configuration is completed and now we can go for next step to write play book.

Step 2: Creating Tomcat Playbook

You need to create a tomcat-playbook.yml and you change it according to your requirement.

sudo vim tomcat-playbook.yml

Paste the given code.

- hosts: tomcat
  become: yes
  tasks:

    - name: update
      apt: update_cache=yes   
      ignore_errors: yes 
   
    - name: Installating JDK.
      apt: name=default-jdk state=latest

    - name: Adding Group and user for Tomcat.
      shell: groupadd tomcat && useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
      
    - name: Installating curl.
      apt: name=curl state=latest
      
    - name: Downloading Apache Tomcat tar.
      shell: wget http://us.mirrors.quenda.co/apache/tomcat/tomcat-8/v8.5.56/bin/apache-tomcat-8.5.56.tar.gz      
      args:
        chdir: /tmp
    - name: Creating Apache Tomcat home directory.
      command: mkdir /opt/tomcat
      
    - name: Extracting Apache Tomcat.
      shell: tar -xzvf /tmp/apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
      
    - name: Updating permission.
      command: "{{ item }}"
      with_items:
        - chown -R tomcat:tomcat /opt/tomcat
        - chmod -R g+r /opt/tomcat/conf
        - chmod g+x /opt/tomcat/conf
                   
    - name: Creating service for Apache tomcat.
      file:
        path: /etc/systemd/system/tomcat.service
        state: touch
        mode: u+rwx,g-rwx,o-x

    - name: download foo.conf
      get_url:
        url: https://raw.githubusercontent.com/aftab70/Apache_Tomcat/master/tomcat_services
        dest: /etc/systemd/system/tomcat.service
 
    - name: Deamon reload.
      command: systemctl daemon-reload
 
    - name: Starting Apache Tomcat service.
      service: name=tomcat state=started

Now we are ready to use the tomcat’s PlayBook, To execute the PlayBook you need to use the given command.

ansible-playbook tomcat-playbook.yml

Conclusion

We have successfully installed Apache Tomcat using Ansible playbook on ubuntu, Kindly report in case you are facing defeculties with follwing details.

  • OS name
  • OS version
  • Package name
  • Logs – Error / Warning / failed

Get in touch with our new devops tools based website- https://www.devopstricks.in/

How to install Tomcat using Ansible Playbook on Ubuntu

2 thoughts on “How to install Tomcat using Ansible Playbook on Ubuntu

  1. Good day! This is my 1st comment here so I just wanted to give a quick shout out and say I really enjoy reading through your articles. Can you suggest any other blogs/websites/forums that cover the same subjects? Thank you so much!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top