标签:component 用户 bin rzsz directory oca sea exp 后台运行
首先我们要明确的是我们需要实现的是一键的自动化脚本,既执行一个脚本然后就自动化安装nfs+rsync+sersync,然后让web01自动挂载nfs共享目录,
既然有目标了那么我们需要做的就是我们需要明白自动化安装的先后顺序。
既然我们是需要ansible才能自动化安装那么我们就需要先安装ansible
安装了ansible之后,既然我们想要挂载,那么我们就需要有nfs共享,和rsync备份。平级
在安装了nfs和rsync之后我们就需要的是sersync来实现实时的备份。
在都安装了之后我们最后做的才是需要的web01客户端的挂载
Ansible是基于ssh-key的。所以我们需要的ssh秘钥登录无密码。但是如果想要非交互式秘钥认证登录那么我们就需要sshpass而这个包则是ansible才能提供的(也可以直接yum安装),而如果要安装ansible那么需要清楚的是ansible是基于erel源的所以步骤如下
epel
yum ansible
秘钥认证
安装ansible
修改配置文件
判断是否成功管理
Rsync是实现备份传输的,他的安装只需要安装rsync,创建配置文件,创建密码文件,创建用户,修改权限,启动rsync
这里需要明确一下的就是nfs是基于rpcbind的,所以我们需要先安装的是rpcbind+nfs,然后我们需要明确原理nfs启动需要找rpcbind,来实现的,所以我们需要先启动rpcbind,在启动nfs
要实现sersync就需要有这个包,所以我们直接在脚本rz就好,但是系统默认不带rz命令所以我们需要先安装lrzsz(我对系统优化自带rz命令)。然后修改配置文件后台运行。
这里web01只是当了一个客户端所以我们只在他这挂载就好,不需要别的
脚本 | /server/scripts |
安装包 | /opt/src |
安装目录 | /usr/local |
Ansible的yml文件 | /etc/ansible/yml |
只需要执行install.sh就好
#!/bin/bsh
sh Automatic_deployment_ansible.sh
mkdir -p /opt/src
echo "pleace rz rsync to /opt/src"
cd /opt/src
rz
if [ -f /opt/src/ser*.tar.gz ]
then
mkdir /etc/ansible/yml
cd /etc/ansible/yml
rz
echo "pleace input you install.yml,"
ansible-playbook index.yml
else
echo "you can not true sersync to /opt/src tryagent"
fi
#!/bin/bash
#just prevent you have ssh key
rm -rf /root/.ssh/id_dsa* >> /dev/null2>&1
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P"" >> /dev/null 2>&1
#加载repo源
wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo && \
#all can ssh each other
yum install ansible -y >> /dev/null2>&1 && \
echo "install ansible"
#judge you ansible install
if [ "`rpm -qa | grep -o ansible`" =="ansible" ]
then
for ip in web01 rsync nfs01
do
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-oStrictHostKeyChecking=no root@$ip" >> /dev/null 2>&1
#justfor you Determine whether distribution of success of ssh
export a=$(ssh $ip hostname -I|awk ‘{print $1"|"$2}‘)
export b=$(cat /etc/hosts | grep $ip | awk ‘{print $1}‘)
done
if [["$b" =~ $a ]]
then
#insert you group of ansible
# read -p "pleaceinsert you ansible group name: " grop
# read -p "pleaceinsert The name of the group members just like one two ...: " arg
cat >> EOF<< /etc/ansible/hosts
[ls]
web01
nfs01
rsync
EOF
#judge you can useansible
ansible ls -m command-a "hostname" 2>> /var/log/ansible.log
if [ $? -ne 0 ]
then
echo"ERROR:you can not use ansible,pleace see /var/log/ansible.log"
else
echo"congratulations!!you ansible successful! and now you can do youwant"
fi
else
echo "you fenfa falit"
fi
else
echo "error:you can not install ansible"
fi
---
- hosts: ls #$(tail /etc/ansible/hosts | sed -rn‘s#^\[(.*)\]$#\1#gp‘)
tasks:
- name:copy
copy:src=/etc/hosts dest=/etc/hosts
- hosts: rsync
tasks:
- name:install rsync
script:/server/scripts/Automatic_deployment_rsync.sh
- hosts: nfs01
tasks:
- name:install nfs01
script:/server/scripts/Automatic_deployment_nfs.sh
- hosts: nfs01
tasks:
- name:copy sersync
copy:src=/opt/src dest=/opt/
- name:install sersync
script:/server/scripts/Automatic_deployment_sersync.sh
- hosts: web01
tasks:
- name :mount
command: mount 172.16.1.31:/backup /mnt
#!/bin/bash
#if install rsync
dir=/etc/rsyncd
#if install already and start we neet to stop rsyncand rm pid file
ps -ef | grep rsync | awk -F" " ‘{print$2}‘ >> /dev/null 2<&1
rm -rf /var/run/rsyncd.pid
#i need create configuration directory
mkdir -p $dir
# Determine whether the installation rsync
if [ "`rpm -qa | grep -o rsync`" !="rsync" ]
then
yum install rsync -y >> /dev/null 2>&1
if [ "`rpm -qa | grep -o rsync`" !="rsync" ]
then
echo"can not install rsync pleace see you network"
fi
fi
#Enter the directory
cd $dir
#Create a configuration file
cat >EOF << rsyncd.conf
uid = rsync
gid = rsync
use chroot =no
maxconnections = 200
timeout =300
pid file =/var/run/rsyncd.pid
lock file =/var/run/rsync.lock
log file =/var/log/rsyncd.log
[backup]
path =/backup/
ignoreerrors
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/rsyncd/password
EOF
#Determine whether a user
user=`id rsync`
if [ $? -eq 0 ]
then
chown -Rrsync.rsync /etc/rsyncd
else
useradd -s/sbin/nologin -M rsync
fi
#Create a password file
echo "rsync_backup:123456" >/etc/rsyncd/password
#give permissions password file
chmod 600 /etc/rsyncd/password
chown root.root $dir/password
mkdir -p /backup >> /dev/null 2>&1
chown rsync.rsync /backup
#start rsync
rsync --daemon --config=$dir/rsyncd.conf
#!/bin/bash
sedir=/usr/local/sersync
cd /opt/src
mkdir -p /usr/local/sersync
tar -zxvf sersync*.tar.gz -C $sedir--strip-components 1 >> /dev/null 2>&1
echo "123456" > $sedir/passwd
chmod 600 $sedir/passwd
sed -i ‘s#<localpathwatch="/opt/tongbu">#<localpathwatch="/backup">#g‘ $sedir/confxml.xml
sed -i ‘s#<remote ip="127.0.0.1"name="tongbu1"/>#<remote ip="rsync"name="backup"/>#g‘ $sedir/confxml.xml
sed -i ‘s#<auth start="false"users="root" passwordfile="/etc/rsync.pas"/>#<authstart="true" users="rsync_backup"passwordfile="/usr/local/sersync/passwd"/>#g‘ $sedir/confxml.xml
cd $sedir
./sersync2 -d -o confxml.xml >> /dev/null2>&1
在这里我是通过install,sh来进行调用ansible的安装脚本,来先安装ansible,在安装并确定无错误之后我又调用执行了index.yml。而index.yml则是先将hosts文件拷贝过去,然后安装调用rsync安装脚本安装,在之后调用sersync安装脚本,安装sersync。在之后yml又让ansible通过web01直接挂载使用nfs自动共享的目录。
使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载
标签:component 用户 bin rzsz directory oca sea exp 后台运行
原文地址:http://523064.blog.51cto.com/513064/1909135