Puppet根据节点机器名推送并自动执行SHELL脚本

简介:

puppet的基础环境介绍:

puppet服务器和客户端都已下载了epel的外部yum源,都已通过yum程序自动安装了puppet程序,过程比较简单,这里就不一一介绍了,机器都放置在同一局域网内,cn7788.com的域名,内部有内网DNS环境,没有用LDP作为域控,局域网还有其它客户端,由于不需要使用puppet环境,所以就不一一介绍了。

1
2
3
4
server.cn7788.com 192.168.1.124  puppet-master
client.cn7788.com 192.168.1.125  puppet-client
lamp.cn7788.com 192.168.1.126  puppet-client
xen.cn7788.com  192.168.1.144 puppet-client

大家可以将上面的域名对应关系可将其都写在各自机器的/etc/hosts文件里,在各个puppet客户端上建议ntpdate精准对时(因为puppet的证书对时间要求严格),不然puppet-client连接时会报如下错误:

1
2
3
4
5
6
7
warning: peer certificate won't be verified  in  thisSSL session
info: Caching certificate  for  client.cn7788.com
info: Caching certificate_revocation_list  for  ca
err: Could not retrieve catalog from remote server:certificate verify failed.  This is oftenbecause the  time  is out of  sync  on the server or client
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run
err: Could not send report: certificate verifyfailed.  This is often because the timeis out of  sync  on the server or client

需求如下:客户机机器xen.cn7788.comlamp.cn7788.com没有安装nagios客户端程序,这时想过通过puppet-server推送SHELL脚本自动安装,其它的客户端暂时没这么需求,这个应该如何实现呢?

由于客户端节点机器比较多,所以这里需要用到节点和模块的概念,这里我们先建立名为nagioscli的模块,如下所示:

1
mkdir  -p /etc/puppet/modules/nagioscli/ {manifests,files,templates}

files目录下的nagioscli.sh文件内容如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
useradd  nagios
cd  /usr/local/src
wget wget http: //syslab .comsenz.com /downloads/linux/nagios-plugins-1 .4.13. tar .gz
wget http: //syslab .comsenz.com /downloads/linux/nrpe-2 .12. tar .gz
tar  zxvf nagios-plugins-1.4.13. tar .gz
cd  nagios-plugins-1.4.13
. /configure
make
make  install
chown  nagios:nagios  /usr/local/nagios
chown  -R nagios:nagios  /usr/local/nagios/libexec
cd  ../
tar  zxvf nrpe-2.12. tar .gz
cd  nrpe-2.12
. /configure
make  all
make  install -plugin
make  install -daemon
make  install -daemon-config
sed  -i 's@allowed_hosts=127.0.0.1@allowed_hosts=114.112.11.11@' /usr/local/nagios/etc/nrpe .cfg
#114.112.11.11为nagios服务器的IP地址,这个可以根据实际需求更改。
/usr/local/nagios/bin/nrpe  -c /usr/local/nagios/etc/nrpe .cfg -d
echo  "/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d"  >>  /etc/rc . local

site.pp文件内容如下:

1
import  "node.pp"

这里扩展了site.pp文件内容,它会载入node.pp文件,这样puppet-master在启动的时候,就会自动截入并处理node.pp文件了。

node.pp文件内容如下所示:

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
node  'lamp.cn7788.com' {
file
{ "/usr/local/src/nagioscli.sh" :
source  => "puppet://server.cn7788.com/modules/nagioscli/nagioscli.sh" ,
group => root,
owner => root,
mode  => 755,
}
exec  {
"auto install naigios client" :
command  => "sh /usr/local/src/nagioscli.sh" ,
user => "root" ,
path =>[ "/usr/bin" , "/usr/sbin" , "/bin" , "/bin/sh" ],
}
}
node  'xen.cn7788.com' {
file
{ "/usr/local/src/nagioscli.sh" :
source  => "puppet://server.cn7788.com/modules/nagioscli/nagioscli.sh " ,
group => root,
owner => root,
mode  =>644,
}
exec  {
"auto install naigios client" :
command  => "sh /usr/local/src/nagioscli.sh" ,
user => "root" ,
path =>[ "/usr/bin" , "/usr/sbin" , "/bin" , "/bin/sh" ],
}
}
node  'client.cn7788.com' {

client.cn7788.com节点机器后面什么都没有,则表示没有任何操作在此节点机器上面,因为client机器也在puppet环境里,并配置成了自动连接,配置成如此,是防止自动连接时puppet频繁报错。

这里以xen.cn7788.com为例,在其主机上输入如下命令:

1
puppetd -- test  --server server.cn7788.com

xen.cn7788.com上命令显示结果如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
info: Caching catalog  for  xen.cn7788.com
info: Applying configuration version  '1382622383'
---  /usr/local/src/nagioscli .sh       2013-10-24 22:35:36.000000000 +0800
+++  /tmp/puppet-file .22857.0   2013-10-24 22:39:08.000000000 +0800
@@ -1,4 +1,5 @@
  #!/bin/bash
+yum -y  install  httpd gcc gcc-c++ glibcglibc-common gd gd-devel
  useraddnagios
  cd /usr/local/src
  wgetwget http: //syslab .comsenz.com /downloads/linux/nagios-plugins-1 .4.13. tar .gz
info: FileBucket adding{md5}f75e9aa3fc301c8e9c85f2677feaa9b5
info: /Stage [main] //Node [xen.cn7788.com] /File [ /usr/local/src/nagioscli .sh]:Filebucketed  /usr/local/src/nagioscli .sh to puppet with sumf75e9aa3fc301c8e9c85f2677feaa9b5
notice: /Stage [main] //Node [xen.cn7788.com] /File [ /usr/local/src/nagioscli .sh] /content : contentchanged  '{md5}f75e9aa3fc301c8e9c85f2677feaa9b5'  to '{md5}a1ed4dc2b98450e3144530f32677f736'
notice: /Stage [main] //Node [xen.cn7788.com] /Exec [auto  install  naigios client] /returns :executed successfully
notice: Finished catalog run  in  283.11 seconds

执行时间比较长,总共耗时283.11秒,我们要检查下xen.cn7788.com的节点机器上是否开启了nrpe 进程,输入命令如下所示:

1
ps  aux |  grep  nrpe |  grep  v  grep

命令显示结果如下所示:

1
nagios  22331  0.0  0.1  5108   924 ?        Ss  22:35   0:00 /usr/local/nagios/bin/nrpe  -c  /usr/local/nagios/etc/nrpe .cfg -d

我们检查下/etc/rc.local,看此命令有没有添加进去,命令如下:

1
grep  - v "^#"  /etc/rc . local

命令执行结果显示如下所示:

1
2
touch  /var/lock/subsys/local
/usr/local/nagios/bin/nrpe  -c /usr/local/nagios/etc/nrpe .cfg -d

检查结果说明puppet-masternagioscli模块是正常的,lamp.cn7788.com的结果类似,这里就不再贴出检测结果了,我们主要看下lamp.cn7788.com总共耗时多少,命令如下所示:

1
puppetd -- test  --serverserver.cn7788.com

结果如下所示:

1
2
3
4
info: Caching catalog  for  lamp.cn7788.com
info: Applying configuration version  '1382622383'
notice:  /Stage [main] //Node [lamp.cn7788.com] /Exec [autoinstall naigios client] /returns : executed successfully
notice: Finished catalog run  in  169.08 seconds

执行时间比较长,总共耗时169.08秒。


其实工作中像这种推送脚本执行的需求还是很多的,类似在各种不同名字的节点上执行的优化服务器命令、批量清除varnish缓存加速服务器缓存、根据机器名推送文件,我们只需要将此案例稍为变通下即可在工作中投入应用了。









本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/1317107,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
5月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
105 17
|
5月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
126 4
|
5月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
270 3
|
6月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
433 25
|
7月前
|
关系型数据库 Shell 网络安全
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
|
8月前
|
JavaScript Shell C#
多种脚本批量下载 Docker 镜像:Shell、PowerShell、Node.js 和 C#
本项目提供多种脚本(Shell、PowerShell、Node.js 和 C#)用于批量下载 Docker 镜像。配置文件 `docker-images.txt` 列出需要下载的镜像及其标签。各脚本首先检查 Docker 是否安装,接着读取配置文件并逐行处理,跳过空行和注释行,提取镜像名称和标签,调用 `docker pull` 命令下载镜像,并输出下载结果。使用时需创建配置文件并运行相应脚本。C# 版本需安装 .NET 8 runtime。
445 3
|
12月前
|
运维 Linux Apache
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
Puppet 作为一款强大的自动化运维工具,被广泛应用于配置管理领域。通过定义资源的状态和关系,Puppet 能够确保系统始终处于期望的配置状态。
357 3
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1212 0

热门文章

最新文章

推荐镜像

更多