Sunday 29 May 2022

Ansible Playbook | Tomcat10 | Ubuntu 20.04 | Systemd | Port Confuguration


cat tomcat_install.yml 
---
 - name: Install Tomcat10 and Configure
   hosts: localhost
   become: yes
   vars: 
     tomcat_port: 8081

   tasks:

      - name: Update the System Packages
        apt:
          update_cache: yes

      - name: Create a Tomcat User
        user:
          name: tomcat

      - name: Create a Tomcat Group
        group:
          name: tomcat

      - name: Create a Tomcat Directory
        file:
          path: /opt/tomcat10
          owner: tomcat
          group: tomcat
          mode: 755
          recurse: yes

      - name: download tomcat server packages
        get_url:
          url: https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.21/bin/apache-tomcat-10.0.21.tar.gz
          dest: /opt/tomcat10

      - name: extract tomcat packages
        unarchive:
          src: /opt/tomcat10/apache-tomcat-10.0.21.tar.gz
          dest: /opt/tomcat10
          remote_src: yes

      - name: Configure tomcat port as 8081
        template: 
          src: server.xml.j2
          dest: /opt/tomcat10/apache-tomcat-10.0.21/conf/server.xml

      - name: Change ownership of tomcat directory
        file:
          path: /opt/tomcat10
          owner: tomcat
          group: tomcat
          mode: "u+rwx,g+rx,o=rx"
          recurse: yes
          state: directory

      - name: Copy Tomcat service from local to remote
        copy:
          src: tomcat.service.j2
          dest: /etc/systemd/system/tomcat.service
          mode: 0755
         
      - name: Start Tomcat service
        systemd:
          name: tomcat
          state: started
          daemon_reload: true

server.xml.j2
<...>
<Connector port="{{ tomcat_port }}" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<...>

 cat tomcat.service.j2 

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat10/apache-tomcat-10.0.21/temp
Environment=CATALINA_HOME=/opt/tomcat10/apache-tomcat-10.0.21
Environment=CATALINA_BASE=/opt/tomcat10/apache-tomcat-10.0.21
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat10/apache-tomcat-10.0.21/bin/startup.sh
ExecStop=/opt/tomcat10/apache-tomcat-10.0.21/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

No comments:

Post a Comment