ansible's method for bulk deployment of tomcat

  • 2020-12-18 02:00:36
  • OfStack

1.1 Build the directory structure

This operation is to install nginx+mysql+tomcat+db directory structure, you can refer to 1 under, good ~


mkdir -p /ansible/roles/{nginx,mysql,tomcat,db}/{defaults,files,handlers,meta,tasks,templates,vars}
defaults looks for the path by default tasks stores the playbooks path files holds files and script packages, and copy module files search paths templates template storage path handlers notify calls part of the playbook store path vars roles internal variable store 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    ### Automatic distribution sshkey The script   (It will be posted completely below) 
 ├ ─ ─  web.retry
 └ ─ ─  web.yml     #### Total call file 
34 directories, 11 files
[root@qtbackup ~]#

1.3 Create 1 total call file in the ansible directory


- hosts: bgo  #### This is a hosts Define a host name. If it is a single host, write the address directly 
 remote_user: root     #### Perform user 
 roles:           ##### Explain the configuration file directory, as we are installing this time tomcat So comment out the others 
 #  - nginx
 #  - mysql
  - tomcat
 #  - db

1.4 Create the tomcat installation playbook file


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

# This is to use yum Module is installed jdk You can use it if you need 
#- 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

############################## This source package is installed JDK#############################
# copy jdk to tmp directory 
- name: copy jdk-8u73-linux-x64.gz
 copy: src=jdk-8u73-linux-x64.gz dest=/tmp/jdk-8u73-linux-x64.gz
# Unpack the jdk Package to /application
- name: Extract archive jdk
 command: /bin/tar xf /tmp/jdk-8u73-linux-x64.gz -C /application
# The name 
- 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"

########################## The installation tomcat###########################################
- name: copy tomcat_tar_gz
 copy: src=apache-tomcat-8.0.29.tar.gz dest=/tmp/apache-tomcat-8.0.29.tar.gz
# Unpack the tomcat to opt directory 
- name: Extract archive
 command: /bin/tar xf /tmp/apache-tomcat-8.0.29.tar.gz -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 profile 
- name: Configure Tomcat users
 template: src=tomcat-users.xml dest=/application/tomcat/conf/
 notify: restart tomcat
# The installation tomcat . init The startup script 
- name: Install Tomcat init script
 copy: src=tomcat-initscript.sh dest=/etc/init.d/tomcat mode=0755
# open 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 === to perform the installation


cd /ansible
ansible-playbook web.yml --syntax-check # Check the grammar 
ansible-playbook web.yml # perform 

Related articles: