Use puppet to manage cron. You can add cron jobs to N servers in batches. For example, the script of the timed synchronization time server.
1. Architecture
192.168.0.200 (redhat) Host Name master.gangpao.com
192.168.0.201 (redhat) Host Name agent01.gangpao.com
2. directory structure
/Etc/puppet
|
| --> Manifests
| --> Modules. pp
| --> Nodes. pp
| --> Site. pp
| --> Modules
| --> Cron
| --> Manifests
| --> Addcron. pp
| --> Base. pp
| --> Crontabs. pp
| --> Init. pp
3. write scripts
Create a directory structure
# Mkdir-p/etc/puppet/manifests
# Mkdir-p/etc/puppet/modules/cron/manifests
# Cd/etc/puppet/modules/cron/manifests
(1) Compile base. pp
Function Description: Install the cron package and start the service.
# Vim
Class cron: base {
Package {cron:
Name => $ operatingsystem? {# Facter get the name of the package determined by the client operating system
Ubuntu => "cron ",
Debian => "cron ",
Redhat => "vixie-cron ",
Centos => "vixie-cron ",
},
Ensure => present,
}
Service {crond:
Name => $ operatingsystem? {# Determine the name of the startup cron
Ubuntu => "cron ",
Debian => "cron ",
Redhat => "crond ",
Centos => "crond ",
},
Ensure => running,
Enable => true,
Pattern => cron,
Require => Package ["cron"], # dependency
}
}
(2) Compile crontabs. pp
Function Description: Install the crontabs package.
# Vim crontabs. pp
Class cron: crontabs {
Package {crontabs:
Name => $ operatingsystem? {
Redhat => "crontabs ",
Centos => "crontabs ",
},
Ensure => present,
}
}
(3) Write addcron. pp
Function Description: Add a crontab scheduled task (the timer synchronization time server synchronizes 1st minutes every 4 hours)
# Vim addcron. pp
Class cron: addcron {
Cron {ntpdate:
Command => "/usr/sbin/ntpdate time-b.timefreq.bldrdoc.gov ",
User => root,
Hour => */4,
Minute => 1
}
}
(4) Compile init. pp
Function Description: initialize a file
# Vim init. php
Class cron {
Case $ operatingsystem {
Centos :{
Include cron: base
Include cron: crontabs
}
Redhat :{
Include cron: base
Include cron: crontabs
Include cron: addcron # Install and add cron
}
Debian: {include cron: base}
Ubuntu: {include cron: base}
Freebsd :{}
}
}
(5) Compile the main manifests file to allow the client to install cron and add a crontab job.
# Cd/etc/puppet/manifests
# Vim modules. pp # load the cron Module
Import "cron"
# Vim nodes. pp # client node File Management
Node agent01.gangpao.com {
Include cron
}
# Vim site. pp # Load modules. pp and nodes. pp
Import "modules. pp"
Import "nodes. pp"
4. Execute the script
Server
# Puppetmasterd-d -- no-daemonize-v -- trace
Client
# 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
View
[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 particle ly that the comments starting with Puppet Name shold
# HEADER: not be deleted, as doing so coshould cause duplicate cron jobs.
# Puppet Name: ntpdate
1 */4 ***/usr/sbin/ntpdate time-b.timefreq.bldrdoc.gov
The crontab of the client is changed.