首页 > 其他分享 >实践项目-Ansible+Playbook自动化部署服务器上线

实践项目-Ansible+Playbook自动化部署服务器上线

时间:2024-12-23 19:52:18浏览次数:4  
标签:name tomcat dev nginx Ansible Playbook version mysql 服务器

(241223)
环境

系统 ip 主机名 域名
debian12.8 192.168.100.6 ansible-main ansible-main.example.com
debian12.8 192.168.100.12 ansible-node1 ansible-node1.example.com
debian12.8 192.168.100.15 ansible-node2 ansible-node2.example.com
debian12.8 192.168.100.16 ansible-node3 ansible-node3.example.com

Ansible-2.9.13
Nginx-1.26.2
MySQL-8.0.40
Tomcat-10.1.34
Java-JDK11

基本配置

sudo apt-get update && apt-get upgrade
sudo apt-get install vim

/etc/hostname

ansible-main

/etc/network/interfaces

auto lo
iface lo inet loopback

auto ens33
iface ens33 inet static
address 192.168.100.12
netmask 255.255.255.0
gateway 192.168.100.254
search localdomain
nameserver 8.8.8.8
nameserver 114.114.114.114

/etc/ssh/sshd_config

PermitRootLogin yes
PubkeyAuthentication no
ssh-keygen -t rsa -b 4096

main连通其他node

for i in main node1 node2 node3; do ssh-copy-id root@ansible-$i; done

安装Ansible

sudo apt update && apt upgrade
sudo apt -y install python3 python3-pip python3-dev libffi-dev gcc libssl-dev

wget https://releases.ansible.com/ansible/ansible-2.9.13.tar.gz
tar -zxvf ansible-2.9.13.tar.gz
cd ansible-2.9.13

python3 setup.py build
python3 setup.py install

任务结构

ansible_lnmt/
├── common/
│   ├── tasks/
│   │   └── main.yml
│   └── handlers/
│       └── main.yml
├── nginx/
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   └── nginx.conf.j2
│   └── handlers/
│       └── main.yml
├── mysql/
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   └── my.cnf.j2
│   └── handlers/
│       └── main.yml
├── tomcat/
│   ├── tasks/
│   │   └── main.yml
│   ├── templates/
│   │   └── server.xml.j2
│   └── handlers/
│       └── main.yml
├── java/
│   ├── tasks/
│   │   └── main.yml
│   └── handlers/
│       └── main.yml

inventory

[all]
ansible-main ansible_host=192.168.100.6
ansible-node1 ansible_host=192.168.100.12
ansible-node2 ansible_host=192.168.100.15
ansible-node3 ansible_host=192.168.100.16

[all:vars]
ansible_user=root
ansible_ssh_private_key_file='SSH私钥路径'

执行脚本Playbook

ansible_lnmt.yml

---
- name: Deploy LNMT Environment
  hosts: all
  become: yes
  vars_files:
    - vars/main.yml

  roles:
    - common
    - java
    - mysql
    - tomcat
    - nginx

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

    - name: restart mysql
      service:
        name: mysql
        state: restarted

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

vars/main.yml

---
mysql_root_password: "mysql_root_password"
java_version: "11"
nginx_version: "1.26.2"
mysql_version: "8.0.40"
tomcat_version: "10.1.34"

roles/common/tasks/main.yml

---
- name: Update system packages
  apt:
    update_cache: yes
    upgrade: dist

- name: Disable swap
  command: swapoff -a
  notify: update fstab to disable swap

- name: Update /etc/fstab to disable swap
  lineinfile:
    path: /etc/fstab
    regexp: '^UUID=.*swap'
    state: absent

- name: Install required packages
  apt:
    name:
      - build-essential
      - libpcre3
      - libpcre3-dev
      - zlib1g
      - zlib1g-dev
      - libssl-dev
      - libaio1
      - libaio-dev
      - libncurses5-dev
      - libncursesw5-dev
      - libreadline-dev
      - libsqlite3-dev
      - libgdbm-dev
      - libdb5.3-dev
      - libbz2-dev
      - libexpat1-dev
      - liblzma-dev
      - tk-dev
      - libffi-dev
      - wget
      - curl
      - gnupg
      - software-properties-common
    state: present

- name: Stop and disable ufw
  service:
    name: ufw
    state: stopped
    enabled: no

roles/common/handlers/main.yml

---
- name: update fstab to disable swap
  command: sed -i '/swap/d' /etc/fstab

roles/java/tasks/main.yml

---
- name: Add Oracle Java PPA
  apt_repository:
    repo: ppa:linuxuprising/java
    state: present

- name: Install Oracle Java {{ java_version }}
  apt:
    name: oracle-java{{ java_version }}-installer
    state: present

- name: Set default Java version
  apt:
    name: oracle-java{{ java_version }}-set-default
    state: present

roles/mysql/tasks/main.yml

---
- name: Download MySQL {{ mysql_version }} source
  get_url:
    url: https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-{{ mysql_version }}.tar.gz
    dest: /tmp/mysql-{{ mysql_version }}.tar.gz

- name: Extract MySQL source
  unarchive:
    src: /tmp/mysql-{{ mysql_version }}.tar.gz
    dest: /opt
    remote_src: yes

- name: Install MySQL build dependencies
  apt:
    name:
      - build-essential
      - cmake
      - libncurses5-dev
      - libssl-dev
      - libaio-dev
      - libaio1
      - libnuma-dev
      - libevent-dev
      - libjemalloc-dev
      - libtirpc-dev
      - libgflags-dev
      - liblz4-dev
      - libzstd-dev
      - liblzma-dev
      - bison
      - flex
    state: present

- name: Create MySQL build directory
  file:
    path: /opt/mysql-{{ mysql_version }}/build
    state: directory

- name: Configure MySQL
  command: >
    cmake ..
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
    -DMYSQL_DATADIR=/usr/local/mysql/data
    -DSYSCONFDIR=/etc
    -DWITH_INNOBASE_STORAGE_ENGINE=1
    -DWITH_PARTITION_STORAGE_ENGINE=1
    -DWITH_FEDERATED_STORAGE_ENGINE=1
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1
    -DWITH_MYISAM_STORAGE_ENGINE=1
    -DENABLED_LOCAL_INFILE=1
    -DENABLE_DTRACE=0
    -DDEFAULT_CHARSET=utf8mb4
    -DDEFAULT_COLLATION=utf8mb4_general_ci
    -DWITH_SSL=system
    -DWITH_ZLIB=system
    -DWITH_EMBEDDED_SERVER=1
    -DWITH_READLINE=1
    -DWITH_UNIT_TESTS=OFF
    -DINSTALL_LAYOUT=STANDALONE
    -DCMAKE_BUILD_TYPE=Release
  args:
    chdir: /opt/mysql-{{ mysql_version }}/build

- name: Compile and install MySQL
  command: >
    make -j$(nproc) && make install
  args:
    chdir: /opt/mysql-{{ mysql_version }}/build

- name: Create MySQL data directory
  file:
    path: /usr/local/mysql/data
    state: directory
    owner: mysql
    group: mysql

- name: Create MySQL user and group
  group:
    name: mysql
    state: present
  user:
    name: mysql
    shell: /bin/false
    home: /usr/local/mysql
    create_home: no
    system: yes

- name: Initialize MySQL data directory
  command: >
    /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

- name: Copy MySQL configuration file
  template:
    src: my.cnf.j2
    dest: /etc/my.cnf
  notify: restart mysql

- name: Create systemd service file for MySQL
  template:
    src: mysql.service.j2
    dest: /etc/systemd/system/mysql.service

- name: Reload systemd daemon
  systemd:
    daemon_reload: yes

- name: Ensure MySQL is started and enabled
  service:
    name: mysql
    state: started
    enabled: yes

- name: Set MySQL root password
  command: >
    /usr/local/mysql/bin/mysqladmin -u root password '{{ mysql_root_password }}'

roles/mysql/templates/my.cnf.j2

[mysqld]
bind-address = 0.0.0.0
socket = /var/run/mysqld/mysqld.sock
datadir = /usr/local/mysql/data
log-error = /var/log/mysql/error.log
pid-file = /var/run/mysqld/mysqld.pid

[client]
socket = /var/run/mysqld/mysqld.sock

roles/mysql/templates/mysql.service.j2

[Unit]
Description=MySQL Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --user=mysql
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure
PrivateTmp=true

[Install]
WantedBy=multi-user.target

roles/tomcat/tasks/main.yml

---
- name: Download Tomcat {{ tomcat_version }}
  get_url:
    url: https://downloads.apache.org/tomcat/tomcat-10/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: /tmp/apache-tomcat-{{ tomcat_version }}.tar.gz

- name: Extract Tomcat
  unarchive:
    src: /tmp/apache-tomcat-{{ tomcat_version }}.tar.gz
    dest: /opt
    remote_src: yes

- name: Create Tomcat user
  user:
    name: tomcat
    shell: /bin/false
    home: /opt/apache-tomcat-{{ tomcat_version }}

- name: Change ownership of Tomcat directory
  file:
    path: /opt/apache-tomcat-{{ tomcat_version }}
    owner: tomcat
    group: tomcat
    recurse: yes

- name: Copy Tomcat configuration file
  template:
    src: server.xml.j2
    dest: /opt/apache-tomcat-{{ tomcat_version }}/conf/server.xml

- name: Create systemd service file for Tomcat
  template:
    src: tomcat.service.j2
    dest: /etc/systemd/system/tomcat.service

- name: Reload systemd daemon
  systemd:
    daemon_reload: yes

- name: Ensure Tomcat is started and enabled
  service:
    name: tomcat
    state: started
    enabled: yes

roles/tomcat/templates/server.xml.j2

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

roles/tomcat/templates/tomcat.service.j2

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-{{ java_version }}-oracle
Environment=CATALINA_PID=/opt/apache-tomcat-{{ tomcat_version }}/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/apache-tomcat-{{ tomcat_version }}
Environment=CATALINA_BASE=/opt/apache-tomcat-{{ tomcat_version }}
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/apache-tomcat-{{ tomcat_version }}/bin/startup.sh
ExecStop=/opt/apache-tomcat-{{ tomcat_version }}/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

roles/nginx/tasks/main.yml

---
- name: Download Nginx {{ nginx_version }} source
  get_url:
    url: https://nginx.org/download/nginx-{{ nginx_version }}.tar.gz
    dest: /tmp/nginx-{{ nginx_version }}.tar.gz

- name: Extract Nginx source
  unarchive:
    src: /tmp/nginx-{{ nginx_version }}.tar.gz
    dest: /opt
    remote_src: yes

- name: Install Nginx dependencies
  apt:
    name:
      - libpcre3
      - libpcre3-dev
      - zlib1g
      - zlib1g-dev
      - libssl-dev
    state: present

- name: Configure Nginx
  command: cd /opt/nginx-{{ nginx_version }} && ./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module

- name: Compile and install Nginx
  command: cd /opt/nginx-{{ nginx_version }} && make && make install

- name: Create Nginx user
  user:
    name: nginx
    shell: /bin/false
    home: /var/lib/nginx

- name: Change ownership of Nginx directories
  file:
    path: /usr/local/nginx
    owner: nginx
    group: nginx
    recurse: yes

- name: Copy Nginx configuration file
  template:
    src: nginx.conf.j2
    dest: /usr/local/nginx/conf/nginx.conf
  notify: restart nginx

- name: Create systemd service file for Nginx
  template:
    src: nginx.service.j2
    dest: /etc/systemd/system/nginx.service

- name: Reload systemd daemon
  systemd:
    daemon_reload: yes

- name: Ensure Nginx is started and enabled
  service:
    name: nginx
    state: started
    enabled: yes

roles/nginx/templates/nginx.conf.j2

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    upstream tomcat_servers {
        server 192.168.100.12:8080;
        server 192.168.100.15:8080;
        server 192.168.100.16:8080;
    }

    server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://tomcat_servers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

roles/nginx/templates/nginx.service.j2

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

check_deployment.yml

---
- name: Check LNMT Deployment
  hosts: all
  become: yes
  tasks:
    - name: Check Nginx service status
      service_facts:

    - name: Verify Nginx is running
      assert:
        that:
          - ansible_facts.services.nginx.state == 'running'

    - name: Check MySQL service status
      service_facts:

    - name: Verify MySQL is running
      assert:
        that:
          - ansible_facts.services.mysql.state == 'running'

    - name: Check Tomcat service status
      service_facts:

    - name: Verify Tomcat is running
      assert:
        that:
          - ansible_facts.services.tomcat.state == 'running'
ansible-playbook deploy_lnmt.yml
ansible-playbook check_deployment.yml

标签:name,tomcat,dev,nginx,Ansible,Playbook,version,mysql,服务器
From: https://www.cnblogs.com/mugetsukun/p/18612492

相关文章

  • 自建Anki服务器,并用AnkiDroid连接,解决error sending request for url()或用户名或密码
    说明参考:https://www.gerenbiji.com/blog/2024年/自建Anki同步服务器/官方文档:https://docs.ankiweb.net/sync-server.html环境:RockyLinuxx86平台方案:使用官方桌面客户端自带的服务器1.下载官方客户端https://apps.ankiweb.net/2.运行命令SYNC_USER1=[用户名1]:[明......
  • 如何在服务器上克隆、pull、push GitHub私有项目
    诸神缄默不语-个人CSDN博文目录情况是这样的,我直接用gitclone命令后,会提示让我输入GitHub账号密码,我输入后它还是显示克隆失败,并显示:Cloninginto'folder_name'...Usernamefor'https://github.com':user_namePasswordfor'https://user_name@github.com':remote......
  • 哪里有200多IP站群服务器?
    寻找拥有200多个独立公网出口IP的站群服务器?地区选择香港:香港站群服务器在亚洲、欧洲和美国的使用速度都非常快,适合企业部署全球产品和服务。香港网际互联网发展迅速,是世界上重要的互联网中心枢纽节点,已经部署了直接连线大陆的cn2专线频宽。此外,香港站群服务器的频......
  • 惊!香港服务器租用托管,超乎想象的性价比之选
    香港服务器租用与托管确实以其优越的地理位置、丰富的带宽资源、以及稳定可靠的网络环境,成为了许多企业和个人用户的性价比之选。一、香港服务器租用定义与特点香港服务器租用是指用户通过支付租金,获得一台或多台服务器的使用权。租用的服务器通常包含较高品质的硬件配置,如高......
  • 香港站群服务器的卓越特性与优势
    香港站群服务器,特别是指那些坐落于香港TKO数据中心的服务器设备,是专为托管众多网站或子网站而精心设计的。这些服务器不仅功能强大,还非常适合大型网站或企业使用,因为它们能够为用户提供卓越的稳定性和访问速度。以下是对香港站群服务器几大核心优势的深入剖析:一、得天独厚的地理......
  • 我的世界服务器搭建教程 兼容Paper核心 兼容Spigot核心
    注意:该服务器是基于Paper1.20.1核心进行初始化,默认兼容spigot插件。一、配置JDK环境二、服务器核心配置三、服务器启动四、加入游戏现在搭建出来的是原版生存服务器,接下来需要进行安装各种插件,包含登录认证;经济;商店;圈地;传送;多地图等可玩性插件。具体内容请看下一章。......
  • uvicorn:ASGI服务器的极速之选
    嗨,小伙伴们好!今天我要给大家介绍一个超级好用的PythonASGI服务器——uvicorn。它就像是给你的Web应用装上了一个超级引擎,让你的应用跑得又快又稳。不管是开发环境还是生产环境,uvicorn都能帮你轻松应对高并发场景。如果你正在用FastAPI或者其他ASGI框架,uvicorn绝对是你的最佳......
  • 国标GB28181视频平台EasyCVR关于服务器端如何配置以支持更好的RTSP流传输?
    在现代视频监控系统中,RTSP流传输因其实时性和高效性被广泛应用于各种场景。然而,为了确保服务器端能够支持更好的RTSP流传输,并在Web端实现流畅播放,需要进行一系列的配置和优化。本文将详细介绍服务器端配置的最佳实践,以及如何利用EasyCVR平台解决RTSP在Web端的播放问题。一、服务......
  • CentOS7下的vsftpd服务器和客户端
    目录1、安装vsftpd服务器和ftp客户端;2、配置vsftpd服务器,允许普通用户登录、下载、上传文件;3、配置vsftpd服务器,允许anonymous用户登录、下载、上传文件;4、配置vsftpd服务器,允许root用户登录、下载、上传文件;5、只允许IP地址为192.168.98.1(Windows主机)和192.168.98.33(Linu......
  • 为何服务器会显示密码错误?
    服务器显示“密码错误”的原因可能有多种,以下是一些常见原因及相应的排查步骤,帮助你系统地解决问题:1. 确认输入信息首先,确保输入的用户名和密码完全正确。注意大小写敏感性,并检查是否有额外的空格或特殊字符。尝试使用其他已知正确的管理员账户登录,以排除输入错误的可能性。2.......