puppet之crontab時間同步

wang_0720發表於2013-12-04
       puppet很重要的一點就是時間同步,所以在搭建puppet伺服器時首先要保證每臺客戶端和伺服器的時間要一致。可以在puppet伺服器上配置一個cron模組,所有客戶端每隔數分鐘同步一次時間,時間伺服器可以是任何一個可用的ntp,本文用的是上海交大的時間伺服器ntp.sjtu.edu.cn。
新建一個cron模組
[root@master modules]# tree cron/
cron/
`-- manifests
    |-- addcron.pp
    |-- base.pp
    `-- init.pp

1 directory, 3 files
[root@master modules]#
定義新增cron類,指明同步的時間伺服器,更新頻率。
[root@master manifests]# cat addcron.pp
class cron::addcron {
    cron {
       ntpdate:
       command => "/sbin/ntpdate ntp.sjtu.edu.cn",
       ensure => "present",
       user=> "root",
       minute => "*/5",
    }
}
定義cron服務類,crond服務要保持在執行狀態
[root@master manifests]# cat base.pp
class cron::base {
 service {
    crond:
    ensure => running,
    enable => true,
    pattern => cron,
    }
}
初始化
[root@master manifests]# cat init.pp
class cron {
    include cron::base
    include cron::addcron
}
匯入cron模組
[root@master puppet]# cat /etc/puppet/manifests/modules.pp
import "cron"
......
在每個節點檔案中,引入cron類
[root@master puppet]# cat /etc/puppet/manifests/nodes.pp
node 'agent1.andy.com' {
include cron
include java
include mysql
}
node 'agent2.andy.com' {
include cron
include java
include mysql
}

node 'agent3.andy.com' {
include cron
include java
include mysql
}
客戶端手動執行puppet agent --server master.andy.com --test 或配置定時更新可以在每個客戶端都新增一個crontab。
客戶端定時同步更新配置如下,每10分鐘同步更新一次
[root@agent1 ~]# cat /etc/puppet/puppet.conf
[main]
    # The Puppet log directory.
    # The default value is '$vardir/log'.
    logdir = /var/log/puppet

    # Where Puppet PID files are kept.
    # The default value is '$vardir/run'.
    rundir = /var/run/puppet

    # Where SSL certificates are kept.
    # The default value is '$confdir/ssl'.
    ssldir = $vardir/ssl
    server = master.andy.com
[agent]
    # The file in which puppetd stores a list of the classes
    # associated with the retrieved configuratiion.  Can be loaded in
    # the separate ``puppet`` executable using the ``--loadclasses``
    # option.
    # The default value is '$confdir/classes.txt'.
    classfile = $vardir/classes.txt

    # Where puppetd caches the local configuration.  An
    # extension indicating the cache format is added automatically.
    # The default value is '$confdir/localconfig'.
    localconfig = $vardir/localconfig
    runinterval = 600
[root@agent1 ~]#
結果如下
[root@agent1 ~]# crontab -l
# HEADER: This file was autogenerated at Mon Dec 02 12:20:16 +0800 2013 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
*/5 * * * * /sbin/ntpdate ntp.sjtu.edu.cn

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27181165/viewspace-1062118/,如需轉載,請註明出處,否則將追究法律責任。