设为首页 收藏本站
查看: 2862|回复: 1

[经验分享] 使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-23 09:26:48 | 显示全部楼层 |阅读模式
第1章 思考1.1如何一键使用ansible搭建1.1.1第一步目标首先我们要明确的是我们需要实现的是一键的自动化脚本,既执行一个脚本然后就自动化安装nfs+rsync+sersync,然后让web01自动挂载nfs共享目录,
1.1.2第二步关系明确及顺序既然有目标了那么我们需要做的就是我们需要明白自动化安装的先后顺序。
  • 既然我们是需要ansible才能自动化安装那么我们就需要先安装ansible
  • 安装了ansible之后,既然我们想要挂载,那么我们就需要有nfs共享,和rsync备份。平级
  • 在安装了nfs和rsync之后我们就需要的是sersync来实现实时的备份。
  • 在都安装了之后我们最后做的才是需要的web01客户端的挂载
  • 1.3Ansible
Ansible是基于ssh-key的。所以我们需要的ssh秘钥登录无密码。但是如果想要非交互式秘钥认证登录那么我们就需要sshpass而这个包则是ansible才能提供的(也可以直接yum安装),而如果要安装ansible那么需要清楚的是ansible是基于erel源的所以步骤如下
  • epel
  • yum ansible
  • 秘钥认证
  • 安装ansible
  • 修改配置文件
  • 判断是否成功管理
  • 1.4Rsync
Rsync是实现备份传输的,他的安装只需要安装rsync,创建配置文件,创建密码文件,创建用户,修改权限,启动rsync
1.1.5Nfs这里需要明确一下的就是nfs是基于rpcbind的,所以我们需要先安装的是rpcbind+nfs,然后我们需要明确原理nfs启动需要找rpcbind,来实现的,所以我们需要先启动rpcbind,在启动nfs
1.1.6Sersync要实现sersync就需要有这个包,所以我们直接在脚本rz就好,但是系统默认不带rz命令所以我们需要先安装lrzsz(我对系统优化自带rz命令)。然后修改配置文件后台运行。
1.1.7Web01这里web01只是当了一个客户端所以我们只在他这挂载就好,不需要别的
第2章 部署安装2.1目录规范
脚本
/server/scripts
安装包
/opt/src
安装目录
/usr/local
Ansible的yml文件
/etc/ansible/yml

2.2执行过程只需要执行install.sh就好
2.3个个脚本的创建2.3.1Install脚本(只需要执行这个脚本就好)#!/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
2.3.2安装ansible脚本#!/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 >>/etc/ansible/hosts<<EOF
[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
2.3.3Yml批量安装的文件---
- 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
2.3.4安装rsync脚本#!/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 > rsyncd.conf << EOF
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
2.3.5安装sersync脚本#!/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
2.4讲解在这里我是通过install,sh来进行调用ansible的安装脚本,来先安装ansible,在安装并确定无错误之后我又调用执行了index.yml。而index.yml则是先将hosts文件拷贝过去,然后安装调用rsync安装脚本安装,在之后调用sersync安装脚本,安装sersync。在之后yml又让ansible通过web01直接挂载使用nfs自动共享的目录。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-353884-1-1.html 上篇帖子: ansible安装配置(一) 下篇帖子: Ansible配置及使用
累计签到:119 天
连续签到:1 天
发表于 2017-4-24 13:16:24 | 显示全部楼层
非常好的一篇分享文件

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表