1.puppet apply单机基础:
参考:http://www.zsythink.net/archives/363
目标:使用puppet apply在单机上实现文件同步,安装软件,开关服务,执行shell。
在/etc/puppet/manifests下生成test1.pp文件,并进行编辑:
#将resolve.erb文件同步到/etc/resolv.conf
file{'dns1':
path => '/etc/resolv.conf',
ensure => file,
source => '/etc/puppet/manifests/resolve/resolve.erb',
mode => 0644,
}
#安装tree软件
package{'install':
name => 'tree',
ensure => 'installed',
}
#开启ntpd服务
service{"stop":
name => 'ntpd',
ensure => 'running',
}
#检测是否存在/etc/resol.conf文件,如果有,删除
exec{'del':
path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin',
cwd => '/etc',
command => 'rm -f resol.conf',
onlyif => 'test -e /etc/resol.conf'
}
2.使用puppet安装httpd服务的配置文件:
系统为centos6.5,puppet版本3.8.7。server侧共有4个配置文件,其中主配置文件/etc/puppet/manifests/site.pp相关配置如下:
node 'puppetclient2'{
include httpd
}
/etc/puppet/modules/httpd/manifests下有init.pp install.pp service.pp 3个配置文件。
[[email protected] manifests]# cat init.pp
class httpd{
include httpd::install
include httpd::service
}
[[email protected] manifests]# cat install.pp
class httpd::install{
package{'httpd':
ensure => present,
}
}
[[email protected] manifests]# cat service.pp
class httpd::service{
service {'httpd':
ensure => running,
hasstatus => true,
hasrestart => true,
enable => true,
require => Class["httpd::install"],
}
}
在agent上进行验证:
[[email protected] useless]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppetclient2
Info: Applying configuration version '1551171577'
Notice: /Stage[main]/Httpd::Install/Package[httpd]/ensure: created
Notice: /Stage[main]/Httpd::Service/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Httpd::Service/Service[httpd]: Unscheduling refresh on Service[httpd]
Notice: Finished catalog run in 5.84 seconds
httpd服务已经启动了:
[[email protected] useless]# /etc/init.d/httpd status
httpd (pid 5598) is running...
网上找到的一段话:
一个模块目录下面通常包括三个目录:files,manifests,templates。manifests 里面必须要包括一个init.pp的文件,这是该模块的初始(入口)文件,导入一个模块的时候,会从init.pp开始执行。可以把所有的代码都写到init.pp里面,也可以分成多个pp文件,init 再去包含其他文件。files目录是该模块的文件发布目录,puppet提供一个文件分发机制,类似rsync的模块。templates 目录包含erb模型文件,这个和file资源的template属性有关。