ansible实时数据同步

rsync快速部署纯代码

1. 环境的准备

批量管理服务器 m01 192.168.80.61 
备份服务器   backup 192.168.80.41
存储服务器   nfs01 192.168.80.31

通过克隆进行虚拟环境的准备,网址的配置进行的是/24,
接下来ip解析

vim /etc/hosts

用的内网ip

172.16.1.61 m01
172.16.1.31 nfs01
172.16.1.41 backup

ssh连接多台主机免密登录

ssh-keygen -t dsa 

分发
ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.31
ssh 主机名称

脚本分发:

yum  -y install sshpass

sshpass -p123456 ssh-copy-id -i /root/.ssh/id_pub.dsa -p 22 root@172.16.1.$ip "-o StrictHostKeyChecking=no"

脚本:

#!/bin/bash

for ip  in 31 41 
do
	echo "====================start fenfa 172.16.1.$ip===================="
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub  root@172.16.1.$ip "-o StrictHostKeyChecking=no" &>/dev/null
	echo "=================host 172.16.1.$ip success!!!!=================="
	echo "====================            end         ===================="
done

多个ip:

#!/bin/bash

for ip  in `cat /server/scripts/ip.txt`
do
	echo "====================start fenfa          $ip===================="
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub  root@$ip "-o StrictHostKeyChecking=no" &>/dev/null
	echo "=================host          $ip success!!!!=================="
	echo "====================            end         ===================="
done


2. ansible剧本环境准备

yum install -y ansible

3. rsync服务的剧本编写

服务器端:
1.安装rsync
2.启动
3.编写配置文件,重启rsync
4.创建虚拟用户
5.创建目录,修改目录权限
6.创建密码文件,账户:密码,权限600

客户端:
1.安装rsync
2.启动
3.创建密码文件,这里只需要密码,权限600
4.测试

剧本的编写:
需要的配置文件
rsyncd.conf

uid = rsync      
gid = rsync     
port = 873    
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by yq"  
path =/backup

服务器端的rsync.password

rsync_backup:123456

客户端的rsync.password

123456

剧本:

- hosts: backup
  #ignore_errors: yes
  tasks:
    - name: 01-yum-rsync
      yum: name=rsync state=installed
    - name: 02-copy-file
      copy: src=/etc/ansible/ansible-playbook/conf/{{item.src}} dest={{item.dest}} mode={{item.mode}}
      with_items: 
        - {src: 'rsyncd.conf',dest: '/etc/',mode: '644'}
        - {src: 'rsync.password',dest: '/etc/',mode: '600'}
      notify: re-rsync
    - name: 03-useradd
      user: name=rsync create_home=no shell=/sbin/nologin
    - name: 04-create-direcity
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 05-start-service
      service: name=rsyncd state=started enabled=yes
    - name: 06-check
      shell: 'netstat -lntup|grep 873'
      register: result
    - name: 07-show
      debug: msg={{result.stdout_lines}}
  handlers:
    - name: re-rsync
      service: name=rsyncd state=restarted

- hosts: nfs01
  vars: 
    mimafile: /etc/ansible/ansible-playbook/conf/rsync.password
  tasks: 
    - name: 01-insatll
      yum: name=rsync state=installed
    - name: 01-1start
      service: name=rsyncd state=started enabled=yes
    - name: 02-mimafile
      copy: content=123456 dest=/etc/rsync.password mode=600
    - name: 03-test
      file: path=/tmp/yang.txt  state=touch 
      when: ( ansible_hostname=='nfs01')
    - name: 04-shell
      shell: rsync -avz /tmp/yang.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

注意文件的路径,服务器端的密码与客户端的密码文件不一样,会出现error module的错误
脚本检查:

ansible-playbook --syntax-check rsync.yaml

本地运行测试

ansible-playbook -c rsync.yaml

正式的运行

ansible-playbook rsync.yaml
上一篇:Vue基础 第五讲笔记:v-show、v-if、v-bind


下一篇:vue的toast组件封装