使用ansible服务,实现批量管理
第一步,先分发公钥,实现ansible无密码进行控制
#!/bin/bash

#mk key 2
rm -f /root/.ssh/id*
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P "" -q

#fenfa

for ip in 41 7 31
do
echo ================fs host 172.16.1.$ip======================
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.$ip"
echo ================fs end 172.16.1.$ip=====================
done

第二步,编写剧本,实现安装rsync,nfs,以及挂载nfs共享目录并且实时备份
#安装相应软件

  • hosts: yxd
    tasks:
    • name: yum rsync,nfs and rpcbind
      yum: name=rsync,nfs-utils,rpcbind state=installed
      #配置rsync服务
  • hosts: 172.16.1.41
    tasks:
    • name: rsync conf
      copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/rsyncd.conf
    • name: mk user
      user: name=rsync shell=/sbin/nologin createhome=no
    • name: mk dir
      file: path=/backup mode=0755 owner=rsync group=rsync state=directory
    • name: passfile
      shell: echo 'rsync_backup:123456' >/etc/rsync.password && chmod 0600 /etc/rsync.password
    • name: server on
      shell: rsync --daemon && echo 'rsync --daemon' >>/etc/rc.local
      #部署NFS并且配置密码文件,实现推数据无密码
  • hosts: 172.16.1.31
    tasks:
    • name: passfile
      shell: echo '123456' >/etc/rsync.password && chmod 0600 /etc/rsync.password
    • name: rsync
      shell: /usr/bin/rsync -az /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
    • name: mk nfsdir
      file: path=/nfs state=directory owner=nfsnobody group=nfsnobody
    • name: nfs.conf
      copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports backup=yes
    • name: rpcbind on
      service: name=rpcbind state=started enabled=yes
    • name: nfs on
      service: name=nfs state=started enabled=yes
    • name: sersync backup
      copy: src=/tmp/sersync/ dest=/sersync/
    • name: sersync on
      shell: chmod +x /sersync/bin/sersync && /sersync/bin/sersync -dro /sersync/conf/confxml.xml
      #挂载客户端到nfs共享目录
  • hosts: 172.16.1.7 172.16.1.8
    tasks:
    • name: mount nfs
      mount: state=mounted fstype=nfs src='172.16.1.31:/nfs' path=/mnt