转载:http://blog.51cto.com/ywzhou/1577299
Puppet模块章节环境说明
服务端 | 客户端
操作系统:CentOS 6.5 x86_64 | 操作系统:CentOS 6.5 x86_64
Hostname:puppet.ewin.com | Hostname:centostest.ewin.com
IP:10.188.1.73 | IP:10.188.1.85
Puppet-Server:puppet-3.7.1 | Puppet-Agent:puppet-3.7.1
作用:为客户端添加计划任务,每两小时同步系统时间
1、服务端配置NTP模块
(1)模块清单
[root@puppet ~]# yum -y install tree
[root@puppet ~]# tree /etc/puppet/modules/ntp/
/etc/puppet/modules/ntp/
├── files
├── manifests
│ └── init.pp
└── templates
(2)定义ntp类
[root@puppet ~]# vi /etc/puppet/modules/ntp/manifests/init.pp
class ntp {
cron { "ntpdate": #系统已默认安装ntpdate,也可以定义package来确保安装ntpdate包
command => "/usr/sbin/ntpdate 65.55.56.206",
ensure => present,
user => root,
hour => '*/2', #每两小时同步一次
minute => 0,
}
}
说明:保持良好的代码编写习惯,注意缩进和=>对齐
(3)定义节点文件,调用模块
[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
node "centostest.ewin.com" {
include ntp
}
(4)应用节点文件
[root@puppet ~]# vi /etc/puppet/manifests/site.pp
import "centostest.pp"
说明:以上三个文件,一个调用一个;定义类>>定义节点(调用类)>>调用节点
2、客户端测试puppet
(1)测试执行puppet
[root@centostest ~]# puppet agent --server puppet.ewin.com --test --noop
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for centostest.ewin.com
Info: Applying configuration version '1415239491'
Notice: /Stage[main]/Ntp/Cron[ntpdate]/ensure: current_value absent, should be present (noop)
Notice: Class[Ntp]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.15 seconds
说明:--noop参数表示不真正执行,只是测试puppet模块应用成功与否
(2)查看计划任务
[root@centostest ~]# crontab -e
HEADER: This file was autogenerated at Thu Nov 06 10:09:29 +0800 2014 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
0 */2 * * * /usr/sbin/ntpdate 65.55.56.206
说明:已成功添加任务,从头部注释可以看出是由puppet添加的
3、客户端测试时间同步
让时间后退1分钟,2小时后查看时间是否同步,嫌长可改成每5分钟同步一次来测试
[root@centostest ~]# date
2014年 11月 06日 星期四 10:33:08 CST
[root@centostest ~]# date -s 10:32:00
2014年 11月 06日 星期四 10:32:00 CST
结论:2小时后查看时间,与正常时间已成功同步
4、Cron计划任务资源参数说明
cron { 'ntpdate'
command => "/usr/sbin/ntpdate ntpserver.domain.com",
user => root,
hour => 6, #每天早上6:00同步时间
minute => 0,
require => Package[ntp], #要求软件包ntp已安装
}
{
每分钟可表示为
minute => '*/1',
每2小时可表示为
hour => '*/2',
minute => 0,
晚11点到早8点间每2小时以及8点整执行同步
hour => '23-7/2,8',
minute => 0,
其他参数
ensure => present|absent, #基本属性present表示启用,absent表示禁用
command => ,#需要执行的命令
environment => PATH=/bin:/usr/bin:/usr/sbin, #运行的环境变量配置
hour => , #第几小时,0~23
minute => , #第几分钟,0~59
month => , #某月,1~12
monthday => , #月中某天,1~31
weekday => , #周中某天,0~7
name => , #名称,一般用标题代替
user => , #指定运行的用户,默认为root
target => , #指定用户的cron项,默认为用户的shell
special => , #特殊的配置
}