puppet项目实施计划之(crond与时间同步配置篇)

用puppet管理cron,可以为N台服务器批量添加cron作业。如定时同步时间服务器的脚本。

一、服务端

服务端操作

1编写脚本,建创目录结构

cd/etc/puppet

midair-p modules/cron/manifests/

2、vi addcron.pp

classcron::addcron {

            cron { ntpdate:

               command =>"/usr/sbin/ntpdate pool.ntp.org",

               user => root,

               hour => '0',

               minute => '0'

            }

      }

3编写base.pp
    功能说明:安装cron包并启动服务

vibase.pp

classcron::base {

    package { cron:

        name => $operatingsystem ? {   #facter         

           ubuntu    => "cron",

            debian    => "cron",

            redhat    => "cron",

            centos    => "cron",

            },

        ensure => present,

    }

    service { crond:

        name => $operatingsystem ? {             ubuntu  => "cron",

            debian  => "cron",

            redhat  => "crond",

            centos  => "crond",

            },

        ensure => running,

        enable => true,

        pattern => cron,

        require =>Package["cron"], 

    }

}

 

4编写crontabs.pp
     功能说明:安装crontabs包

vicrontabs.pp

classcron::crontabs {

         package { crontabs:

              name => $operatingsystem ? {

              redhat => "crontabs",

              centos  => "crontabs",

              },

              ensure => present,

         }

      }

 

5编写init.pp
       功能说明:初始化文件

viinit.pp

 

classcron {

             case $operatingsystem {

                 centos: {

                    include cron::base

                    include cron::crontabs

                    include cron::addcron    

                 }

                  redhat: {

                    include cron::base

                    include cron::crontabs

                    include cron::addcron    

                 }

                 debian: { include cron::base }

                 ubuntu: { include cron::base }

                 freebsd: { }

             }

       }

 

6编写主manifests文件实现让客户端安装cron和添加一条crontab作业。

cd/etc/puppet/mainfests

 vi modules.pp    #加载cron模块

import"cron"

vinodes.pp      #客户端节点管理文件

node'client176.puppet.com'{

      include cron

      }

visite.pp       #加载modules.pp 和nodes.pp

import"modules.pp"

import"nodes.pp"

二、客户端

1、在客户端连接服务器,看能否同步

[root@agent~]# puppet agent --server master.kylinyunwei.com --test

notice:Ignoring --listen on onetime run

info:Caching catalog for agent.kylinyunwei.com

info:Applying configuration version '1414049769'

err:/Stage[main]/Cron::Base/Package[cron]/ensure: change from absent to presentfailed: Execution of '/usr/bin/yum -d 0 -e 0 -y install cron' returned 1:Error: Nothing to do

 

notice:/Stage[main]/Cron::Base/Service[crond]: Dependency Package[cron] has failures:true

warning:/Stage[main]/Cron::Base/Service[crond]: Skipping because of failed dependencies

notice:/Stage[main]/Cron::Addcron/Cron[ntpdate]/ensure: created

notice:Finished catalog run in 1.27 seconds

出现created则为成功

2、在客户端查看计划任务

[root@agent~]# crontab -l

#HEADER: This file was autogenerated at Thu Oct 23 15:36:12 +0800 2014 bypuppet.

#HEADER: While it can still be managed manually, it is definitely notrecommended.

#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

3、出现如上所示则说明成功同步,可以看到客户端的crontab改变了。