How to deploy tomcat in batches with ansible

How to deploy tomcat in batches with ansible

1.1 Building the Directory Structure

This operation is to install the directory structure of nginx+mysql+tomcat+db. You can refer to it. It's good~

mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
  • defaults default search path
  • tasks stores playbooks path
  • files stores files and script packages, copy module file search path
  • templates Template storage path
  • Handlers notify calls some playbook storage paths
  • vars roles variable storage path

1.2 File Directory Structure

[root@qtbackup ~]# tree /ansible/
/ansible/
├── playbook
│ └── playbook.yml
├── roles
│ ├── db
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ ├── mysql
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ ├── nginx
│ │ ├── defaults
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ └── tomcat
│ ├── defaults
│ ├── files
│ │ ├── apache-tomcat-8.0.29.tar.gz
│ │ ├── jdk-8u73-linux-x64.gz
│ │ ├── tomcat-initscript.sh
│ │ └── tomcat-users.xml
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── tomcat-users.xml
│ └── vars
├── ssh_key.sh ###Automatically distribute sshkey script (will be posted in full below)
├── web.retry
└── web.yml ####Total number of files called: 34 directories, 11 files
[root@qtbackup ~]#

1.3 Create a general call file in the ansible directory

- hosts: bgo ####This is the host group name defined by hosts. If it is a single host, you can directly write the address remote_user: root ####Execution user roles: #####Explanation of the configuration file directory, because we are installing tomcat this time, so comment out the others # - nginx
 # -mysql
  - tomcat
 # - db

1.4 Create a tomcat installation playbook file

vim /ansible/roles/tomcat/tasks/main.yml

#This is to install jdk using the yum module. If necessary, you can use #- name: install java
# yum: name=java-1.8.0-openjdk.x86_64 state=present

===============================================================
#Create user- name: group
 group: name=tomcat
- name: user
 user: name=tomcat group=tomcat home=/usr/tomcat
 sudo: True

################################This source package installs JDK###############################
#Copy jdk to the tmp directory - name: copy jdk-8u73-linux-x64.gz
 copy: src=jdk-8u73-linux-x64.gz dest=/tmp/jdk-8u73-linux-x64.gz
#Unzip the jdk package to /application
- name: Extract archive jdk
 command: /bin/tar xf /tmp/jdk-8u73-linux-x64.gz -C /application
#Rename - name: java
 shell: mv /application/jdk1.8.0_73 /application/java
#Add environment variables - name: add /etc/profile
 lineinfile: dest=/etc/profile regexp="^JAVA_HOME=" line="JAVA_HOME=/application/java/"
- name: add /etc/profile
 lineinfile: dest=/etc/profile regexp="^CLASS_PATH=" line="CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
- name: add /etc/profile
 lineinfile: dest=/etc/profile regexp="^PATH=\$PATH:\$JAVA_HOME" line="PATH=$PATH:$JAVA_HOME/bin"
- name: add /etc/profile
 lineinfile : dest=/etc/profile regexp="^export JAVA_HOME" line="export JAVA_HOME"

############################Install tomcat##########################################
- name: copy tomcat_tar_gz
 copy: src=apache-tomcat-8.0.29.tar.gz dest=/tmp/apache-tomcat-8.0.29.tar.gz
#Unzip tomcat to the opt directory - name: Extract archive
 command: /bin/tar xf /tmp/apache-tomcat-8.0.29.tar.gz -C /opt
#Create a soft link - name: Symlink install directory
 file: src=/opt/apache-tomcat-8.0.29/ dest=/application/tomcat state=link
#Give directory permissions - name: Change ownership of Tomcat installation
 file: path=/application/tomcat/ owner=tomcat group=tomcat state=directory recurse=yes
#Push configuration file - name: Configure Tomcat users
 template: src=tomcat-users.xml dest=/application/tomcat/conf/
 notify: restart tomcat
#Install tomcat. init startup script - name: Install Tomcat init script
 copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755
#Start tomcat
- name: Start Tomcat
 service:name=tomcat state=started enabled=yes

1.5 Calling handlers via notify

- name: restart tomcat 
 service:name=tomcat state=restarted

1.6 Syntax Check === Execute Installation

cd /ansible
ansible-playbook web.yml --syntax-check #Check syntax ansible-playbook web.yml #Execute

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Ansible, a centralized management platform
  • Ansible-playbook implements a detailed tutorial on automatic deployment of KVM and installation of python3
  • How to deploy K8s cluster with Ansible
  • Environment and deployment installation of ansible management tool

<<:  Summary of special processing statements of MySQL SQL statements (must read)

>>:  Reasons and solutions for failure of dynamically added routing pages in Vue when refreshing

Recommend

Steps to use autoconf to generate Makefile and compile the project

Preface Under Linux, compilation and linking requ...

Explanation of the problem that JavaScript strict mode does not support octal

Regarding the issue that JavaScript strict mode d...

MySQL Optimization: InnoDB Optimization

Study plans are easily interrupted and difficult ...

IE8 Beta 1 has two areas that require your attention

<br />Related articles: Web skills: Multiple...

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small func...

Mysql database recovery actual record by time point

Introduction: MySQL database recovery by time poi...

About WSL configuration and modification issues in Docker

https://docs.microsoft.com/en-us/windows/wsl/wsl-...

Implementation of fastdfs+nginx cluster construction

1. Introduction to fastdfs 1. What is fastdfs Fas...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...

Summary of how to modify the root password in MySQL 5.7 and MySQL 8.0

MySQL 5.7 version: Method 1: Use the SET PASSWORD...

Detailed explanation of the process of zabbix monitoring sqlserver

Let's take a look at zabbix monitoring sqlser...

js to realize web music player

This article shares simple HTML and music player ...

How to change the encoding of MySQL database to utf8mb4

The utf8mb4 encoding is a superset of the utf8 en...