文章目录
1.部署LNMP的脚本2.部署ansible的脚本3.使用ansible批量部署lnmp环境
1.部署LNMP的脚本
#! /bin/bash
if [ ! -f
"/etc/yum.repos.d/epel.repo" ];then
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
cd /usr/local/src/
tar -zxf nginx-1.19.0.tar.gz
cd nginx-1.19.0/
useradd nginx -s /sbin/nologin -M
yum -y
install gcc gcc-c++
make pcre-devel openssl-devel
./configure \
--prefix
=/usr/local/nginx \
--user
=nginx \
--group
=nginx \
--with-stream \
--with-http_stub_status_module \
--with-http_ssl_module
make && make install
cat > /lib/systemd/system/nginx.service
<<-EOF
[Unit
]
Description
=nginx
service
After
=network.target
[Service
]
Type
=forking
ExecStart
=/usr/local/nginx/sbin/nginx
ExecReload
=/usr/local/nginx/sbin/nginx -s reload
ExecStop
=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp
=true
[Install
]
WantedBy
=multi-user.target
EOF
systemctl start nginx
systemctl
enable nginx
yum -y
install php php-fpm php-mysql mariadb-devel
systemctl start php-fpm
systemctl
enable php-fpm
yum -y
install mariadb mariadb-server mariadb-devel
systemctl start mariadb
systemctl
enable mariadb
cd /usr/local/nginx/conf/
sed -i
'45i\ index index.php index.html index.htm;' nginx.conf
sed -i
'46d' nginx.conf
sed -i
'65,71s/#//' nginx.conf
sed -i
'69s/^/#/' nginx.conf
sed -i
'70s/_params/.conf/' nginx.conf
systemctl restart nginx
2.部署ansible的脚本
#! /bin/bash
if [ ! -f
"/etc/yum.repos.d/epel.repo" ];then
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
yum -y
install ansible
cat >> /etc/hosts
<<-EOF
192.168.248.133 vm2
192.168.248.134 vm3
192.168.248.135 vm4
192.168.248.136 vm5
192.168.248.137 vm6
192.168.248.138 vm7
EOF
ssh-keygen -f /root/.ssh/id_rsa -N
''
for i
in vm2 vm3 vm4 vm5 vm6 vm7
do
ssh-copy-id root@
$i
done
mkdir -p /root/ansible/
{roles,playbooks
}
cat >> /root/ansible/ansible.cfg
<<-EOF
[defaults
]
inventory
= /root/ansible/hosts
roles_path
= /root/ansible/roles
EOF
cat >> /root/ansible/hosts
<<-EOF
[webserver
]
vm4
vm5
EOF
3.使用ansible批量部署lnmp环境
#! /bin/bash
mkdir -p /root/ansible/roles/lnmp/tasks
mkdir -p /root/ansible/roles/lnmp/files
cat >> /root/ansible/roles/lnmp/tasks/main.yml
<<-EOF
---
- name: 拷贝nginx安装包
copy:
src: nginx-1.19.0.tar.gz
dest: /usr/local/src/
- name: 执行脚本
script:
creates
=/usr/local/nginx/conf/nginx.conf
/root/scripts/lnmp_install.sh
EOF
cd ~/ansible/roles/lnmp/files
rz
cat >> /root/ansible/playbooks/lnmp.yml
<<-EOF
---
- hosts: webserver
roles:
- lnmp
EOF
cd /root/ansible
ansible-playbook /root/ansible/playbooks/lnmp.yml