使用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: yxdtasks:
- name: yum rsync,nfs and rpcbindyum: name=rsync,nfs-utils,rpcbind state=installed#配置rsync服务
- hosts: 172.16.1.41tasks:
- name: rsync confcopy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/rsyncd.conf
- name: mk useruser: name=rsync shell=/sbin/nologin createhome=no
- name: mk dirfile: path=/backup mode=0755 owner=rsync group=rsync state=directory
- name: passfileshell: echo 'rsync_backup:123456' >/etc/rsync.password && chmod 0600 /etc/rsync.password
- name: server onshell: rsync --daemon && echo 'rsync --daemon' >>/etc/rc.local#部署NFS并且配置密码文件,实现推数据无密码
- hosts: 172.16.1.31tasks:
- name: passfileshell: echo '123456' >/etc/rsync.password && chmod 0600 /etc/rsync.password
- name: rsyncshell: /usr/bin/rsync -az /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
- name: mk nfsdirfile: path=/nfs state=directory owner=nfsnobody group=nfsnobody
- name: nfs.confcopy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports backup=yes
- name: rpcbind onservice: name=rpcbind state=started enabled=yes
- name: nfs onservice: name=nfs state=started enabled=yes
- name: sersync backupcopy: src=/tmp/sersync/ dest=/sersync/
- name: sersync onshell: chmod +x /sersync/bin/sersync && /sersync/bin/sersync -dro /sersync/conf/confxml.xml#挂载客户端到nfs共享目录
- hosts: 172.16.1.7 172.16.1.8tasks:
- name: mount nfsmount: state=mounted fstype=nfs src='172.16.1.31:/nfs' path=/mnt