Ansible Role apache_tomcat
This role installs and configures an instance of Apache Tomcat. The role uses the operating system’s package manager, so EPEL is a must on RHEL. Log rotation in Tomcat is disabled and is done by logrotated. This role currently supports Tomcat v9.
Optionally this role also installs:
the Manager Web GUIs (default: install). When installed, the Manager Web GUIs are available at
http://tomcat:8080/host-manager/html: Admin Interface (Virtual Host Manager)
http://tomcat:8080/manager/html: Web Application Manager
http://tomcat:8080/manager/status: Server Status
the home page („ROOT“ web application; default: install). When installed, it is accessible at http://tomcat:8080/.
Notes:
On RHEL 8 and compatible, it installs Tomcat 9.0.65+ and Java 1.8.0+
On RHEL 9 and compatible, it installs Tomcat 9.0.87+ and OpenJDK 11.0.24+ (LTS)
If activating AJP, this role currently sets
secretRequired
tofalse
.
Multiple Tomcat Instances
How to deploy multiple Tomcat instances on a single server using this and other roles? Imagine you want to run an ‚author‘ and a ‚public‘ instance. Place your config files in host_files
(for example host_files/{{ inventory_hostname }}/var/lib/tomcats/{author,public}/conf/{context,server}.xml
and deploy like this:
files__directories__host_var:
- path: '/var/lib/tomcats/author/conf'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/author/lib'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/author/logs'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/author/webapps'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/author/work'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/public/conf'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/public/lib'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/public/logs'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/public/webapps'
owner: 'tomcat'
group: 'tomcat'
- path: '/var/lib/tomcats/public/work'
owner: 'tomcat'
group: 'tomcat'
files__files__host_var:
- path: '/var/lib/tomcats/author/conf/context.xml'
- path: '/var/lib/tomcats/author/conf/server.xml'
- path: '/var/lib/tomcats/public/conf/context.xml'
- path: '/var/lib/tomcats/public/conf/server.xml'
shell__commands__host_var:
- name: '100-prepare-tomcat-instances'
commands: |
cp --archive --no-clobber /etc/tomcat/* /var/lib/tomcats/author/conf/
cp --archive --no-clobber /etc/tomcat/* /var/lib/tomcats/public/conf/
restorecon -Fvr /var/lib/tomcats
chown -R tomcat:tomcat /var/lib/tomcats
creates: '/var/lib/tomcats/author/conf/server.xml'
- name: '110-enable-tomcat-services'
commands: |
systemctl enable --now tomcat@author.service
systemctl enable --now tomcat@public.service
If you are running multiple instances, and there is no need for the main tomcat.service
, you can simply disable it:
# we only run sub-tomcat's
apache_tomcat__service_enabled: false
apache_tomcat__service_state: 'stopped'
The deployment:
ansible-playbook --inventory=myinv linuxfabrik.lfops.apache_tomcat
ansible-playbook --inventory=myinv linuxfabrik.lfops.files
ansible-playbook --inventory=myinv linuxfabrik.lfops.shell
Mandatory Requirements
On RHEL-compatible systems, enable the EPEL repository. This can be done using the linuxfabrik.lfops.repo_epel role.
Set SELinux Booleans and Policies properly. This can be done using the linuxfabrik.lfops.selinux role.
If you use the Apache Tomcat Playbook, this is automatically done for you.
Mandatory Role Variables
Only mandatory if installing the Manager Web GUI and/or the ROOT webapp.
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager
role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access:
manager-gui
: Allows access to the HTML GUI and the status pages.manager-script
: Allows access to the text interface and the status pages.manager-jmx
: Allows access to the JMX proxy and the status pages.manager-status
: Allows access to the status pages only.
The GUI is protected against CSRF, but the text and JMX interfaces are not. To maintain CSRF protection, users with the manager-gui
role should not be given the manager-script
or manager-jmx
roles.
Variable |
Description |
---|---|
|
String. A regex that describes which IP addresses are allowed to access the documentation webapp. |
|
String. A regex that describes which IP addresses are allowed to access the manager and host-manager webapps. |
|
List of dictionaries. Users allowed to access the Manager Web GUI. Subkeys:
|
Example:
# only mandatory if installing the Manager Web GUI and/or the ROOT webapp
apache_tomcat__users__host_var:
- username: 'tomcat-admin'
password: 'linuxfabrik'
roles:
- 'admin-gui'
- 'manager-gui'
state: 'present'
apache_tomcat__webapps_docs_context_xml_allow: '|192\.2\.0\.\d+|10\.80\.32\.\d+'
apache_tomcat__webapps_manager_context_xml_allow: '|192\.2\.0\.\d+|10\.80\.32\.\d+'
Optional Role Variables
Variable |
Description |
Default Value |
---|---|---|
|
Number. Manager App. File size limit for WAR file uploads in bytes. Defaults to 50MB. |
|
|
Number. Manager App. Request limit in bytes. Defaults to 50MB. |
|
|
Number. The maximum size of the static resource cache in kilobytes. If not specified, the default value is |
|
|
Number. |
|
|
Number. |
|
|
For specifiying various JVM Options after |
|
|
Number. Log files are rotated |
|
|
List of dictionaries. Tomcat roles to deploy. Subkeys:
Built-in Tomcat manager roles are:
|
|
|
Number. The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. This is typically only useful in embedded and testing applications. Doc |
unset (not listening on AJP) |
|
String. The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is „off“ (disable compression), „on“ (allow compression, which causes text data to be compressed), „force“ (forces compression in all cases), or a numerical integer value (which is equivalent to „on“, but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to „on“ or more aggressive, the output will also be compressed. If not specified, this attribute is set to „off“. |
|
|
The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. Use a value of |
|
|
Number. The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to |
|
|
Number. The minimum number of threads always kept running. This includes both active and idle threads. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as |
|
|
The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of |
|
|
|
|
|
Bool. Enables or disables the service, analogous to |
|
|
String. Changes the state of the service, analogous to |
|
|
Bool. If set to |
|
|
Bool. If set to |
|
|
Number. Manager App. File size limit for WAR file uploads in bytes. Defaults to 50MB. |
|
|
Number. Manager App. Request limit in bytes. Defaults to 50MB. |
|
Example:
# optional
apache_tomcat__webapps_manager_web_xml_max_file_size: 209715200
apache_tomcat__webapps_manager_web_xml_max_request_size: 209715200
apache_tomcat__context_xml_cache_max_size: 102400
apache_tomcat__env_xms: '1024M'
apache_tomcat__env_xmx: '1024M'
apache_tomcat__env_xx: '+UseParallelGC'
apache_tomcat__logrotate: 7
apache_tomcat__roles__host_var:
- name: 'admin-gui'
state: 'present'
- name: 'manager-gui'
state: 'absent'
apache_tomcat__server_xml_ajp_port: 8009
apache_tomcat__server_xml_connector_compressable_mime_types: 'text/html,text/xml,text/plain'
apache_tomcat__server_xml_connector_compression: 'on'
apache_tomcat__server_xml_connector_connection_timeout: 60000
apache_tomcat__server_xml_connector_max_threads: 200
apache_tomcat__server_xml_connector_min_spare_threads: 10
apache_tomcat__server_xml_connector_port: 8080
apache_tomcat__server_xml_shutdown_port: 8005
apache_tomcat__service_enabled: true
apache_tomcat__service_state: 'started'
apache_tomcat__skip_admin_webapps: false
apache_tomcat__skip_root_webapp: false
Troubleshooting
WARN org.hibernate.engine.jdbc.internal.JdbcServicesImpl:169 - HHH000342: Could not obtain connection to query metadata : Cannot create PoolableConnectionFactory (Communications link failure`
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
chown -R root:tomcat /var/lib/tomcat/webapps/
?Database Credentials correct?
Connection string correct? Example:
jdbc:mysql://localhost/linuxfabrik?createDatabaseIfNotExist=true&useEncoding=true&characterEncoding=UTF-8
SELinux Boolean for Tomcat set?
setsebool tomcat_can_network_connect_db on