Installing Tomcat on Ubuntu
sudo apt-get update
sudo apt-get install tomcat8 -y
sudo systemctl enable tomcat8
sudo systemctl start tomcat8
- So the playbook looks as shown below
---
- hosts: appserver
become: yes
vars:
tomcat_package_name: tomcat8
pre_tasks:
- name: update ubuntu packages
apt:
update_cache: yes
tasks:
- name: install tomcat
package:
name: "{{ tomcat_package_name }}"
state: present
notify:
- enable and restart tomcat
handlers:
- name: enable and restart tomcat
service:
name: "{{ tomcat_package_name }}"
enabled: yes
state: started
- The hosts file is as shown below
[appserver]
172.31.3.192 ansible_python_interpreter="/usr/bin/python3"
- Lets add the tasks for centos
sudo yum install tomcat
- Now adding this to playbook
---
- hosts: appserver
become: yes
vars:
tomcat_package_name: tomcat
tomcat_additional_packages:
- tomcat-webapps
- tomcat-admin-webapps
pre_tasks:
- name: update ubuntu packages
apt:
update_cache: yes
when: ansible_os_family == "Debian"
tasks:
- name: install tomcat
package:
name: "{{ tomcat_package_name }}"
state: present
notify:
- enable and restart tomcat
- name: install additional packages
package:
name: "{{ item }}"
state: present
loop: "{{ tomcat_additional_packages }}"
notify:
- enable and restart tomcat
handlers:
- name: enable and restart tomcat
service:
name: "{{ tomcat_package_name }}"
enabled: yes
state: started
- Now we have to change the existing file ‘/usr/share/tomcat/conf/tomcat.conf’
JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xmx512m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC"
- Now lets go with the assumption that the values are fixed
- So lets create a file called as tomcat.conf with following
# System-wide configuration file for tomcat services
# This will be loaded by systemd as an environment file,
# so please keep the syntax. For shell expansion support
# place your custom files as /etc/tomcat/conf.d/*.conf
#
# There are 2 "classes" of startup behavior in this package.
# The old one, the default service named tomcat.service.
# The new named instances are called tomcat@instance.service.
#
# Use this file to change default values for all services.
# Change the service specific ones to affect only one service.
# For tomcat.service it's /etc/sysconfig/tomcat, for
# tomcat@instance it's /etc/sysconfig/tomcat@instance.
# This variable is used to figure out if config is loaded or not.
TOMCAT_CFG_LOADED="1"
# In new-style instances, if CATALINA_BASE isn't specified, it will
# be constructed by joining TOMCATS_BASE and NAME.
TOMCATS_BASE="/var/lib/tomcats/"
# Where your java installation lives
JAVA_HOME="/usr/lib/jvm/jre"
# Where your tomcat installation lives
CATALINA_HOME="/usr/share/tomcat"
# System-wide tmp
CATALINA_TMPDIR="/var/cache/tomcat/temp"
# You can pass some parameters to java here if you wish to
#JAVA_OPTS="-Xminf0.1 -Xmaxf0.3"
JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Djava.awt.headless=true -Xmx512m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC"
# Use JAVA_OPTS to set java.library.path for libtcnative.so
#JAVA_OPTS="-Djava.library.path=/usr/lib"
# You can change your tomcat locale here
#LANG="en_US"
# Run tomcat under the Java Security Manager
SECURITY_MANAGER="false"
# SHUTDOWN_WAIT has been deprecated. To change the shutdown wait time, set
# TimeoutStopSec in tomcat.service.
# If you wish to further customize your tomcat environment,
# put your own definitions here
# (i.e. LD_LIBRARY_PATH for some jdbc drivers)
- Now modify the playbook to copy the tomcat.conf
---
- hosts: appserver
become: yes
vars:
tomcat_package_name: tomcat
tomcat_additional_packages:
- tomcat-webapps
- tomcat-admin-webapps
tomcat_conf_location: "/usr/share/tomcat/conf/tomcat.conf"
pre_tasks:
- name: update ubuntu packages
apt:
update_cache: yes
when: ansible_os_family == "Debian"
tasks:
- name: install tomcat
package:
name: "{{ tomcat_package_name }}"
state: present
notify:
- enable and restart tomcat
- name: install additional packages
package:
name: "{{ item }}"
state: present
loop: "{{ tomcat_additional_packages }}"
notify:
- enable and restart tomcat
- name: copy tomcat configuration
copy:
src: tomcat.conf
dest: "{{ tomcat_conf_location }}"
notify:
- enable and restart tomcat
handlers:
- name: enable and restart tomcat
service:
name: "{{ tomcat_package_name }}"
enabled: yes
state: restarted
- this approach is good for copying the static files, but what should be done if the values are dynamic
- In the case of ansible dynamic nature can be handled by ansible variables, but to use ansible variables in configuration files we need to learn
- Ansible templates
- Jinja2 (Mechanism used in ansible templates.)