SoFunction
Updated on 2025-03-10

Ansible method to deploy tomcat in batches

1.1 Building a directory structure

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

mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
  • defaults search paths by default
  • tasks store playbooks path
  • files store files and script packages, copy module file search path
  • templates template storage path
  • handlers notify calls part of the playbook storage path
  • Variable storage path in vars roles

1.2 File directory structure

[root@qtbackup ~]# tree /ansible/
/ansible/
├── playbook
│  └── 
├── 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.
│    │  ├── 
│    │  ├── 
│    │  └── 
│    ├── handlers
│    │  └── 
│    ├── meta
│    ├── tasks
│    │  └── 
│    ├── templates
│    │  └── 
│    └── vars
├── ssh_key.sh    ###Automatically distribute sshkey scripts (there will be completely posted below)├── 
└──      ####Total call files34 directories, 11 files
[root@qtbackup ~]#

1.3 Create a total call file in the ansible directory

- hosts: bgo  ####This is the name of a hosts that defines a host group. If it is a single host, you can write the address directly. remote_user: root     ####Execution User roles:           #####Explanation of the configuration file directory, because we installed tomcat this time, so we comment on other ones #  - nginx
 #  - mysql
  - tomcat
 #  - db

1.4 Create tomcat installation playbook file

vim  /ansible/roles/tomcat/tasks/

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

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

################################################################################################################################################################################################################################################################Copy jdk to tmp directory- name: copy 
 copy: src= dest=/tmp/
#Decompress jdk package to /application- name: Extract archive jdk
 command: /bin/tar xf /tmp/ -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"

###############################################################################################################################################################################################################################################################- name: copy tomcat_tar_gz
 copy: src=apache-tomcat-8.0. dest=/tmp/apache-tomcat-8.0.
#Extract tomcat toopt directory- name: Extract archive
 command: /bin/tar xf /tmp/apache-tomcat-8.0. -C /opt
#Create a soft connection- name: Symlink install directory
 file: src=/opt/apache-tomcat-8.0.29/ dest=/application/tomcat state=link
#Grant 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= dest=/application/tomcat/conf/
 notify: restart tomcat
#Installing tomcat.  init startup script- name: Install Tomcat init script
 copy: src= dest=/etc//tomcat mode=0755
#Enable tomcat- name: Start Tomcat
 service: name=tomcat state=started enabled=yes

1.5 Call handlers via notify

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

1.6 Syntax detection ===Execute installation

cd /ansible
ansible-playbook  --syntax-check #Check syntaxansible-playbook  #implement

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.