?配置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
[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
[root@cjq11 ~]#?mkdir -pv /etc/ansible/lnmp/roles/{prepare,nginx,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}
[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
官网:www.mysql.com
把安装包上传到ansible服务器
[root@cjq11 roles]# cd /etc/ansible/lnmp/roles
[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
[root@cjq11 roles]#vim mysql/handlers/main.yml
- name: restart mysqld
??service: name=mysqld?enabled=yes state=restarted
[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
[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