Red Hat JBoss Web Server combines a web server (Apache HTTPD), a servlet engine (Apache Tomcat), and modules for load balancing (mod_jk
and mod_cluster
). Ansible is one of the best automation tools on the market. In this article, we'll use Ansible to completely automate the deployment of a JBoss Web Server instance on a freshly installed Red Hat Enterprise Linux (RHEL) server.
Our objective is to automate a JBoss Web Server deployment through the following tasks:
- Retrieve the archive containing JBoss Web Server from a repository and install the files it contains on the system.
- Configure the Red Hat Enterprise Linux operating system (create users, groups, and the required setup files to make JBoss Web Server a systemd service).
- Fine-tune the configuration of the JBoss Web Server server itself (bind it to the appropriate interface and port).
- Deploy a web application and starting the systemd service.
- Perform a health check if the deployed application is accessible.
Ansible fully automates these operations, with no manual steps required.
Set the target environment
Before we start the automation, we need to specify our target environment. In this case, we're using RHEL 8 with Python 3.6.
# cat /etc/redhat-release
Red Hat Enterprise Linux release 8.4 (Ootpa)
# ansible --version
ansible 2.9.22
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
Note: The playbook might not work if you want to use a different Python version or target operating system.
Install the JBoss Web Server Ansible collection
Once you have RHEL 8 set up and Ansible ready to go, you need to install the JBoss Web Server Ansible collection. Ansible uses the collection to perform the following tasks on JBoss Web Server:
- Ensure required dependencies are installed (e.g., unzip).
- Install Java (if missing and requested).
- Install the binaries and integrate the software into the system (user, group, etc.).
- Deploy the configuration files.
- Start and enable JBoss Web Server as a systemd service.
- Configure a Password Vault with JBoss Web Server and a load balancer (e.g.,
mod_cluster
)—not used for this article.
Here's the installation:
$ ansible-galaxy collection install middleware_automation.jws
Process install dependency map
Starting collection install process
Installing 'middleware_automation.jws:0.0.1' to '/root/.ansible/collections/ansible_collections/middleware_automation/jws'
Installing 'middleware_automation.redhat_csp_download:1.1.2' to '/root/.ansible/collections/ansible_collections/middleware_automation/redhat_csp_download'
Note: Ansible Galaxy fetches and downloads the collection's dependencies. That is why, at the end of the execution, the JBoss Web Server collection was installed, as well as redhat_csp_download
, which will help facilitate the retrieval of the archive containing the JBoss Web Server server.
Installing the collection reduces the configuration to achieve our automation to the bare minimum.
Create a playbook to test the installation
Before we continue, let's create a minimal playbook to confirm that the collection was properly installed:
---
- name: "JBoss Web Server installation and configuration"
hosts: "all"
become: yes
collections:
– middleware_automation.jws
tasks :
In its current state, this playbook doesn’t perform any tasks on the target system. If the playbook runs successfully, then we know that the collection has been properly installed.
# ansible-playbook -i hosts min.yml
PLAY [JBoss Web Server installation and configuration] *************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [localhost]
PLAY RECAP *****************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Install the Apache Tomcat web server
Next, we'll install the Apache Tomcat web server, which consists of several steps.
Download the archive
First, let's modify our minimal playbook to retrieve the archive containing Apache Tomcat. For this purpose, we will leverage the get_url
module from Ansible:
---
- name: "JBoss Web Server installation and configuration"
hosts: "all"
become: yes
vars:
tomcat_version: 9.0.50
tomcat_download_url: https://archive.apache.org/dist/tomcat/tomcat-9/v{{tomcat_version}}/bin/apache-tomcat-{{tomcat_version}}.zip
tomcat_install_dir: /opt
tomcat_zipfile: “{{tomcat_install_dir}}/tomcat.zip"
collections:
- middleware_automation.jws
pre_tasks:
- name: "Download latest JBoss Web Server Zipfile from {{ tomcat_download_url }}."
get_url:
url: "{{ tomcat_download_url }}"
dest: "{{ tomcat_zipfile }}"
remote_src: yes
when:
- tomcat_download_url is defined
Note: The playbook downloads the archive during the pre_tasks
section. It’s important to use the role provided by the JBoss Web Server collection in the next step. This role will be executed before the tasks
block and after the pre_tasks
block, and it requires that the archive file be already present on the target system.
Execute a new playbook:
# ansible-playbook -i hosts playbook.yml
PLAY [JBoss Web Server installation and configuration] *************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [localhost]
TASK [Download latest JBoss Web Server Zipfile from https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.zip.] ***
changed: [localhost]
PLAY RECAP *****************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Install Java
JBoss Web Server is a Java-based server, so the target system is required to install a Java Virtual Machine (JVM). While Ansible primitives can perform such tasks natively, the jws
role can also take care of this part, provided the tomcat_java_version
variable is defined:
- name: "JBoss Web Server installation and configuration"
hosts: "all"
become: yes
vars:
...
tomcat_java_version: 1.8.0
collections:
– middleware_automation.jws
…
Keep in mind that this feature has limits; it works only if the target system’s distribution belongs to the Red Hat family:
$ ansible -m setup localhost | grep family
"ansible_os_family": "RedHat",
Install Java web server
For Java web server to work, we need to provide one more variable to our playbook. The tomcat_setup
(set to true
) signals to the jws
role that we want it to perform the installation:
---
- name: "JBoss Web Server installation and configuration"
hosts: "all"
become: yes
vars:
tomcat_setup: true
...
collections:
- middleware_automation.jws
roles:
- jws
pre_tasks:
...
tasks:
Run the playbook again
Let’s run our playbook again to see if it works as expected.
As you can see, quite a lot happened during this execution. Indeed, the jws
role took care of all the setup:
- Deploying a base configuration.
- Removing unused applications.
- Starting the web server.
The playbooks also perform a few tasks on the target system, like installing any required dependencies if they are missing and ensuring the environment is properly configured.
Configure JBoss Web Server as a systemd service
A nice feature of the jws
role is the included functionality to configure JBoss Web Server as a systemd service. For this to occur, you just need to define the tomcat_service_name
variable:
---
- name: "JBoss Web Server installation and configuration"
hosts: "all"
become: yes
vars:
…
tomcat_service_name: tomcat
...
collections:
- middleware_automation.jws
roles:
…
Keep in mind this only works when systemd is installed and the system belongs to the Red Hat family.
# systemctl status tomcat
● tomcat.service - JBoss Web Server Web Application Container
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-07-22 16:52:08 UTC; 16h ago
Main PID: 6234 (java)
Tasks: 37 (limit: 307)
Memory: 187.4M
CPU: 21.528s
CGroup: /system.slice/tomcat.service
└─6234 /usr/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-9.0.50/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /opt/apache-tomcat-9.0.50/bin/bootstrap.jar:/opt/apache-tomcat-9.0.50/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-9.0.50 -Dcatalina.home=/opt/apache-tomcat-9.0.50 -Djava.io.tmpdir=/opt/apache-tomcat-9.0.50/temp org.apache.catalina.startup.Bootstrap start
Jul 22 16:52:08 4414af93c931 systemd[1]: Started Apache Tomcat Web Application Container.
Jul 22 16:52:08 4414af93c931 systemd-service.sh[6220]: Tomcat started.
Jul 22 16:52:08 4414af93c931 systemd-service.sh[6219]: Tomcat runs with PID: 6234
Deploy a web application
Now that JBoss Web Server is running, let’s modify the playbook and facilitate the deployment of a web application:
tasks:
- name: " Checks that server is running"
uri:
url: "http://localhost:8080/"
status_code: 404
return_content: no
- name: "Deploy demo webapp"
get_url:
url: 'https://people.redhat.com/~rpelisse/info-1.0.war'
dest: "{{ tomcat_home }}/webapps/info.war"
notify:
- Restart Tomcat service
A handler in the jws
role restarts JBoss Web Server when the web application is downloaded. To finish our demonstration, we can add a quick test in the post_tasks
section of the playbook to confirm that the web application is functional:
post_tasks:
- name: "Sleep for {{ tomcat_sleep }} seconds to let Tomcat starts "
wait_for:
timeout: "{{ tomcat_sleep }}"
- name: "Test application"
get_url:
url: "http://localhost:8080/info/"
dest: /tmp/info.txt
Conclusion
That’s all for today! Future articles will demonstrate other features provided by the Red Hat JBoss Web Server collection, including support for mod_cluster
and securing the server with Tomcat’s Vault feature.
In the meantime, you can find the playbook used for this article in the Ansible Collection for JBoss Web Server GitHub repository.
Last updated: September 19, 2023