- Tomcat Ansible Galaxy
- Videos For Tomcat Ansible
- Convert Your Shell Commands As Ansible Playbook | Ansible Playbook To Install Tomcat Server
I've been working on ansible playbook to download and start tomcat in a host. This is my inventory host file: group1 machine1 ansiblehost=10.40.0.168 I have group1.yml file in my groupvars.
Apache Tomcat is often used as an application server for strictly web-based applications. It basically executes Java servlets and renders web pages that include JSP coding. Manual installation on Tomcat is time-consuming. Using Ansible we can easily automate the installation process. For ease of task, I have saved tomcat admin/manager username/password as admin/adminsecret as plaintext in the ansible role. Its not a good practice at all. Its not a good practice at all. Later, on another post I will try to implement secure credential management in ansible. Jump start your automation project with great content from the Ansible community.
Okay so say you have a custom install of tomcat and java, which is what a lot of people do because java update and tomcat updates can bring things down. So things need to be tested before updates and standard patch cycles can end up affecting the environment.
But you want to handle the startup and stopping via systemd to be able to get status outputs and let system handle the service on reboots. This is how to do it slick.
.
Ansible Setup:
Role:
Setup the new role:
.
.
.
Main.yml
Note: this will install the redhat tomcat version of tomcat. Do not worry we are not going to be using this tomcat. This is just so redhat automatically setups all the needed services and locations. We will then update the SystemD config for tomcat to use the custom version.
– name: Install the latest version of tomcat
package:
name: tomcat
state: latest
.
Note: This symlink is important as tomcat default install by redhat is inside /opt/tomcat. Update the src to the custom location of your tomcat
.
– name: Create symbolic link for 'tomcat' in /opt
file:
src: /custom/install/tomcat
path: /opt/tomcat
force: yes
state: link
.
Note: This will enable tomcat to start up on reboot
.
– name: Enable tomcat service on startup
shell: systemctl enable tomcat
.
Note: This is the tomcat systemd service file that systemd uses for the default install. We are going to empty.
.
– name: Null tomcat.service file
shell: '>/etc/systemd/system/tomcat.service'
.
Note: We are now going to add our custom block for tomcat into the tomcat.service file we just emptied above using the blockinfle module. This means that this whole section will also be managed by ansible as well. Make sure you adjust the java_home if your java isn't location inside tomcat. Along with the user,group,umask for to your custom tomcat.
.
– name: Edit tomcat.service for systemd
blockinfile:
dest: /etc/systemd/system/tomcat.service
insertafter:
block: |
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.targetnetwork.target
[Service]
Type=forking
.
Environment=JAVA_HOME=/opt/tomcat
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=/bin/kill -15 $MAINPID
.
User=tomcat
Group=tomcat
UMask=
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
.
Note: This will then reload the custom tomcat via systemd
– name: Start tomcat service with Systemd
systemd:
name: tomcat
daemon_reload: yes
.
Note: This will then check to see if the new tomcat is service running and out to the ansible playbook log.
– name: get service facts
service_facts:
.
– name: Check to see if tomcat is running
debug:
var: ansible_facts.services['tomcat.service']
.
.
Ansibe playbook log:
.
[root@nickansible]# ansible-playbook –i inventory/DEV/hosts justtomcatrole.yml –limit ‘nicktestvm‘ -k
.
SSH password:
PLAY [all] ************************************************************************************************************************************************************************************************
.
TASK [AddTomCatSystemD : Create symbolic link for 'tomcat' in /opt] ***************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Enable tomcat service on startup] ************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Null tomcat.service file] ********************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Edit tomcat.service for systemd] *************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Start tomcat service with Systemd] ***********************************************************************************************************************************************
ok: nicktestvm]
.
TASK [AddTomCatSystemD : get service facts] ***************************************************************************************************************************************************************
ok: nicktestvm]
.
TASK [AddTomCatSystemD : Check to see if tomcat is running] ***********************************************************************************************************************************************
ok: nicktestvm] => {
'ansible_facts.services['tomcat.service']': {
'name': 'tomcat.service',
'source': 'systemd',
'state': 'running',
'status': 'enabled'
}
}
.
.
Main.yml
Note: this will install the redhat tomcat version of tomcat. Do not worry we are not going to be using this tomcat. This is just so redhat automatically setups all the needed services and locations. We will then update the SystemD config for tomcat to use the custom version.
– name: Install the latest version of tomcat
package:
name: tomcat
state: latest
.
Note: This symlink is important as tomcat default install by redhat is inside /opt/tomcat. Update the src to the custom location of your tomcat
.
– name: Create symbolic link for 'tomcat' in /opt
file:
src: /custom/install/tomcat
path: /opt/tomcat
force: yes
state: link
.
Note: This will enable tomcat to start up on reboot
.
– name: Enable tomcat service on startup
shell: systemctl enable tomcat
.
Note: This is the tomcat systemd service file that systemd uses for the default install. We are going to empty.
.
– name: Null tomcat.service file
shell: '>/etc/systemd/system/tomcat.service'
.
Note: We are now going to add our custom block for tomcat into the tomcat.service file we just emptied above using the blockinfle module. This means that this whole section will also be managed by ansible as well. Make sure you adjust the java_home if your java isn't location inside tomcat. Along with the user,group,umask for to your custom tomcat.
.
– name: Edit tomcat.service for systemd
blockinfile:
dest: /etc/systemd/system/tomcat.service
insertafter:
block: |
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.targetnetwork.target
[Service]
Type=forking
.
Environment=JAVA_HOME=/opt/tomcat
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=/bin/kill -15 $MAINPID
.
User=tomcat
Group=tomcat
UMask=
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
.
Note: This will then reload the custom tomcat via systemd
– name: Start tomcat service with Systemd
systemd:
name: tomcat
daemon_reload: yes
.
Note: This will then check to see if the new tomcat is service running and out to the ansible playbook log.
– name: get service facts
service_facts:
.
– name: Check to see if tomcat is running
debug:
var: ansible_facts.services['tomcat.service']
.
.
Ansibe playbook log:
.
[root@nickansible]# ansible-playbook –i inventory/DEV/hosts justtomcatrole.yml –limit ‘nicktestvm‘ -k
.
SSH password:
PLAY [all] ************************************************************************************************************************************************************************************************
.
TASK [AddTomCatSystemD : Create symbolic link for 'tomcat' in /opt] ***************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Enable tomcat service on startup] ************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Null tomcat.service file] ********************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Edit tomcat.service for systemd] *************************************************************************************************************************************************
changed: nicktestvm]
.
TASK [AddTomCatSystemD : Start tomcat service with Systemd] ***********************************************************************************************************************************************
ok: nicktestvm]
.
TASK [AddTomCatSystemD : get service facts] ***************************************************************************************************************************************************************
ok: nicktestvm]
.
TASK [AddTomCatSystemD : Check to see if tomcat is running] ***********************************************************************************************************************************************
ok: nicktestvm] => {
'ansible_facts.services['tomcat.service']': {
'name': 'tomcat.service',
'source': 'systemd',
'state': 'running',
'status': 'enabled'
}
}
.
PLAY RECAP ************************************************************************************************************************************************************************************************
nicktestvm : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
.
.
.
[root@nicktestvm ~]# cat /etc/systemd/system/tomcat.service
# BEGIN ANSIBLE MANAGED BLOCK
[Unit]
Tomcat Ansible Galaxy
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
.
[Service]
Type=forking
.
Environment=JAVA_HOME=/opt/tomcat
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=/bin/kill -15 $MAINPID
.
User=tomcat
Group=tomcat
UMask=0028
RestartSec=10
Restart=always
.
[Install]
WantedBy=multi-user.target
# END ANSIBLE MANAGED BLOCK
.
.
SystemD Status:
.
root@nicktestvm ~]# systemctl status tomcat
● tomcat.service – Apache Tomcat Web Application Container
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-12-24 05:11:21 GMT; 21h ago
Process: 6333 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=0/SUCCESS)
Process: 6353 ExecStart=/opt/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 6363 (java)
CGroup: /system.slice/tomcat.service
└─6363 /usr/local/java/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms1…
.
Dec 24 05:11:21 nicktestvm systemd[1]: Starting Apache Tomcat Web Application Container…
Dec 24 05:11:21 nicktestvm startup.sh[6353]: Existing PID file found during start.
Dec 24 05:11:21 nicktestvm startup.sh[6353]: Removing/clearing stale PID file.
Dec 24 05:11:21 nicktestvm systemd[1]: Started Apache Tomcat Web Application Container.
.
.
# host usually use all , sometime and apply to some server role |
# but also can use ansible-playbook with --limit , more dynamicly |
# reference |
# https://gist.github.com/gomes/7697353 |
# http://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex |
# kill $(ps aux | grep '[/]home/testworkspace/qleoffice' | awk '{print $2}') |
- hosts: all |
# sudo permission |
become: true |
vars: |
tomcat_folder: qleoffice |
tomcat_path: '/home/testworkspace/{{ tomcat_folder }}' |
grep_string: '[/]home/testworkspace/{{ tomcat_folder }}' |
tasks: |
- name: > shutdown qle tomcat ,path = '{{ tomcat_path }}/bin/' |
command: sh {{ tomcat_path }}/bin/shutdown.sh |
- name: > sleep 3s |
command: sleep 3s |
- name: Check if Apache is running |
shell: ps aux | grep '{{ grep_string }}' | awk '{print $2}' |
ignore_errors: yes |
changed_when: false |
register: service_apache_status |
- debug: msg='Check if Apache is running {{ service_apache_status }}' |
- debug: msg='service_apache_status.stdout != ' = {{ service_apache_status.stdout != '}}' |
- name: kill process is tomcat is still running |
shell: kill $(ps aux | grep '{{ grep_string }}' | awk '{print $2}') |
when: service_apache_status.stdout != ' |
- command: sleep 1s |
when: service_apache_status.stdout != ' |
- name: > startup qle tomcat , path = {{ tomcat_path }}/bin/startup.sh |
shell: setsid /bin/sh -i -c '{{ tomcat_path }}/bin/startup.sh' |
projectPathName='$1' |
projectPath='/home/www/$projectPathName' |
echo$projectPath |
echo$(ps aux | grep $projectPath/ | awk '{print $2}') |
kill$(ps aux | grep $projectPath/ | awk '{print $2}') |
echo'pause 3 second' |
sleep 3s |
cd$projectPath |
sh bin/shutdown.sh && sleep 8s && sh bin/startup.sh && tail -f logs/catalina.out |
Videos For Tomcat Ansible
projectPathName='xxxx' |
sh restart_template.sh $projectPathName |