本文共 3022 字,大约阅读时间需要 10 分钟。
用puppet管理cron,可以为N台服务器批量添加cron作业。如定时同步时间服务器的脚本。
1、架构192.168.0.200(redhat) 主机名master.gangpao.com
192.168.0.201(redhat) 主机名agent01.gangpao.com 2、目录结构/etc/puppet
|
|-->manifests | |-->modules.pp | |-->nodes.pp | |-->site.pp |-->modules |-->cron |-->manifests |-->addcron.pp |-->base.pp |-->crontabs.pp |-->init.pp3、编写脚本
建创目录结构#mkdir -p /etc/puppet/manifests
#mkdir -p /etc/puppet/modules/cron/manifests #cd /etc/puppet/modules/cron/manifests(1)、编写base.pp
功能说明:安装cron包并启动服务#vim class cron::base { package { cron: name => $operatingsystem ? { #facter 获取客户端操作系统确定包的名称 ubuntu => "cron", debian => "cron", redhat => "vixie-cron", centos => "vixie-cron", }, ensure => present, } service { crond: name => $operatingsystem ? { #确定启动cron的名称 ubuntu => "cron", debian => "cron", redhat => "crond", centos => "crond", }, ensure => running, enable => true, pattern => cron, require => Package["cron"], #依赖关系 }}(2)、编写crontabs.pp 功能说明:安装crontabs包
#vim crontabs.pp
class cron::crontabs { package { crontabs: name => $operatingsystem ? { redhat => "crontabs", centos => "crontabs", }, ensure => present, } } (3)、编写addcron.pp 功能说明:添加crontab定时任务(定时同步时间服务器每4个小时同步的第1分钟同步)#vim addcron.pp
class cron::addcron { cron { ntpdate: command => "/usr/sbin/ntpdate time-b.timefreq.bldrdoc.gov", user => root, hour => '*/4', minute => '1' } } (4)、编写init.pp 功能说明:初始化文件#vim init.php
class cron { case $operatingsystem { centos: { include cron::base include cron::crontabs } redhat: { include cron::base include cron::crontabs include cron::addcron #这三行实现安装添加cron功能 } debian: { include cron::base } ubuntu: { include cron::base } freebsd: { } } } (5)、编写主manifests文件实现让客户端安装cron和添加一条crontab作业。#cd /etc/puppet/manifests
#vim modules.pp #加载cron模块 import "cron" #vim nodes.pp #客户端节点管理文件 node 'agent01.gangpao.com'{ include cron } #vim site.pp #加载modules.pp 和nodes.pp import "modules.pp" import "nodes.pp" 4、执行脚本 服务端#puppetmasterd -d --no-daemonize -v --trace
客户端#puppet agent --server master.gangpao.com --test
info: Caching catalog for agent01.gangpao.com info: Applying configuration version '1310314253' notice: /Stage[main]/Cron::Addcron/Cron[ntpdate]/minute: minute changed '0' to '1' notice: /Stage[main]/Cron::Addcron/Cron[ntpdate]/hour: hour changed '*/2' to '*/4' notice: Finished catalog run in 6.65 seconds
查看
[root@agent01 puppet]# crontab -l
# HEADER: This file was autogenerated at Mon Jul 11 00:10:19 +0800 2011 by puppet. # HEADER: While it can still be managed manually, it is definitely not recommended. # HEADER: Note particularly that the comments starting with 'Puppet Name' should # HEADER: not be deleted, as doing so could cause duplicate cron jobs. # Puppet Name: ntpdate 1 */4 * * * /usr/sbin/ntpdate time-b.timefreq.bldrdoc.gov
可以看到客户端的crontab改变了。
本文转自yuangang_love 51CTO博客,原文链接:http://blog.51cto.com/linux008/607854,如需转载请自行联系原作者