Defining Variables
- As we have seen variables can be defined at host level or group level in the inventory, We can also define variables in the playbook
- To understand some of the concepts of ansible we will be doing the installation of the tomcat server. Refer Here for the manual steps
sudo apt update
sudo apt install openjdk-11-jdk -y
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
cd /tmp
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
sudo tar -xf /tmp/apache-tomcat-9.0.54.tar.gz -C /opt/tomcat/
sudo ln -s /opt/tomcat/apache-tomcat-9.0.54 /opt/tomcat/latest
sudo chown -R tomcat: /opt/tomcat
sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
sudo nano /etc/systemd/system/tomcat.service
# Contents
###
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
###
sudo systemctl daemon-reload
sudo systemctl enable tomcat.service
sudo systemctl start tomcat.service
sudo systemctl status tomcat.service
-
For rest of steps Refer Here
-
In the current changeset we have installed the java package and created the user with variables defined in the playbook itself Refer Here
-
Execute syntax check and dry run
-
Now execute the playbook
-
Refer Here for the changeset containing the download and untar
-
We need to give execute permissions to all .sh files in /opt/tomcat/latest/bin. In ansible first lets get all the .sh files in the folder /opt/tomcat/latest/bin using ansible find module Refer Here and store the response ina variable
shfiles -
Refer Here for the changeset containing the changes to assign permissions to right set of files
-
Refer Here for the fix with symlink
-
Now execute the playbook
-
Now lets use the copy module to copy the service file from playbook folder to the remote node Refer Here
-
Refer Here this changeset for tomcat9 service & daemon reloads
-
Corrected few failure Refer Here this changeset and executed the ansible playbook
-
Now tried to access the tomcat page using browser
-
Exercise:
- Ensure when we execute ansible playbook to install tomcat on node where tomcat is already running the changed should be zero
- Try to make changes in this playbook to make it work on centos 7 (8)/RHEL 7 (8)
