Ansible Roles一键搭建 LNMP+Sersync+Rsync
一、ansible重构playbook
| 环境准备
主机 | 内网IP | 外网IP | 身份 | 条件 |
---|---|---|---|---|
web01 | 172.16.1.7 | web服务器 | 关闭selinux和防火墙 | |
web02 | 172.16.1.8 | web服务器 | - | |
backup | 172.16.1.41 | rsync服务器 | - | |
nfs | 172.16.1.31 | nfs服务器 | - | |
lb01 | 172.16.1.5 | 192.168.15.5 | 负载均衡 | - |
lb02 | 172.16.1.6 | 192.168.15.6 | 负载均衡 | - |
db01 | 172.16.1.51 | 数据库 | - | |
m01 | 172.16.1.61 | 跳板机 |
1.配置主机清单
[root@m01 roles]# cat /etc/ansible/hosts
[web_group]
web01 ansible_ssh_pass='123'
web02 ansible_ssh_pass='123'
[nfs_group]
nfs ansible_ssh_pass='123'
[slb]
lb01 ansible_ssh_pass='123 '
lb02 ansible_ssh_pass='123 '
[db_group]
db01 ansible_ssh_pass='123'
[backup_group]
backup ansible_ssh_pass='123'
[nginx_group:children]
web_group
slb
[nfs_server:children]
web_group
nfs_group
[nginx_group:vars]
web=host_vars
2.配置hosts
[root@m01 roles]# vim /etc/hosts
172.16.1.107 web01
172.16.1.108 web02
172.16.1.131 nfs
172.16.1.141 backup
172.16.1.151 db01
172.16.1.105 lb01
172.16.1.106 lb02
3.创建所有目录
[root@m01 roles]# ansible-galaxy init nginx # 依次创建以下目录
- Role nginx was created successfully
[root@m01 roles]# ll
total 0
drwxr-xr-x 10 root root 154 May 26 23:11 base
drwxr-xr-x 10 root root 154 May 27 20:36 database
drwxr-xr-x 10 root root 154 May 29 15:31 discuz
drwxr-xr-x 10 root root 154 May 27 21:59 keepalived
drwxr-xr-x 10 root root 154 May 25 23:56 mariadb
drwxr-xr-x 10 root root 154 May 29 18:04 nfs
drwxr-xr-x 10 root root 154 May 27 00:21 nginx
drwxr-xr-x 10 root root 154 May 25 23:56 php
drwxr-xr-x 10 root root 154 May 29 15:53 rsync
drwxr-xr-x 10 root root 154 May 27 21:44 slb
4.剧本依次编写
# 优化部分剧本准备文件
[root@m01 files]# pwd
/project/roles/base/files
[root@m01 files]# ll
total 12
-rw-r--r-- 1 root root 2523 May 26 23:13 Cen