710661809 发表于 2018-7-29 12:31:35

使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载

  使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载
  作者:ls
  归档:学习笔记
  2016/6/27
  快捷键:
  Ctrl + 1    标题1
  Ctrl + 2    标题2
  Ctrl + 3    标题3
  Ctrl + 4    实例
  Ctrl + 5    程序代码
  Ctrl + 6    正文
  格式说明:
  蓝色字体:注释
  黄色背景:重要
  绿色背景:注意
  
  http://blog.51cto.com/e/u261/themes/default/images/spacer.gif
  老男孩教育教学核心思想6重:重目标、重思路、重方法、重实践、重习惯、重总结
  学无止境,老男孩教育成就你人生的起点!
  联系方式:
  网站运维QQ交流群:
  Linux385168604
  架构师 390642196
  Python 29215534
  大数据 421358633
  官方网站:
  http://www.oldboyedu.com
  目录
  第1章 ctrl+1. 1
  1.1 ctrl+2. 1
  1.1.1 ctrl+3. 1
  

第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
  
  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
  
  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]
查看完整版本: 使用ansible自动化部署nfs+rsync+sersync+web01自动化挂载