需求
- web01和web02安装nginx
- 写一个前端页面(标识+webxx_page)
- 安装nfs
- web01和web02挂在nfs的共享目录。
- 将nfs的共享目录数据推送给到backup
- 安装rsync
# 1、添加管理用户
# 2、安装rsync
- 更改配置文件
- 创建认证文件
- 创建备份目录
- 启动服务
# 3、安装nfs
- 更改配置文件
- 创建共享目录
- 启动服务
- rsync通过定时任务执行脚本推送
# 4、安装web01和web02
- 更改nginx官方源
- 配置主页
- 挂载nfs
- 启动服务
主机清单
[root@m01 ~]# vi /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[backup_group]
backup ansible_ssh_host=10.0.0.41
[nfs_group]
nfs ansible_ssh_host=10.0.0.31
[all:children]
web_group
backup_group
nfs_group
分发密钥
[root@m01 ~]# vi fenfa.sh
#!/bin/bash
ip='61 7 8 31 41'
for i in $ip;
do
sshpass -p1 ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@10.0.0.$i
done
编辑playbook
[root@m01 ~]# cat install.yml
- hosts: all
tasks:
- name: Create www group
group:
name: www
gid: 666
state: present
- name: Create www user
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: on of the firewalld
service:
name: firewalld
state: started
- name: rsync rule input firewalld
firewalld:
port: 873/tcp
permanent: no
state: enabled
- name: nfs rule input firewalld
firewalld:
service: nfs
permanent: no
state: enabled
- hosts: backup_group
tasks:
- name: install Rsync
yum:
name: rsync
state: present
- name: modify rsync conf file
copy:
src: /root/rsync/rsyncd.conf
dest: /etc/rsyncd.conf
owner: root
group: root
mode: 0644
- name: Create rsync passwd file
copy:
content: rsync_backup:123
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
- name: Create /backup directory
file:
path: /backup
owner: www
group: www
mode: 0755
state: directory
- name: Start rsync service
service:
name: rsyncd
state: started
- hosts: nfs_group
tasks:
- name: install nfs
yum:
name: nfs-utils
state: present
- name: Modify nfs-conf file
copy:
content: /data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
dest: /etc/exports
owner: root
group: root
mode: 0644
- name: Create /data directory
file:
path: /data
owner: www
group: www
mode: 0755
state: directory
- name: Start nfs service
service:
name: nfs-server
state: started
- name: BACKUP script
copy:
src: /root/nfs/backup_data.sh
dest: /root/backup_data.sh
owner: root
group: root
mode: 0644
- name: Cron task
cron:
name: "backup data"
job: "/bin/bash /root/backup_data.sh &>/dev/null"
- hosts: web_group
tasks:
- name: change nginx yum
yum_repository:
file: nginx
name: nginx
description: nginx
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
enabled: yes
- name: install nginx
yum:
name: nginx
state: present
- name: change nginx web page
copy:
content: a_{{ ansible_fqdn }}_{{ ansible_default_ipv4.address }}_page
dest: /usr/share/nginx/html/index.html
owner: root
group: root
mode: 0644
- name: mount nfs
mount:
src: 172.16.1.31:/data
path: /mnt
fstype: nfs
state: mounted
- name: start nginx
service:
name: nginx
state: started
需要的配置文件和脚本
[root@m01 ~]# vi rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
path = /backup
[root@m01 ~/nfs]# vi backup_data.sh
#!/bin/bash
export RSYNC_PASSWORD=123
rsync -az /data rsync_backup@172.16.1.41::backup