首页 > 技术文章 > 第六章 Ansible+NFS搭建作业平台

jhno1 2020-10-27 15:12 原文

一、需求

#搭建LNP交作业的页面
1.安装nginx
2.控制端配置nginx推送配置
3.安装php
4.控制端配置php推送配置
5.启动php
6.部署代码
7.启动nginx
8.配置NFS服务端
9.客户端挂载

二、环境准备

主机名 IP 身份
m01 10.0.0.61、172.16.1.61 跳板机
web01 172.16.1.7 web服务器
web02 172.16.1.7 web服务器
nfs 172.16.1.31 nfs服务器

三、编写脚本

#!/bin/bash
#安装wget
 yum -y  install  wget &&\
#下载源码包
wget -O /root/php.tar.gz http://116.62.138.182/package/php.tar.gz &&\
wget -O /root/kaoshi.zip http://116.62.138.182/package/kaoshi.zip &&\
#安装ansible
yum -y install ansible &&\
#配置ansible
sed -ir 's#\#host_key_checking = False#host_key_checking = False#g' /etc/ansible/ansible.cfg &&\
sed -ir 's#\#log_path = /var/log/ansible.log#log_path = /var/log/ansible.log#g' /etc/ansible/ansible.cfg  &&\
#配置本地hosts
echo -e '172.16.1.7 web01\n172.16.1.8 web02\n172.16.1.31 nfs' >>/etc/hosts &&\
#创建目录
mkdir  /package &&\
mv /root/kaoshi.zip /root/php.tar.gz /package &&\
#配置主机清单
echo -e "[web_group]\nweb01 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root'\nweb02 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root'\n\n[nfs_group]\nnfs ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='root'" >/etc/ansible/hosts  &&\
#配置本地yum源
echo -e '[nginx-stable]\nname=nginx stable repo\nbaseurl=http://nginx.org/packages/centos/$releasever/$basearch/\ngpgcheck=1\nenabled=1\ngpgkey=https://nginx.org/keys/nginx_signing.key\nmodule_hotfixes=true' > /etc/yum.repos.d/nginx.repo &&\
#配置web服务器yum源
ansible 'web_group' -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo' &&\
#安装依赖包
ansible 'web_group' -m yum -a 'name=gcc,gcc-c++,autoconf,pcre,pcre-devel,make,automake,wget,httpd-tools,vim,tree state=present' &&\
#安装nginx
ansible 'web_group' -m yum -a 'name=nginx state=present' &&\
#配置nginx
ansible 'web_group' -m shell -a "sed -ir '2s#nginx#www#g' /etc/nginx/nginx.conf " &&\
ansible 'web_group' -m shell -a "sed -ir '3s#1#auto#g' /etc/nginx/nginx.conf " &&\
ansible 'web_group' -m shell -a "sed  -i  '25aclient_max_body_size 200m;' /etc/nginx/nginx.conf" &&\
#配置本地nginx文件
echo -e 'server{\n    listen 80;\n    server_name linux.zuoye.com;\n    root /code/zuoye;\nlocation / {\n    index index.html;\n}\nlocation ~* \.php$ {\n   fastcgi_pass 127.0.0.1:9000;\n   fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;\n   include fastcgi_params; \n}\n}' >/root/linux.zuoye.com.conf &&\
#推送nginx配置文件
ansible 'web_group' -m copy -a 'src=/root/linux.zuoye.com.conf dest=/etc/nginx/conf.d/' &&\
#创建用户组和用户
ansible 'web_group' -m group -a 'name=www state=present gid=666' &&\
ansible 'web_group' -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present' &&\
#创建目录
ansible 'web_group' -m file -a 'path=/code/zuoye/upload state=directory owner=www group=www mode=755 recurse=yes' &&\
#推送作业平台
ansible 'web_group' -m unarchive -a 'src=/package/kaoshi.zip dest=/code/zuoye/' &&\
#授权目录
ansible 'web_group' -m file -a 'path=/code/ owner=www group=www mode=755 recurse=yes' &&\
#创建php目录
ansible 'web_group' -m file -a 'path=/php state=directory ' &&\
#推送PHP
ansible 'web_group' -m unarchive -a 'src=/package/php.tar.gz dest=/php/' &&\
#本地安装PHP
ansible 'web_group' -m shell -a ' yum -y localinstall /php/*.rpm' &&\
#配置PHP
ansible 'web_group' -m shell -a "sed -ir '8s#apache#www#g' /etc/php-fpm.d/www.conf " &&\
ansible 'web_group' -m shell -a "sed -ir '10s#apache#www#g' /etc/php-fpm.d/www.conf " &&\
ansible 'web_group' -m shell -a "sed -ir 's#post_max_size = 8M#post_max_size = 200M#g' /etc/php.ini " &&\
ansible 'web_group' -m shell -a "sed -ir 's#upload_max_filesize = 2M#upload_max_filesize = 200M#g' /etc/php.ini " &&\
ansible 'web_group' -m shell -a "sed -ir '9s#/var/www/html/upload#/code/zuoye/upload#g' /code/zuoye/upload_file.php " &&\
#启动服务
ansible 'web_group' -m systemd -a 'name=nginx state=started' &&\
#启动PHP
ansible 'web_group' -m systemd -a 'name=php-fpm state=started' &&\ 

#安装nfs
ansible 'web_group' -m yum -a 'name=nfs-utils state=present' &&\
ansible 'nfs_group' -m yum -a 'name=nfs-utils state=present' &&\
#配置nfs服务端
ansible nfs -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&\
#创建用户
ansible 'nfs_group' -m group -a 'name=www gid=666 state=present ' &&\
ansible 'nfs_group' -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present' &&\
#创建目录并授权
ansible 'nfs_group' -m file -a 'path=/data state=directory owner=www group=www mode=755 recurse=yes' &&\
#启动nfs
ansible 'nfs_group' -m systemd -a 'name=nfs state=started' &&\
ansible  'web_group' -m systemd -a 'name=nfs state=started' &&\ 
#客户端挂载目录
ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/code/zuoye/upload fstype=nfs state=mounted'

推荐阅读