红联Linux门户
Linux帮助

CentOS7 Puppet安装脚本小结

发布时间:2016-02-25 10:00:21来源:linux网站作者:life_爱水的鱼

前言:

该脚本安装对应的是puppet的server端以及client端;具体使用时参考脚本中的参数说明;


软件环境:

客户端系统:CentOS-7-x86_64
服务器端:CentOS-7-x86_64
Ruby版本:ruby-1.8.7.374-4.el6_6.x86_64.rpm
Puppet版本:3.8.4


安装脚本:

#!/bin/bash

# Program:
#   This script is to install puppet master or agent 
# History:
# 2016/01/20 yuu
#
# 参数说明:  
#   传入的第一个参数:如果为master则表示puppet server的安装,
#   不传或传其它的参数值则表示安装puppet client 

master="master"

if [ $# == 0 ]
then
target="client"
else
target=$1
fi

function installPuppet()
{
#if [ "`rpm -qa | grep -c ntp`" == 0 ],该句效果和下面两句一样
rpm -qa | grep ntp >> /dev/null
if [ $? != 0 ]
then
yum install -y ntp && echo "###installing ntp###"
fi
systemctl start ntpd.service && echo "### start ntpd.service ###"
systemctl enable ntpd.service && echo "### enable ntpd.service ###"
yum -y install ruby ruby-libs ruby-shadow && echo "### installing ruby ###"
rpm -Uvh http://yum.puppetlabs.com/el/7Server/products/x86_64/puppetlabs-release-7-11.noarch.rpm && echo "### 配置yum源 ###"
yum clean all

# 以下内容为server与client端的不同
if [ $target = $master ]
then
yum -y install puppet-server && echo "### installing puppet server ###"
else
yum -y install puppet && echo "### installing puppet client ###"
fi

if [ $? == 0 ]
then
echo "### install puppet succeeded ###"
if [ $target = $master ]
then
systemctl start puppetmaster.service && echo "### start puppetmaster.service ###"
systemctl enable puppetmaster.service && echo "### enable puppetmaster.service ###"
# 服务端监听8140端口,iptables -nv -L | grep -w 8140可查看开放监听的端口
/sbin/iptables -A INPUT -p tcp --dport 8140 -j ACCEPT && echo "### server listen 8140 ###"
else
systemctl start puppet.service && echo "### start puppet.service ###"
systemctl enable puppet.service && echo "### enable puppet.service ###"
# 客户端监听8139端口
/sbin/iptables -A INPUT -p tcp --dport 8139 -j ACCEPT && echo "### client listen 8139 ###"
fi
else
echo "### install puppet failed ###"
fi
}
installPuppet


小结与思考:

1: bash shell 的if语句会定义if行定义的那个命令,如果if后面命令的退出状态码是0(即该命令成功运行),位于then部分的命令就会被执行,如果返回状态码是其它值,则then部分的内容不会执行;如:

if date
then
echo "success"
fi

2:注意$#,$1,$2...等参数在函数内与函数外之间的区别;
3:字符串比较注意 $str1 = $str2 比较;
4:学习上面关于命令的返回值做判断的两种方式;


参考资料:

《Puppet实战》 作者:刘宇 著 出版社: 机械工业出版社 出版时间:2014年1月


本文永久更新地址:http://www.linuxdiyf.com/linux/18374.html