一、备份服务:playbook_rsync
1.1、定义主机资产信息:
backup nfs web -->roles_rsync
1.2、梳理回顾软件部署过程:
服务端:步骤
客户端:步骤
1.3、创建角色目录信息:
mkdir /etc/ansible/roles/roles_rsync/{tasks,files,templates,vars,handlers,meta} -p
1.4、不同角色子目录中设置配置信息
tasks/main.yaml 目录文件:
- name: 01-下载软件程序
yum:
name: rsync
state: installed
- name: 02-发送配置文件
template:
src: rsyncd.conf.tmplate
dest: /etc/rsyncd.conf
notify: rsync_restart
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 03-创建虚拟用户信息
user:
name: rsync
shell: /sbin/nologin
create_home: no
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 04-创建备份存储目录
file:
path: "{{ data_dir }}"
state: directory
owner: rsync
group: rsync
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 05-创建服务端密码文件信息
copy:
content: oldboy:123456
dest: /etc/rsync.password
mode: '0600'
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 06-创建客户端密码文件信息
copy:
content: "{{ pass_info }}"
dest: /etc/rsync.password
mode: '0600'
when: ansible_eth1.ipv4.address != "172.16.1.41"
- name: 07-启动服务程序
service:
name: rsyncd
state: started
enabled: true
when: ansible_eth1.ipv4.address == "172.16.1.41"
提前准备好rsync配置文件
templates/rsyncd.conf.tmplate
vars/main.yaml 目录文件
data_dir: /backup02
port_info: 873
handlers/main.yaml
- name: rsync_restart
service:
name: rsyncd
state: restarted
二、存储服务:
2.1、定义主机资产信息:nfs01 web
2.2、梳理回顾软件部署过程
服务端:
第一个历程:下载安装软件程序
第二个历程:分发传输配置文件
第三个历程:创建存储目录/进行授权
第四个历程:启动服务程序
客户端:
第一个历程:下载安装软件程序
第二个历程:实现挂载操作过程
第三个历程:检查是否挂载成功
2.3、创建角色目录信息:
mkdir /etc/ansible/roles/roles_nfs/{tasks,files,templates,vars,handlers,meta} -p
2.4、不同角色子目录中设置配置信息
tasks/main.yaml 目录文件信息编写
# cat roles_nfs/tasks/main.yaml
- name: 01-下载软件程序
yum:
name: nfs-utils
state: installed
- name: 02-发送配置文件
copy:
src: exports.conf.template
dest: /etc/exports
when: ansible_eth1.ipv4.address == "172.16.1.31"
notify: nfs_restart
- name: 03-创建存储目录
file:
path: "{{ item.data_info }}"
state: directory
owner: "{{ item.user_info }}"
group: "{{ item.group_info }}"
loop:
- {data_info: '/data/www', user_info: 'nfsnobody', group_info: 'nfsnobody'}
- {data_info: '/data/bbs', user_info: 'nfsnobody', group_info: 'nfsnobody'}
- {data_info: '/data/blog', user_info: 'nfsnobody', group_info: 'nfsnobody'}
when: ansible_eth1.ipv4.address == "172.16.1.31"
- name: 04-启动服务程序
service:
name: nfs
state: started
enabled: true
when: ansible_eth1.ipv4.address == "172.16.1.31"
- name: 05-创建挂载点目录
file:
path: "{{ item }}"
state: directory
loop:
- /oldboy01
- /oldboy02
- /oldboy03
when: ansible_eth1.ipv4.address != "172.16.1.31"
- name: 05-挂载存储目录
mount:
src: "172.16.1.31:{{ item.data_info }}"
path: "{{ item.mount_dir }}"
fstype: nfs
opts: defaults
state: mounted
loop:
- {data_info: '/data/www', mount_dir: '/oldboy01' }
- {data_info: '/data/bbs', mount_dir: '/oldboy02' }
- {data_info: '/data/blog', mount_dir: '/oldboy03' }
when: ansible_eth1.ipv4.address != "172.16.1.31"
#- name: 06-检查是否挂载
# shell:
# cmd: df -h|grep "{{ item }}"
# register: mount_info
# loop:
# - /oldboy01
# - /oldboy02
# - /oldboy03
# when: ansible_eth1.ipv4.address != "172.16.1.31"
#- name: 07-输出挂载信息
# debug:
# msg: "{{ mount_info.stdout_lines }}"
# when: ansible_eth1.ipv4.address != "172.16.1.31"
handlers/main.yaml 目录文件编写
- name: nfs_restart
service:
name: nfs
state: restarted
三、全网备份:playbook_backup
3.1、定义主机资产信息:backup nfs web lb zabbix
3.2、梳理回顾软件部署过程:
服务端:脚本文件信息--设置定时任务
客户端:脚本文件信息--设置定时任务
3.3、创建角色目录信息:
mkdir /etc/ansible/roles/roles_backup/{tasks,files,templates,vars,handlers,meta} -p
tasks/main.yaml 目录文件信息编写:
- name: 01-分发脚本文件信息-server
copy:
src: backup_server.sh
dest: /server/scripts/backup_server.sh
mode: 0755
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 02-分发脚本文件信息-client
copy:
src: backup_client.sh
dest: /server/scripts/backup_client.sh
mode: 0755
when: ansible_eth1.ipv4.address != "172.16.1.41"
- name: 03-编写定时任务-server
cron:
name: server_backup
minute: 0
hour: 5
job: /server/scripts/backup_server.sh &>/dev/null
when: ansible_eth1.ipv4.address == "172.16.1.41"
- name: 04-编写定时任务-client
cron:
name: client_backup
minute: 0
hour: 0
job: /server/scripts/backup_client.sh &>/dev/null
when: ansible_eth1.ipv4.address != "172.16.1.41"
files 目录文件:
files: backup_client.sh backup_server.sh
meta/main.yaml 目录文件配置
dependencies:
- role: roles_rsync
四、实时同步:
4.1、定义主机资产信息:nfs01 backup
4.2、梳理回顾软件部署过程:
nfs01 -- 部署sersync服务
· 下载软件程序
· 解压软件程序 unarchive--/usr/local
· 分发配置文件
· 修改命令权限 file
· 利用命令启动 shell /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
4.3、创建角色目录信息:
mkdir /etc/ansible/roles/roles_sersync/{tasks,files,templates,vars,handlers,meta} -p
task/main.yaml 目录文件编写:
- name: 01-解压软件程序
unarchive:
src: /server/tools/sersync.tar.gz
dest: /usr/local/
- name: 02-分发配置文件
copy:
src: confxml.xml
dest: /usr/local/sersync
- name: 03-修改文件权限
file:
path: /usr/local/sersync/sersync2
mode: 0755
- name: 04-启动实时同步服务
shell:
cmd: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
files/ sersync.tar.gz confxml.xml
meta/main.yaml 目录文件编写:
dependencies:
- role: roles_nfs
- role: roles_rsync