環境準備
主機名 |
安裝服務 |
wan |
lan |
web01 |
nfs客戶端 |
10.0.0.7 |
172.16.1.7 |
web02 |
nfs客戶端 |
10.0.0.8 |
172.16.1.8 |
nfs |
nfs服務端 |
10.0.0.9 |
172.16.1.9 |
backup |
backup服務端 |
10.0.0.41 |
172.16.1.41 |
流程分析
1.安裝ansible
2.優化ansible
3.推送公鑰
4.開啓防火牆
5.開啓80 443 873 nfs等端口和服務白名單
6.關閉selinux
7.建立同一的用戶
1.安裝nfs-utils
2.拷貝nfs配置文件
3.建立共享目錄
4.啓動nfs服務端
1.在nfs服務端安裝sersync
2.拷貝sersync配置文件到nfs服務端
3.nfs服務端配置rsync密碼文件
4.啓動sersync
主機清單
mkdir /root/ansible/nfs -p && \
vim ansible/nfs/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22
[nfs_group]
nfs ansible_ssh_host=172.16.1.31 asible_ssh_user=root ansible_ssh_port=22
[backup_group]
backup ansible_ssh_host=172.16.1.41 asible_ssh_user=root ansible_ssh_port=22
nfs配置文件
vim /root/ansible/nfs/exports
/wordpress_backup 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
sersync配置文件
[root@nfs ~]# vim /root/ansible/nfs/sersync.conf
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<!-- inotify監控的事件,true爲監控,false爲不監控 -->
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<!-- 監控的目錄和rsync服務器的IP地址,rsync的模塊名稱 -->
<localpath watch="/data">
<remote ip="172.16.1.41" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!--rsync推送的選項-->
<commonParams params="-az"/>
<!--是否開啓認證,認證模塊的用戶名,用於認證的本地密碼配置文件-->
<auth start="true" users="backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
yml
vim /root/ansible/nfs/nfs.yml
- hosts: all
tasks:
- name: Install nfs nfs-utils
yum:
name: nfs-utils
state: present
when: ansible_fqdn is match 'nfs*'
- name: Install web nfs-utils
yum:
name: nfs-utils
state: present
when: ansible_fqdn is match 'web*'
- name: content NFS Server
copy:
content: "/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)\n"
dest: /etc/exports
owner: root
group: root
mode: 0644
when: ansible_fqdn is match 'nfs*'
- name: Create data Directory
file:
path: "{{ item }}"
state: directory
owner: www
group: www
mode: 0755
recurse: yes
with_items:
- "/data"
- "/data/wordpress"
- "/data/WeCenter"
when: ansible_fqdn is match 'nfs*'
- name: wget rsync
shell: "wget http://test.driverzeng.com/other/sersync2.5.4_64bit_binary_stable_final.tar.gz"
- name: jieya sersync
unarchive:
src: /root/sersync2.5.4_64bit_binary_stable_final.tar.gz
dest: /root
copy: no
when: ansible_fqdn is match 'nfs*'
- name: gaiming
shell: "mv /root/GNU-Linux-x86 /usr/local/sersync"
when: ansible_fqdn is match 'nfs*'
ignore_errors: yes
- name: copy sersync.conf
copy:
src: /root/ansible/nfs/sersync.conf
dest: /usr/local/sersync/confxml.xml
backup: yes
when: ansible_fqdn is match 'nfs*'
- name: Start NFS Server
systemd:
name: nfs-server
state: started
enabled: yes
when: ansible_fqdn is match 'nfs*'
- name: Start NFS Server
systemd:
name: nfs-server
state: started
enabled: yes
when: ansible_fqdn is match 'web*'
- name: content NFS Server
copy:
content: "123\n"
dest: /etc/rsync.passwd
owner: root
group: root
mode: 0600
when: ansible_fqdn is match 'nfs*'
- name: start sersync
shell: /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml
when: ansible_fqdn is match 'nfs*'
- name: Mount NFS Server
mount:
path: /opt
src: 172.16.1.31:/data
fstype: nfs
opts: defaults
state: mounted
when: ansible_fqdn is match 'web*'
執行
1.執行base.yml
[root@m01 ~]# ansible-playbook ansible/base.yml
2.執行rsync.yml
[root@m01 ~]# ansible-playbook ansible/nfs/nfs.yml -i /root/ansible/nfs/hosts