Centos7 系统使用Playbook批量部署多台LNMP环境

发布时间:2024年01月11日

使用Playbook批量部署多台LNMP环境

?配置absible源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

?yum -y install ansible

配置主机清单
定义主机名

[root@cjq11 ~]# vim /etc/ansible/hosts

[webservers]

192.168.10.12

192.168.10.13
设置ssh免密连接

[root@cjq11 ~]# yum install sshpass -y(如果安装不了,下面源码安装)

源码安装sshpass脚本

[root@cjq11 ~]# vim sshpass.sh

#!/bin/bash

#这是源码安装sshpass的脚本

get="wget http://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz"

yum_install="yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel"

app="sshpass-1.06.tar.gz"

directory="/usr/local/src/sshpass-1.06"

precompile="./configure"

if ?$get && $yum_install 1>/dev/null ; then

??????if tar -zxf $app -C /usr/local/src/ && cd $directory 1>/dev/null ; then

?????? if $precompile 1>/dev/null ; then

?????? ??if make -j 2 && make install 1>/dev/null ; then

?????? ?? echo "sshpass install ok!"

?????? ??else

?????? ?? echo "sshpass install errot!"

?????? ??fi

?????? else

?????? ??echo "cmake error!"

?????? ??exit 1

?????? fi

??????else

?????? echo "tar error!"

?????? exit 1

??????fi

else

??echo "yum error!"

??exit 1

fi

自动配置ssh连接的脚本

[root@cjq11 ~]# vim keygen.sh

#!/bin/bash

#这是自动配置ssh连接的脚本

echo "?192.168.10.12

192.168.10.13?" > ip.list ???#这个文件的IP需要改成自己的客户端IP

rpm -q sshpass &>/dev/null

[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P '' -q

export SSHPASS=123456

while read ip

do

sshpass -e ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" $ip

done<ip.list
配置playbook创建一个lnmp构建的任务
创建相关文件

[root@cjq11 ~]#?mkdir -pv /etc/ansible/lnmp/roles/{prepare,nginx,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}

构建nginx的任务
配置nginxtasks

[root@cjq11 ]# cd /etc/ansible/lnmp/roles

[root@cjq11 roles]# vim nginx/tasks/main.yml

- name: 下载nginx

??get_url:

????url: "https://nginx.org/download/nginx-1.22.0.tar.gz"

????dest: /root/

- name: 下载依赖安装包

??shell: yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel ?pcre pcre-devel

- name: 解压

??shell: tar -zxf "/root/nginx-1.22.0.tar.gz"

- name: 进入目录

??shell: cd /usr/local/src/nginx-1.22.0

- name: 预编译

??shell: ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module

??args:

????chdir: /usr/local/src/nginx-1.22.0

- name: 编译和安装

??shell: make -j 2 && make install

??args:

????chdir: /usr/local/src/nginx-1.22.0

-?name:启动nginx

?shell:?/usr/local/nginx/sbin/nginx
部署MySQL8
MySQL8.0.25下载(8.0.35安装方法相同)

官网:www.mysql.com

把安装包上传到ansible服务器

[root@cjq11 roles]# cd /etc/ansible/lnmp/roles

构建mysql的任务
配置mysqltasks

[root@cjq11 roles]# vim mysql/tasks/main.yml

- name: install the mysql

- name: 移除mariadb数据库

??shell: yum -y remove mariadb-libs.x86_64

-name: 传软件包

??copy:

????src: /root/mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar

????dest: /root/

- name: 创建mysql解压目录

??file:

????path: /usr/local/mysql/

????state: directory

- name: 解压

??shell: ?tar -xvf mysql-8.0.25-1.el7.x86_64.rpm-bundle.tar -C /usr/local/mysql/

- name: 安装MySQL客户端工具

??shell: rpm -ivh mysql-community-common-8.0.25-1.el7.x86_64.rpm --nodeps --force

??args:

????chdir: /usr/local/mysql/

- name: 安装MySQL的库文件

??shell: rpm -ivh mysql-community-libs-8.0.25-1.el7.x86_64.rpm --nodeps --force

??args:

????chdir: /usr/local/mysql/

- name: 安装MySQL服务器的主包

??shell: rpm -ivh mysql-community-client-8.0.25-1.el7.x86_64.rpm --nodeps --force

??args:

????chdir: /usr/local/mysql/

- name: 安装MySQL的公共文件

??shell: rpm -ivh mysql-community-server-8.0.25-1.el7.x86_64.rpm --nodeps --force

??args:

????chdir: /usr/local/mysql/

- name: 初始化

??shell: mysqld --initialize

- name: 改权限

??file:

????path: /var/lib/mysql/

????group: mysql

????owner: mysql

????recurse: yes

??notify: restart mysqld
构建mysql的handlers

[root@cjq11 roles]#vim mysql/handlers/main.yml

- name: restart mysqld

??service: name=mysqld?enabled=yes state=restarted
构建PHP源码安装的任务
配置phptasks

[root@cjq11 roles]#vim php/tasks/main.yml

- name: 下载oniguruma5php

??shell: wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.8-1.el7.remi.x86_64.rpm --no-check-certificate

- name: 下载oniguruma5php-devel

??shell: wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.8-1.el7.remi.x86_64.rpm --no-check-certificate

- name: 下载Libmcrypt

??shell: wget https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz --no-check-certificate

- name: 下载依赖安装包

??shell: yum -y install oniguruma5php-*

- name: 解压libmcrypt

??shell: tar -zxf "libmcrypt-2.5.8.tar.gz"

- name: 预编译libmcrypt

??shell: ./configure --prefix=/usr/local/libmcrypt-2.5.8

??args:

????chdir: /root/libmcrypt-2.5.8

- name: 编译和安装libmcrypt

??shell: make && make install

??args:

????chdir: /root/libmcrypt-2.5.8

- name: 下载php

??shell: wget https://www.php.net/distributions/php-8.1.9.tar.gz --no-check-certificate

- name: 下载依赖安装包

??shell: yum -y install php-mcrypt libmcrypt libmcrypt-devel ?autoconf ?freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c bzip2-devel libmcrypt-devel freetype-devel libjpeg-devel sqlite-devel libwebp expat-devel

- name: 解压php

??shell: tar -zxf "php-8.1.9.tar.gz" -C /usr/local/src/

- name: 预编译php

??shell:?./configure --prefix=/usr/local/php8.1.9 --enable-mysqlnd ?--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-gd --with-zlib --enable-sysvshm --enable-mbstring --with-freetype --with-expat --enable-xml --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/usr/local/php8.1.9/etc/ --with-bz2 --with-jpeg

??args:

????chdir: /root/libmcrypt-2.5.8

- name: 编译安装php

??shell: make -j 2 && make install

??args:

????chdir: /root/libmcrypt-2.5.8????????

????????????????????????????????????????????????????????????????????????????????????????????

定义整个的任务入口文件

[root@cjq11 ~]#??vim /etc/ansible/lnmp/roles/site.yml

- name: prepare build

??remote_user: root

??hosts: webservers

??roles:

????- prepare ???#要执行的roles,确保roles目录下有同名的文件夹

??tags: prepare ??#给任务打标签,方便单独测试



- name: lnmp?build mysql

??remote_user: root

??hosts: webservers

??roles:

????- mysql

??tags: mysql



- name: lnmp?build php

??remote_user: root

??hosts: webservers

??roles:

????- php

??tags: php



- name: lnmp?build nginx

??remote_user: root

??hosts: webservers

??roles:

????- nginx

??tags: nginx
根据tags标签测试服务

[root@cjq11 ~]#ansible-playbook -C /etc/ansible/lnmp/roles/site.yml -t nginx

开始部署

[root@cjq11 ~]#ansible-playbook -i /etc/ansible/hosts ?/etc/ansible/lnmp/roles/site.yml

文章来源:https://blog.csdn.net/2301_81851878/article/details/135464144
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。