Puppet客户端自动安装脚本

运维自动化发展已经是势不可挡,传统的靠大量人力运维的方式渐不能满足企业IT发展的需求,其中Puppet自动化更是受到IT人士的青睐,那今天我们一起来研究一下puppet客户端的自动安装脚本。脚本不足之处,欢迎指正,共同进步!

  背景:当我们需要使用puppet批量管理服务器时,首先需要指定FQDN名或者DNS解析,然后接下来就是批量安装puppet客户端了,怎样安装比较快呢?这里仅供参考,把脚本放置在kickstart ks.cfg文件%post段,系统安装完毕后,会执行该脚本!直接看脚本如下:

  
  
  
  
  1. #!/bin/sh  

  2. #auto install puppet client  

  3. #wugk 2012-11-30  

  4. #PUPPET_SERVER=192-9-117-160-app.com  

  5. #此脚本为客户端安装,同时包含服务器端安装,根据实际需求修改即可。

  6. cat <<EOF  

  7. ++++++++++++++++++++welcome to use auto install puppet ++++++++++++++++++  

  8. +++++++++-----------------------------------------------+++++++++++++++++

  9. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  

  10. EOF  

  11. #安装ruby及相关库支持      

  12. echo "Please waiting ruby soft ........"

  13.    rpm -ivh compat-readline5-5.2-17.1.el6.x86_64.rpm ruby-libs-1.8.7.352-7.el6_2.x86_64.rpm ruby-1.8.7.352-7.el6_2.x86_64.rpm ruby-devel-1.8.7.352-7.el6_2.x86_64.rpm  

  14. echo  

  15. echo  

  16. #定义客户端安装函数client,即定义client命令      

  17. client ()  

  18. {  

  19.    echo "Now install puppet client rpm -ivh puppet-2.7.20-1.el6.noarch.rpm"

  20.    #安装facter源码包,主要用于检测客户端本机信息,例如主机名、ip、内存等信息  

  21.    ####now install facter soft  

  22.    tar xzf facter-1.6.8.tar.gz ;cd  facter-1.6.8/ ;/usr/bin/ruby install.rb  

  23.    #安装puppet客户端程序,这里采用rpm包安装方式

  24.    cd ../; rpm -ivh --nodeps  puppet-2.7.20-1.el6.noarch.rpm

  25.    #配置config  puppet puppet_Server可以手动输入,也可以指定变量

  26.    ####sed auto puppet config  

  27. read -p "Please insert puppet server address:" PUPPET_SERVER  

  28.    sed -i 's/^#PUPPET/PUPPET/g' /etc/sysconfig/puppet

  29.    #指定服务端主机名,并配置监听端口、允许认证      

  30.    grep $PUPPET_SERVER /etc/sysconfig/puppet ;[ $? -ne 0 ]&&sed -i "/PUPPET_SERVER/s/puppet/$PUPPET_SERVER/g" /etc/sysconfig/puppet      grep listen /etc/puppet/puppet.conf ;[ $? -ne 0 ]&&echo "   listen = true" >> /etc/puppet/puppet.conf  

  31.    tail -2 /etc/puppet/auth.conf |grep "allow *" ;[ $? -ne 0 ]&&echo "allow *" >> /etc/puppet/auth.conf  

  32.    echo -e "[puppetrunner]\nallow  * " >/etc/puppet/namespaceauth.conf  

  33.    #配置puppet master服务器端主机名和客户端本地主机名,获取本机的ip信息,并加入/etc/hosts中

  34.    grep $PUPPET_SERVER /etc/hosts ;[  $? -ne 0 ] &&echo "`echo $PUPPET_SERVER|awk -F- '{print $1"."$2"."$3"."$4}'` $PUPPET_SERVER" >>/etc/hosts  

  35.    echo +++++++++++++++++++++++++++++++++++++++++++++++++++++  

  36.    ETH=`ifconfig |grep Bcast|cut -d: -f 2|awk '{print $1}'`  

  37.    echo "Please insert PUPPET_SERVER /etc/hosts :: $ETH `echo $ETH|sed 's/\./-/g'`-app.com"

  38.    cat /etc/hosts |grep -v 127 |grep $ETH ;[  $? -ne 0 ] &&echo "$ETH `echo $ETH|sed 's/\./-/g'`-app.com" >>/etc/hosts  

  39.    echo "The puppet is install puppet ,please exec puppet agent --server=$PUPPET_SERVER --test"

  40.    HOSTNAME=`echo $ETH|sed 's/\./-/g'`-app.com  

  41.    hostname $HOSTNAME  

  42.    cat /etc/hosts|grep 127.0.0.1|grep $HOSTNAME ;[ $? -ne 0 ] &&sed -i "s/127.0.0.1/127.0.0.1 $HOSTNAME/" /etc/hosts  

  43.    rm -rf facter-1.6.8  

  44. }  #定义服务器端安装函数server,即定义server命令

  45. server ()  

  46. {  

  47.    echo "Now install puppet client rpm -ivh puppet-2.7.20-1.el6.noarch.rpm"

  48.    #安装puppet master、mongrel支持Nginx+多端口puppetmaster负载均衡

  49.    rpm -ivh --nodeps puppet-server-2.7.20-1.el6.noarch.rpm puppet-2.7.20-1.el6.noarch.rpm

  50.    rpm -ivh --nodeps ruby-irb-1.8.7.352-7.el6_2.x86_64.rpm

  51.    rpm -ivh --nodeps ruby-rdoc-1.8.7.352-7.el6_2.x86_64.rpm

  52.    rpm -ivh --nodeps rubygem-daemons-1.0.10-2.el6.noarch.rpm

  53.    rpm -ivh --nodeps rubygem-fastthread-1.0.7-2.el6.x86_64.rpm

  54.    rpm -ivh --nodeps rubygem-gem_plugin-0.2.3-3.el6.noarch.rpm

  55.    rpm -ivh --nodeps rubygem-mongrel-1.1.5-3.el6.x86_64.rpm

  56.    rpm -ivh --nodeps rubygem-rake-0.8.7-2.1.el6.noarch.rpm

  57.    rpm -ivh --nodeps rubygems-1.3.7-1.el6.noarch.rpm

  58.    #安装facter

  59.    ####now install facter soft  

  60.    tar xzf facter-1.6.8.tar.gz ;cd  facter-1.6.8/ ;/usr/bin/ruby install.rb  

  61.    ####sed auto puppet config  

  62.    echo  

  63. read -p "Please insert puppet server address:" PUPPET_SERVER  

  64.    grep listen /etc/puppet/puppet.conf ;[ $? -ne 0 ]&&echo "   listen = true" >> /etc/puppet/puppet.conf  

  65.    grep autosign /etc/puppet/puppet.conf ;[ $? -ne 0 ]&&sed -i '/ssldir/a\    autosign = true' /etc/puppet/puppet.conf  

  66.    tail -2 /etc/puppet/auth.conf |grep "allow *" ;[ $? -ne 0 ]&&echo "allow *" >> /etc/puppet/auth.conf  

  67.    tail -1 /etc/sysconfig/puppetmaster |grep "mongrel";[ $? -ne 0 ]&& echo -e "PUPPETMASTER_PORTS=( 18140 18141 18142 18143 18144 )\nPUPPETMASTER_EXTRA_OPTS=\"-servertype=mongrel  --ssl_client_header=HTTP_X_SSL_SUBJECT\"" >>/etc/sysconfig/puppetmaster  

  68.    #配置puppet master 、Hosts

  69.    grep $PUPPET_SERVER /etc/hosts ;[  $? -ne 0 ] &&echo "`echo $PUPPET_SERVER|awk -F- '{print $1"."$2"."$3"."$4}'` $PUPPET_SERVER" >>/etc/hosts  

  70.    echo +++++++++++++++++++++++++++++++++++++++++++++++++++++  

  71.    echo  

  72.    ETH=`ifconfig |grep Bcast|cut -d: -f 2|awk '{print $1}'`  

  73.    echo "Please insert PUPPET_SERVER /etc/hosts :: $ETH `echo $ETH|sed 's/\./-/g'`-app.com"

  74.    cat /etc/hosts |grep -v 127 |grep $ETH ;[  $? -ne 0 ] && echo "$ETH `echo $ETH|sed 's/\./-/g'`-app.com" >>/etc/hosts  

  75.    HOSTNAME=`echo $ETH|sed 's/\./-/g'`-app.com  

  76.    hostname $HOSTNAME  

  77.    cat /etc/hosts|grep 127.0.0.1|grep $HOSTNAME ;[ $? -ne 0 ] &&sed -i "s/127.0.0.1/127.0.0.1 $HOSTNAME/" /etc/hosts  

  78.    cd ../ ;rm -rf facter-1.6.8  

  79. }  

  80. #如下case条件语句可以选用,这里仅供参考,真实安装去掉函数定义和case方式即可。

  81. ###config caseselect

  82. #$1表示第一个参数  

  83. case $1  in

  84.   client )  

  85.   client  

  86. ;;    

  87.   server )  

  88.   server  

  89. ;;  

  90.    * )  

  91.  echo "Please exec $0 { client or server }"

  92. ;;  

  93. esac

此脚本仅供参考,欢迎大家多交流!


你可能感兴趣的:(服务器,服务端,puppet,客户机)