使用ansible对centos服务器远程批量初始化脚本

一、安装ansible

1
brew install ansible

二、安装sshpass(免密)

1
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

三、脚本介绍

  • init.sh
    • 输入需要初始化计算机数量
    • 输入被初始化ip地址
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      💬  [🔸 70 🔸 zengguang@localhost linux_server_init]$ 👉  cat init.sh
      #!/bin/bash
      #author zengguang
      #date 2017-09-04 14:29:10
      #describe init server
      hosttime=hosts_$(date +%F)
      USER=***
      PASSWD=*******
      DIR=`pwd`


      read -p "please input will init servers number :" numserver
      pdnum=$(echo $numserver|grep "[0-9]")
      send(){
      mv /usr/local/etc/ansible/hosts /usr/local/etc/ansible/$hosttime
      for ((a=1;a<=$numserver;a++))
      do
      echo "[zgtest$a]" >> /usr/local/etc/ansible/hosts
      read -p "please input will init $a server ipaddress :" newserver
      echo $newserver ansible_ssh_user=$USER ansible_ssh_pass=$PASSWD >> /usr/local/etc/ansible/hosts
      done
      }
      send2(){
      echo "########################################COPY START#########################################"
      ansible all -m copy -a "src=$DIR/serverinit.sh dest=/root"
      echo "########################################CHMOD START#########################################"
      ansible all -m command -a "chmod 755 /root/serverinit.sh"
      echo "########################################SERVERINIT START#########################################"
      ansible all -m command -a "sh /root/serverinit.sh"
      }
      if [ -z $numserver ]
      then
      echo "your input the init server number is 0"
      exit 0
      elif [ -z $pdnum ]
      then
      echo "your input is not num"
      exit 1
      else
      send
      send2
      fi
  • serverinit.sh
    • 更改复杂root密码
    • 增加用户**
    • 挂载disk
    • 添加远程 ssh-key
    • yum安装所 软件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
       💬  [🔸 71 🔸 zengguang@localhost linux_server_init]$ 👉  cat serverinit.sh
      #!/bin/bash
      #author zengguang
      #date 2017-09-01 17:49:01
      #describe serverinit script

      USER=user
      usergroup(){
      groupadd -f $USER
      useradd -m -d '/home/user' -g $USER $USER
      TXPASSWD=$(< /dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB'| head -c 16 ;echo)
      echo "$USER:$TXPASSWD"|chpasswd
      echo "userpasswd is :" >> /tmp/guang.log
      echo $TXPASSWD >> /tmp/guang.log
      }

      keygen(){
      zengguang="***************ssh-key*************"
      ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
      echo "#zengguang" >> ~/.ssh/authorized_keys
      echo $zengguang >> ~/.ssh/authorized_keys
      chmod -w ~/.ssh/authorized_keys
      su $USER<<EOF
      ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
      echo "#zengguang" >> ~/.ssh/authorized_keys
      echo $zengguang >> ~/.ssh/authorized_keys
      chmod -w ~/.ssh/authorized_keys
      EOF
      }

      diskmount(){
      disknum=$(fdisk -l| grep "/dev"|grep -v "/dev/vda"|grep "GB"|awk '{print $2}'|cut -d ":" -f 2|awk '$1>90{print $1}'|wc -w)
      if [ "$disknum" == "0" ]
      then
      echo "that's no disk need mount" >> /tmp/guang.log
      elif [ "$disknum" == "1" ]
      then
      echo "one disk need to mount" >> /tmp/guang.log
      onedisk=$(fdisk -l| grep "/dev"|grep -v "/dev/vda"|grep "GB"|awk '{print $2}'|cut -d ":" -f 1|sed -n '1p')
      mkdir /system
      mkfs.ext4 $onedisk
      sleep 5
      echo "onedisk formatting ok ">>/tmp/guang.log
      echo $onedisk ' /system ext4 defaults 0 0' >> /etc/fstab
      mount -a
      chown tongxin:tongxin -R /system
      echo -e '\E[32m'"onedisk mount ok">>/tmp/guang.log
      elif [ "$disknum" == "2" ]
      then
      echo "two disks need to mount" >> /tmp/guang.log
      onedisk=$(fdisk -l| grep "/dev"|grep -v "/dev/vda"|grep "GB"|awk '{print $2}'|cut -d ":" -f 1|sed -n '1p')
      twodisk=$(fdisk -l| grep "/dev"|grep -v "/dev/vda"|grep "GB"|awk '{print $2}'|cut -d ":" -f 1|sed -n '2p')
      mkdir /{system,docker}
      mkfs.ext4 $onedisk
      sleep 5
      mkfs.ext4 $twodisk
      sleep 5
      echo "twodisks formatting ok ">>/tmp/guang.log
      echo $onedisk ' /system ext4 defaults 0 0' >> /etc/fstab
      echo $twodisk ' /docker ext4 defaults 0 0' >> /etc/fstab
      mount -a
      chown tongxin:tongxin -R /system
      chown tongxin:tongxin -R /docker
      echo -e '\E[32m'"twodisks mount ok">>/tmp/guang.log

      else
      echo "the disks more then two .please look look"
      fi
      }

      echo "######################################## change root passwd #########################################"
      ROOTPASSWD=$(< /dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB'| head -c 32 ;echo)
      echo "root:$ROOTPASSWD"|chpasswd
      echo "rootpasswd is :" >> /tmp/guang.log
      echo $ROOTPASSWD >> /tmp/guang.log
      echo "######################################## add user user #########################################"
      usergroup
      echo "######################################## mount disks #########################################"
      diskmount
      echo "######################################## chmod user #########################################"
      echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
      echo "######################################## change PS1 #########################################"
      echo "export PS1='[\[\e[32m\]#\##\[\e[31m\]\u@\[\e[36m\]\h \W]\\$\[\e[m\] '">> /etc/profile
      source /etc/profile
      echo "######################################## change no passwd ########################################"
      keygen
      echo "######################################## yum install other #########################################"
      sudo yum -y install gcc gcc-c++ ncurses-devel perl pcre-devel openssl openssl-devel libcurl-devel
      cat /tmp/guang.log
      echo "######################################## rm others #########################################"
      sudo rm -rf /root/serverinit.sh
      sudo rm -rf /tmp/guang.log