Ansible: Playbook for Tomcat 9 on Ubuntu 18.04 systemd with AWS
Ansible 2.0
In this post, we'll make a simple Ansible playbook for Tomcat9 on Ubuntu 18.04 Systemd with AWS (t2-micro type).
When we launch the instance, we may want to use UserData to have it Python installed, otherwise Ansible will complain for not finding it:
#! /bin/bash apt-get install -y python
The screenshot below is the result after running our playbook:
Besides our playbook, we need additional files (hosts for inventory and tomcat.service for /etc/systemd/system on the instance where Tomcat is installed):
hosts:
[myservers] 34.227.106.154 ansible_user=ubuntu ansible_ssh_private_key_file=/Users/kihyuckhong/.ssh/einsteinish.pem
Note that we set the "ansible_user" to "ubuntu" and used my pem key for "ansible_ssh_private_key_file".
tomcat.service:
[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64 Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC' Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom' ExecStart=/opt/tomcat/bin/startup.sh ExecStop=/opt/tomcat/bin/shutdown.sh User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
Here is our playbook (myplay.yml):
--- - hosts: all become: yes become_method: sudo remote_user: ubuntu tasks: - name: Update and upgrade apt packages apt: upgrade: yes update_cache: yes cache_valid_time: 86400 #One day - name: install JDK 11 apt: name: openjdk-11-jdk state: present - name: add tomcat group group: name: tomcat - name: add tomcat user user: name: tomcat group: tomcat home: /user/share/tomcat createhome: no - name: create /opt/tomcat directory file: path: /opt/tomcat state: directory mode: 0755 - name: download & unarchive unarchive: src: http://apache.cs.utah.edu/tomcat/tomcat-9/v9.0.16/bin/apache-tomcat-9.0.16.tar.gz dest: /opt/tomcat remote_src: yes extra_opts: [--strip-components=1] - name: Change ownership file: path: /opt/tomcat 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 dest: /etc/systemd/system/ mode: 0755 - name: Start and enable Tomcat service systemd: name: tomcat state: started enabled: true daemon_reload: true
Note that the playbook we'll be using is basic not organized via roles and not using variables at all. So, it can be used only at the start of a tomcat/ansible project.
Now, it's time to run our playbook:
$ ansible-playbook -i hosts myplay.yml PLAY [all] ************************************************************************************************************************************************************************************************* TASK [Gathering Facts] ************************************************************************************************************************************************************************************* ok: [34.227.106.154] TASK [Update and upgrade apt packages] ********************************************************************************************************************************************************************* [WARNING]: Could not find aptitude. Using apt-get instead. changed: [34.227.106.154] TASK [install JDK 11] ************************************************************************************************************************************************************************************** changed: [34.227.106.154] TASK [add tomcat group] ************************************************************************************************************************************************************************************ changed: [34.227.106.154] TASK [add tomcat user] ************************************************************************************************************************************************************************************* changed: [34.227.106.154] TASK [create /opt/tomcat directory] ************************************************************************************************************************************************************************ changed: [34.227.106.154] TASK [download & unarchive] ******************************************************************************************************************************************************************************** changed: [34.227.106.154] TASK [Change ownership] ************************************************************************************************************************************************************************************ changed: [34.227.106.154] TASK [Copy Tomcat service from local to remote] ************************************************************************************************************************************************************ changed: [34.227.106.154] TASK [Start and enable Tomcat service] ********************************************************************************************************************************************************************* changed: [34.227.106.154] PLAY RECAP ************************************************************************************************************************************************************************************************* 34.227.106.154 : ok=10 changed=4 unreachable=0 failed=0
After running our playbook, we can ssh into the instance if our tomcat is running:
$ ssh -i ~/.ssh/einsteinish.pem ubuntu@34.227.106.154 Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-1021-aws x86_64) ... ubuntu@ip-172-31-59-103:~$ ps aux|grep tomcat tomcat 31313 0.0 0.0 4628 140 ? S 17:25 0:00 /bin/sh /opt/tomcat/bin/catalina.sh start tomcat 31314 4.3 14.9 3208096 150720 ? Sl 17:25 0:03 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Xms512M -Xmx1024M -server -XX:+UseParallelGC -Dignore.endorsed.dirs= -classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat -Dcatalina.home=/opt/tomcat -Djava.io.tmpdir=/opt/tomcat/temp org.apache.catalina.startup.Bootstrap start $ sudo systemctl status tomcat ● tomcat.service - Apache Tomcat Web Application Container Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-02-08 17:25:14 UTC; 15min ago Process: 31295 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS) Main PID: 31313 (catalina.sh) Tasks: 44 (limit: 1152) CGroup: /system.slice/tomcat.service ├─31313 /bin/sh /opt/tomcat/bin/catalina.sh start └─31314 /usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava Feb 08 17:25:14 ip-172-31-59-103 systemd[1]: Starting Apache Tomcat Web Application Container... Feb 08 17:25:14 ip-172-31-59-103 startup.sh[31295]: Tomcat started. Feb 08 17:25:14 ip-172-31-59-103 systemd[1]: Started Apache Tomcat Web Application Container.
Ansible 2.0
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization