I've been working on ansible playbook to download and start tomcat in a host.
This is my inventory host file:
[group1]
machine1 ansible_host=10.40.0.168
I have group1.yml file in my group_vars:
---
ansible_ssh_user: user
ansible_ssh_pass: pass
ansible_sudo_pass: passp
My playbook is:
---
- hosts: group1
sudo: yes
tasks:
- name: Update all packages to the latest version
apt:
upgrade: dist
- name: Download tomcat
get_url: url=http://mirrors.up.pt/pub/apache/tomcat/tomcat-9/v9.0.1/bin/apache-tomcat-9.0.1-fulldocs.tar.gz dest=/opt/apache-tomcat-9.0.1.tar.gz
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /opt/apache-tomcat-9.0.1.tar.gz
dest: /opt/
remote_src: yes
- name: Run Tomcat
shell: ./startup.sh
args:
chdir: /opt/apache-tomcat-9.0.1/bin
I try to run ./startup.sh
in /opt/apache-tomcat-9.0.1/bin
folder to start tomcat.
I run the following command:
ansible-playbook playbookname.yml
If I run ./startup.sh
in a host machine it works fine, but when I run it from the control machine I get:
PLAY [group1] **********************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [myname]
TASK [Update all packages to the latest version] ***********************************************************************
ok: [myname]
TASK [Download tomcat] *************************************************************************************************
ok: [myname]
TASK [Unarchive a file that is already on the remote machine] **********************************************************
ok: [myname]
TASK [Run Tomcat] ******************************************************************************************************
changed: [myname]
PLAY RECAP *************************************************************************************************************
myname : ok=5 changed=1 unreachable=0 failed=0
After this I try to open tomcat, but it's not running on the host.
How can I start tomcat from ansible?
You should add it as a service, eg as below:
service file: /etc/systemd/system/tomcat.service (It should be in target destination machine)
File should contain as below, (Adjust to your java environment)
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/jre
Environment=CATALINA_PID=/opt/tomcat/apache-tomcat-8.0.47/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat/apache-tomcat-8.0.47
Environment=CATALINA_BASE=/opt/tomcat/apache-tomcat-8.0.47
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/apache-tomcat-8.0.47/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
Then start the server with below systemd ansible module,
- name: enable tomcat startup
systemd:
name: tomcat
enabled: yes
state: restarted
become: true