Java Application deployment using Ansible - Java, Tomcat and MySQL installation & configuration management
About: This is a simple java project Deployment to a remote server using Ansible. In this demo, you can see how to install the pre-requisites and other requirements before deploying the project.
- JAVA 1.8 installation
- Tomcat web server
- MySQL 5.7 database
- Python packages
- User creation
- DB Creation
- Table creation
- Creat two roles.. one
tomcat
& othermysql
usingansible-galaxy
ansible-galaxy init tomcat
ansible-galaxy init mysql
It will create all defult folder structure.. keep the folders which you require and remove the rest for better visibility and understanding.
- Create a folder called
group_vars
in the same folder where 'Roles' folder is present, to keep the variables and vault information.
[centos@ip-172-31-24-62 ansible]$ ls -l group_vars/all
total 8
-rw-r--r--. 1 root root 99 Feb 19 18:24 vars
-rw-r--r--. 1 root root 419 Feb 19 18:25 vault
- Create a folder called
db
in the same folder where 'Roles' folder is present, to store the sql script to import whie installing the mysql
[centos@ip-172-31-24-62 ansible]$ ls -l db/
total 4
-rw-r--r--. 1 root root 402 Feb 18 18:47 devopsclass.sql
- Add the tasks into the main.yml file of the respective roles.
[centos@ip-172-31-24-62 ansible]$ ls -l roles/tomcat/tasks/
total 4
-rw-r--r--. 1 root root 1117 Feb 19 17:16 main.yml
[centos@ip-172-31-24-62 ansible]$ ls -l roles/mysql/tasks/
total 4
-rw-r--r--. 1 root root 1245 Feb 19 18:02 main.yml
- Once you setup the project run the play book (site.yml) - site.yml contains the roles which need to be executed.
ansible-playbook roles/site.yml
Note: Here we have used onle one server to install both webserver and database. I will update this further and add the DB part into a different database server.
After the successfull execution of the playbook, you can view the application by logging into it.
e.i.
And check the Databae:
[centos@ip-172-31-42-48 bin]$ mysql -u ranjit -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.47 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| devopsclass |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql>
Error 1: TASK [tomcat : Install Java] *************************************************************************************************** fatal: [finserv]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (ansible.legacy.yum) module: become, become_user. Supported parameters include: bugfix, enable_plugin, state, cacheonly, use_backend, releasever, disable_plugin, enablerepo, installroot, update_cache (expire-cache), disable_excludes, security, autoremove, download_dir, disablerepo, lock_timeout, install_weak_deps, conf_file, disable_gpg_check, skip_broken, update_only, exclude, install_repoquery, allow_downgrade, download_only, list, name (pkg), validate_certs."}
Resolution: In ansible.cfg file make the below changes
become=True
become_user=root
Error 2: TASK [mysql : Remove Test database if it exist.] ******************************************************************************* fatal: [finserv]: FAILED! => {"changed": false, "msg": "A MySQL module is required: for Python 2.7 either PyMySQL, or MySQL-python, or for Python 3.X mysqlclient or PyMySQL. Consider setting ansible_python_interpreter to use the intended Python version."}
Resolution: Set the interpreter line in ansible.cfg
interpreter_python=/usr/bin/python2