0..主机清单
[web]
172.16.1.[7:9]
[lb]
172.16.1.[5:6]
[backup]
172.16.1.41
[nfs_server]
172.16.1.31
[nfs_client:children]
web
backup
[zabbix]
172.16.1.72
[db]
172.16.1.51
172.16.1.101
1..剧本安装nfs服务
- hosts: nfs
vars:
IP: 172.16.1.31
Web_dir: web
Web_owner: www
tasks:
- name: 01_install_nfs_server_and_rpcbind
yum:
name: ['nfs-utils', 'rpcbind']
state: installed
- name: 02_create_backup_dir
file: dest=/data/{{ item.dir }} state=directory owner={{ item.owner }}
with_items:
- { dir: '{{ Web_dir }}',owner: '{{ Web_owner }}' }
- name: 03_edit_exprot
copy: content="/data/web 172.16.1.0/24(rw,sync,no_all_squash,root_squash,anonuid=1200,anongid=1200)" dest=/etc/exports
- name: 04_start_rpcbind_and_nfs
service: name={{ item }} state=started enabled=yes
with_items:
- rpcbind
- nfs
- hosts: web
tasks:
- name: 01_install_nfs-utils
yum: name=nfs-utils state=installed
- name: 02_create_backup_dir
file: name=/data/web state=directory
- name: 03_mount_nfs_dir
mount: src=172.16.1.31:/data/web path=/data/web fstype=nfs state=mounted
2..剧本安装rsync服务
rsync_server:
[root@m01 ansible_playbook]# vim rsync_server_install.yaml
- hosts: 172.16.1.41
tasks:
- name: 01_install_rsync_server
yum: name=rsync state=installed
- name: 02_send_rsyncd.conf
copy: src=/tmp/rsyncd.conf dest=/etc/
- name: 03_create_rsync_user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04_create_passwd-file
copy: content='rsync_backup:123456' dest=/etc/rsync.password mode=600 owner=root
- name: 05_create_backup_dir
file: dest=/backup/{{ item }} state=directory owner=rsync group=rsync
with_items:
- web
- nfs
- lb
- zabbix
- manager
- name: 06_start_rsync_server
service: name=rsyncd state=started enabled=yes
==================================================================================================
rsync_client:
[root@m01 ansible_playbook]# vim rsync_client_install.yaml
- hosts: 172.16.1.7
tasks:
- name: 01_send_rsync_passwd_file
copy: content='123456' dest=/etc/rsync.password mode=600
- name: 02_create_backup_dir
file: dest=/data/web state=directory
~
自动同步配置要点:
inotify-tools
和sersync软件安装在客户端(web服务器上,不需要启动服务)
只需配置好后,运行
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
命令即可
[root@web01 ~]# chmod +x /etc/rc.d/rc.local
PS: 将sersync2添加到$PATH中:
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/"
将上条命令添加到/etc/profile文件中
echo 'export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/"' >> /etc/profile
最好使用绝对路径:
将sersync2 -dro /usr/local/sersync/confxml.xml
添加开机自启:
echo "/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml" >> /etc/rc.local
====================================================
# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Oct 28 20:51 /etc/rc.local -> rc.d/rc.local
1)作用是,里面写的内容将作为开机启动时自动执行的命令(需要给rc.d/rc.local加上执行权限)
2)文件中的内容一定是命令信息
====================================================
网友评论