Ansible部署rsync,ansiblersync



 一  环境准备

2017年9月9日 21:35:37  seventeenwen

1.1 主机名和主机IP地址规划表


 主机名 IP地址规划
m01  10.0.0.61    172.16.1.61
web01                                          10.0.0.8      172.16.1.8
web02                                          10.0.0.7      172.16.1.7 
nfs                                          10.0.0.31    172.16.1.31 
backup                                          10.0.0.41    172.16.1.41

1.2 通过密钥分发脚本发送给各个受控端

分发脚本: 脚本执行结果 1.3 编辑Ansible的hosts文件
1.4 通过Ansible的测试模块测试

二  开始编写角色

2.1 编写common角色 在每一台服务器上都有一些共有的特性,这些特性可以通过Ansible的common角色来批量的定制化。例如创建同一的目录结构,共有的目录结构
目录名 目录功能
/server/scripts 存放要运行的脚本
/tools 存放主机要安装的工具
common角色的yml文件         在Ansible的配置文件中可以通过使用with_items:来指定循环,学名叫做迭代(with_items),不但可以通过with_items来使用批量创建目录还可以批量安装软件包。 执行结果:

2.2 编写rsync角色

rsync的部署流程:
Rsync备份服务部署实践
服务器类型 部署序号 部署步骤说明 Rsync服务端部署 备注说明
服务端 第一步 检查软件是否安装 # 查看软件是否安装
rpm -qa rsync
rpm -qa | grep rsync
# 查看软件安装了哪些信息
rpm -ql rsync
rpm -qf `which rsync`
  第二步 进行服务软件安装 N/A 默认rsync软件已经安装,若没有安装,执行以下命令
yum install rsync -y
  第三步 编写服务配置文件 # 编写rsync配置文件
vim /etc/rsyncd.conf (man rsyncd.conf)
默认配置文件不存在
配置文件进行修改后,需要对服务进行重启
  第四步 添加目录管理用户 # 创建备份目录管理用户
useradd rsync -s /sbin/nologin -M
根据配置文件设置进行管理用户创建
创建的用户为虚拟用户
  第五步 创建程序所需目录 # 创建数据备份目录
mkdir /backup
# 数据目录进行授权
chown rsync.rsync /backup
创建程序目录需要确认相应授权信息
  第六步 进行安全相关配置 # 创建认证文件信息
echo "rsync_backup:123456" >/etc/rsync.password
# 修改认证文件权限
chmod 600 /etc/rsync.password
创建涉及密钥信息文件均要进行授权,提升文件信息安全
  第七步 启停程序服务进程 # 启动rsync程序守护进程
rsync --daemon
# 停止rsync程序守护进程
killall rsync
rsync服务采用输入命令方式启动守护进程
rsync服务采用kill killalll pkill等方式进行停止
rsync服务启动脚本可以进行shell编写或使用xinetd服务
  第八步 检查服务是否启动 # 检查服务进程是否存在
ps -ef | grep rsync
# 检查服务进程端口信息
netstat -lntup | grep rsync
rsync软件服务的默认端口号为873
服务器类型 部署序号 部署步骤说明 Rsync客户端部署 备注说明
客户端 第一步 检查软件是否安装 # 查看软件是否安装
rpm -qa rsync
rpm -qa | grep rsyn
# 查看软件安装了哪些信息
rpm -ql rsync
# 查看某个命令如何安装的
rpm -qf `which rsync`
  第二步 进行服务软件安装 N/A 默认rsync软件已经安装,若没有安装,执行以下命令
yum install rsync -y
  第三步 进行安全相关配置 # 创建认证文件信息
echo "123456" >/etc/rsync.password
# 修改认证文件权限
chmod 600 /etc/rsync.password
创建涉及密钥信息文件均要进行授权,提升文件信息安全
  第四步 进行服务应用测试 # 确认备份数据传输
rysnc -avzP /etc/hosts rsync_backup@172.16.1.41::backup
# 确认备份数据传输,采用免密钥进行传输
rysnc -avzP /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
属于软件命令参数信息
编写rsync角色 config_rsyncd.yml install.yml main.yml 模板文件 rsyncd.conf.j2的内容 rsync.password.j2 配置变量。
编写入口文件site.yml
#!/bin/bash
#判断参数是否是0个
if [ $# -eq 0 ];then
    echo “please input ip address: ”
    exit 1
fi
#加载系统函数
. /etc/init.d/functions
#将ip地址放入到数组中
ipArray=($*)
#判断原先是否已经有密钥文件,如果有删除重新生成
if [ -e /root/.ssh/id_dsa ];then
    rm -f /root/.ssh/id_dsa*
fi
#生成密钥对, -f 指定存放的位置
ssh-keygen -t dsa -f /root/.ssh/id_dsa  -P ""  &>/dev/null
#循环分发公钥
for ip in  ${ipArray[*]}
do
sshpass -p123456  ssh-copy-id  -i  /root/.ssh/id_dsa.pub  "-o StrictHostKeyChecking=no  $ip"  &>/dev/n
ull
    if [ $? -eq 0 ];then
        action "$ip  "    /bin/true
    else
        action "$ip "     /bin/false
    fi
done

 
[root@m01 /]# cat /etc/ansible/hosts 
[nfs]
172.16.1.31
[web]
172.16.1.[7:8]
[backup]
172.16.1.41
 
[root@m01 /]# ansible all -m ping
172.16.1.41 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.8 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.7 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.16.1.31 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.1.31 port 22: Connection timed out\r\n", 
    "unreachable": true
}
 
[root@m01 tasks]# pwd 
/etc/ansible/roles/common/tasks
[root@m01 tasks]# cat main.yml 
---
# Author: seventeenwen
# Date: 2017/09/09 23:11
# Description: common role
- name: mkdir common directory
  file: path={{ item }} state=directory
  with_items:
    - /server/scripts/
    - /tools
    - /beifen
    
 
[root@m01 ansible]# ansible-playbook site.yml 
PLAY [all] *******************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.41]
ok: [172.16.1.8]
ok: [172.16.1.31]
TASK [common : mkdir common directory] ***************************************************************
ok: [172.16.1.31] => (item=/server/scripts/)
ok: [172.16.1.41] => (item=/server/scripts/)
ok: [172.16.1.7] => (item=/server/scripts/)
ok: [172.16.1.8] => (item=/server/scripts/)
ok: [172.16.1.31] => (item=/tools)
ok: [172.16.1.41] => (item=/tools)
ok: [172.16.1.8] => (item=/tools)
ok: [172.16.1.7] => (item=/tools)
ok: [172.16.1.31] => (item=/beifen)
ok: [172.16.1.41] => (item=/beifen)
ok: [172.16.1.8] => (item=/beifen)
ok: [172.16.1.7] => (item=/beifen)
PLAY RECAP *******************************************************************************************
172.16.1.31                : ok=2    changed=0    unreachable=0    failed=0   
172.16.1.41                : ok=2    changed=0    unreachable=0    failed=0   
172.16.1.7                 : ok=2    changed=0    unreachable=0    failed=0   
172.16.1.8                 : ok=2    changed=0    unreachable=0    failed=0   
#同过ansible批量查看创建结果
[root@m01 ansible]# ansible all  -a "ls -ld /beifen /tools /server/scripts"
172.16.1.8 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 4096 Sep  7 21:46 /beifen
drwxr-xr-x 2 root root 4096 Sep  6 10:28 /server/scripts
drwxr-xr-x 2 root root 4096 Aug 29 12:03 /tools
172.16.1.7 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 4096 Sep  9 21:03 /beifen
drwxr-xr-x 2 root root 4096 Aug 29 11:53 /server/scripts
drwxr-xr-x 2 root root 4096 Aug 29 12:03 /tools
172.16.1.41 | SUCCESS | rc=0 >>
drwxr-xr-x 3 root root 4096 Aug 30 11:35 /beifen
drwxr-xr-x 2 root root 4096 Aug 30 19:51 /server/scripts
drwxr-xr-x 2 root root 4096 Aug 29 12:03 /tools
172.16.1.31 | SUCCESS | rc=0 >>
drwxr-xr-x 2 root root 4096 Aug 26 18:46 /beifen
drwxr-xr-x 2 root root 4096 Sep  4 18:52 /server/scripts
drwxr-xr-x 2 root root 4096 Aug 29 12:03 /tools
 
[root@m01 rsync]# tree
.
├── tasks
│   ├── config_rsyncd.yml
│   ├── install.yml
│   └── main.yml
└── templates
    ├── rsyncd.conf.j2
    └── rsync.password.j2
2 directories, 5 files
---
# Author: seventeenwen
# Date: 2017/09/10  8:54
# Description: config rysncd
- name: copy rsyncd.conf
  template: dest=/etc/rsyncd.conf src=rsyncd.conf.j2 own
er=rsync group=rsync
- name: mkdir dir
  file: path={{ path }} group=rsync owner=rsync state=di
rectory
- name: touch secretsfile
  template: dest=/etc/rsync.password mode=600 src=rsync.
password.j2
- name: start rsync
  shell: rsync --daemon

 
- name: install rsync
  yum: name=rsync state=installed
- name: useradd rsync
  user: name=rsync uid=801 shell=/sbin/nologin createhome=no
 
- include: install.yml
- include: config_rsyncd.yml                               
 
#rsyncd conf
uid=rsync
gid=rsync
use chroot=no
max connections=200
timeout=300
lock file =/var/run/rsync.lock
log file =/var/log/rsyncd.log
pid file =/var/run/rsyncd.pid
ignore errors
read only =false
list =false
hosts allow ={{ hosts_allow }}
auth users ={{ auth_users }}
secrets file ={{ secrets_file }}
[{{ model }}]
comment = "backup dir by oldboy"
path = {{ path }}
 
{{auth_users}:{{password}}
 
[root@m01 group_vars]# vim  backup 
hosts_allow: 172.16.1.0/24
auth_users: rsync_backup
secrets_file: /etc/rsync.password
model: backup
path: /data
password: 123456
[root@m01 ansible]# vim site.yml 
- name: all
  hosts: all
  roles:
    - common
- name: this rsync
  hosts: backup
  remote_user: root
  roles:
    - rsync

相关内容

    暂无相关文章