puppet设置
1,文件和目录
服务器端
修改配置文件fileserver.conf
[root@server puppet]# vim fileserver.conf
# This file consists of arbitrarily named sections/modules
# defining where files are served from and to whom
# Define a section 'files'
# Adapt the allow/deny settings to your needs. Order
# for allow/deny does not matter, allow always takes precedence
# over deny
# [files]
# path /var/lib/puppet/files
# allow *.example.com
#可以设置域名访问
# deny *.evil.example.com
# allow 192.168.0.0/2
#可以设置ip段访问
[puppettest]
#设置模块名字:puppettest
path /tmp/
#设置模块路径
allow *
#用allow,deny来限制使用者,这里设置*允许所有人访问。
重启服务端服务
[root@server ~]# /etc/init.d/puppetmaster start
配置文件同步
[root@server ~]# vim /etc/puppet/manifests/site.pp
node 'client.puppet.com' {
#设置生效节点,client.puppet.com
file {"/tmp/temp.txt" :
#定义同步temp.txt文件,资源tite和path可以合并表示。
source => "puppet://server.puppet.com/puppettest/temp.txt",
#定义数据源,其中server.puppet.com为服务器的主机名,puppettest为文件服务器中设置的模块名,temp.txt为最终更新的文件名。
#puppet://server.puppet.com /puppettest /temp.txt
# 在fileserver.conf中定义的主机名或是网段。 在site.pp中定义的模块名,puppettest代表path定义的路径。 用户要更新的文件
# source => "http://192.168.2.129/html/index.html"
# source => "/home/xuefeng/temp.txt"
#用客户机上的源,更新/tmp/temp.txt
backup =>
".back_$uptime_seconds",
#这是可选项,在替换文件时可以对源文件进行备份,“.bak”以点开头;$uptime_seconds是前文安装的facter软件提取的变量,选开机秒数做命名,假如当前开机为1000秒,备份后的/tmp/temp.txt的全名应该为/tmp/temp.txt.bak_1000.
group => root,
#设置文件属组
owner => root,
#设置文件属主
mode => 644;
#设置文件权限
}
$aaa = "this is tesst!"
file{
"/tmp/testfile1":
content => $aaa;
#调用变量
}
file{
"/tmp/testfile2":
ensure=>"/etc/hosts";
#将hosts做个软连接
}
file{
"/tmp/testfile3":
ensure=>absent;
#检测testfile3是否存在,如果存在则删除。
}
file{
"/tmp/testfile4":
ensure=>present;
#检测testfile4是否存在,如果不存在则创建。
}
file{
"/tmp/testdir1":
ensure=>directory;
#创建目录testdir1。
}
file{
"/tmp/testdir2":
ensure=>absent,
#删除目录testdir2
force=>true;
#删除目录testdir2,及其下得子目录和文件。
}
file{
"/tmp/testdir3":
ensure=>directory,
#定义这是个必须存在的目录
source=>"puppet://server.puppet.com/tmp",
#指定数据源
ignore=>'*log*',
#不同步log目录和文件
recurse=>true,
#是否递归到子目录,必选。
purge=>true,
#是否要删除服务器没有而客户端有得文件,可选。
force=>true;
#配合上文,如果要删除的事子目录需要加上此选项
}
}
客户端
[root@client ~]# puppetd --server server.puppet.com --test
#执行同步
查看变化!
2,包的安装
服务器端
node 'client.puppet.com' {
package{"mysql":
#设定软件包名字
ensure => "installed"
#设置软件包得状态,installd(present)表示要安装该软件,absent表示反安装该软件,pureged表示干净的移除该软件,latest表示安装软件包的最新版本
}
}
node 'client.puppet.com' {
package{"abc":
#设定软件包名字
ensure => "present"
#设置软件包得状态,installd(present)表示要安装该软件,absent表示反安装该软件,pureged表示干净的移除该软件,latest表示安装软件包的最新版本
provider => rpm,
#指定安装方式
source => "puppet://server.puppet.com/package/abc.rpm"
#此处设置和文件同步的设置格式一样,将包放在对应的目录里,客户端会从服务器中下载并安装
}
}
重启服务端服务
[root@server ~]# /etc/init.d/puppetmaster start
客户端
同步操作:
[root@client puppet]# puppetd --server server.puppet.com --test
info: Caching catalog for client.puppet.com
info: Applying configuration version '1322562163'
notice: /Stage[main]//Node[client.puppet.com]/Package[mysql]/ensure: created
notice: Finished catalog run in 160.48 seconds
查看结果
[root@client puppet]# rpm -qa mysql
mysql-5.0.77-4.el5_6.6
3,服务管理
服务端
[root@server manifests]# vim site.pp
node 'client.puppet.com' {
service {
"crond":
#指定需要开启的服务,名称一定要和/etc/init.d/下对应的服务名一致。
ensure => "running";
#设定服务的状态,running(true)为运行,stopped(false)为停止,restart重启,status服务状态,
}
}
重启服务端服务
[root@server ~]# /etc/init.d/puppetmaster start
客户端
同步操作
[root@client ~]# puppetd --server server.puppet.com --test
info: Caching catalog for client.puppet.com
info: Applying configuration version '1322619214'
notice: /Stage[main]//Node[client.puppet.com]/Service[crond]/ensure: ensure changed 'stopped' to 'running'
notice: Finished catalog run in 0.16 seconds
查看结果
[root@client ~]# ps -ef|grep crond
root 29705 1 0 10:13 ? 00:00:00 crond
root 29708 3507 0 10:13 pts/0 00:00:00 grep crond
4,exec资源管理
服务端
[root@server manifests]# vim site.pp
node 'client.puppet.com' {
file {"/tmp/nginx-1.0.10.tar.gz" :
source => "puppet://server.puppet.com/nginx/nginx-1.0.10.tar.gz",
recurse => true,
before => Exec["nginx-tar"]
#before用于控制不同对象(资源)的执行顺序,表示某个对象(资源)在另一个对象之后发生,(require与之相反,它表示之前发生)
}
Exec{path => ["/bin/","/sbin/","/usr/bin/","/usr/sbin/","/usr/local/bin/","/usr/local/sbin/"]}
#定义命令的路径,在command中就不用把全路径写上了,如果某些命令不在定义的路径中,还需要写全路径。
exec{"nginx-tar":
command => "/bin/tar zxvf /tmp/nginx-1.0.10.tar.gz",
}
}
#以上实现将nginx从服务器端复制到客户端,并解压。
重启服务端服务
[root@server ~]# /etc/init.d/puppetmaster start
例子:
客户端
执行同步
[root@client ~]# puppetd --server server.puppet.com --test
info: Caching catalog for client.puppet.com
info: Applying configuration version '1322624504'
notice: /Stage[main]//Node[client.puppet.com]/File[/tmp/nginx-1.0.10.tar.gz]/ensure: defined content as '{md5}930b297b00fa1018fb0a1dd3e6b7e17e'
notice: /Stage[main]//Node[client.puppet.com]/Exec[nginx-tar]/returns: executed successfully
notice: Finished catalog run in 1.44 seconds
查看结果
[root@client ~]# ls /tmp/
nginx-1.0.10 nginx-1.0.10.tar.gz
5,cron
服务器端
添加模块,目录结构如下:
/etc/puppet |
|-->manifests
| |-->modules.pp
| |-->nodes.pp
| |-->site.pp
|-->modules
|-->cron
|-->manifests
|-->addcron.pp
|-->base.pp
|-->crontabs.pp
|-->init.pp
建创目录结构
#mkdir -p /etc/puppet/manifests
#mkdir -p /etc/puppet/modules/cron/manifests
#cd /etc/puppet/modules/cron/manifests
(1)、编写base.pp
功能说明:安装cron包并启动服务
#vim base.pp
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 'client.puppet.com'{
include cron
}
#vim site.pp #加载modules.pp 和nodes.pp
import "modules.pp"
import "nodes.pp"
重启服务端服务
[root@server ~]# /etc/init.d/puppetmaster start
客户端
执行同步
[root@client ~]# puppetd --server server.puppet.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
参考:
http://docs.puppetlabs.com/ 基础文档
http://www.mysqlops.com/2011/09/05/puppet%E8%BF%90%E7%BB%B4%E8%87%AA%E5%8A%A8%E5%8C%96%E4%B9%8Bhost%E4%B8%BB%E6%9C%BA%E7%AE%A1%E7%90%86.html
http://www.linux521.com/2009/system/201103/13780_3.html
http://os.51cto.com/art/201012/240992.htm
http://zhumeng8337797.blog.163.com/blog/static/100768914201172952926171/
http://dongxicheng.org/cluster-managemant/puppet/
http://puppet.chinaec2.com/