Puppet常用资源:
常用的资源主要有以下几个:
file:主要负责管理文件
package:软件包的安装管理
service:系统服务的管理
cron:配置自动任务计划
exec:远程执行运行命令
【验证puppet配置】
1、file资源
在服务端写个例子测试一下。这个例子作用很简单,用来在客户端的/tmp目录下新建一个 test.txt 文件,内容为:hello,test!在服务端编写代码:【服务器端不需要新建这个文件】
vi /etc/puppet/manifests/site.pp
file {
“/tmp/test.txt”:
content => “helo,test!”;
}
在客户端上执行 puppet,运行成功后会在 /tmp 看到新生成的 test.txt:
[root@slave9 tmp]# puppet agent --test --server=master
info: Caching catalog for slave9
info: Applying configuration version '1373006825'
notice: /Stage[main]//Node[default]/File[/tmp/test.txt]/ensure: defined content as '{md5}002c9dbca1fe0e6339dc7a2cbb6f776d'
notice: Finished catalog run in 0.02 seconds
最后查看cat /tmp/test.txt
hello,test!
2、Package资源:
【package资源】远程安装服务
package {
["ntp"]:
ensure => "installed";
"screen":
ensure => "absent";
}
定义的意思是yum install ntp服务,并且卸载screen安装包。
客户端执行更新:
info: Applying configuration version '1373268238'
notice: /Stage[main]//Package[screen]/ensure: removed
notice: /Stage[main]//Package[ntp]/ensure: created
notice: Finished catalog run in 33.36 seconds
【service资源】 针对服务的重启
service {
"sshd":
ensure => running;
"nfs":
ensure => stopped;
}
意思是定义启动sshd服务,停止nfs服务。
【Cron资源】
cron{
"ntpdate":
command => "/usr/sbin/ntpdate pool.ntp.org",
user => root,
hour => 0,
minute => 0,
意思是在客户端写入一个计划任务:0 0 * * * /usr/sbin/ntpdate pool.ntp.org 自动同步时间!
info: Applying configuration version '1373269123'
notice: /Stage[main]//Cron[ntpdate]/ensure: created
notice: Finished catalog run in 0.17 seconds
【其他】
1、每隔3小时执行 hour => "*/3",
2、如果客户端的crond是这样定义:19 0 * * *,想要改成19 * * * *
hour => "*",
【puppet推送】
向客户端推送本地脚本:
首先修改vi /etc/puppet/fileserver.conf 文件,添加如下三行:
[share] --模块名字
path /etc/puppet/files
allow *
##注意files后不能有空格,否则重启puppetmaster的时候会报错,我不小心在这折腾了很久(╯﹏╰)
然后新建/etc/puppet/files这个目录,然后cp所需要的东西到files目录
file {
"/tmp/nginx_install.sh": --nginx_install.sh脚本会拷贝到/tmp目录下
source => "puppet:///share/nginx_install.sh",
group => root,
owner => root,
mode => "755"
}
需要注意几个地方,puppet:///share/nginx_install.sh,而不应该写puppet://master/files/nginx_install.sh
recurse => "true", ##传送该目录下所有文件 【需要测试】
意思是把 /etc/puppet/files/nginx_install.sh这个脚本推送到客户端的/tmp/下!
http://kexl908.blog.51cto.com/605006/901767
【注意】
1、可以把多个file资源的title写在一起,前提是file资源在客户端必须先存在
["/tmp/test.txt", "/tmp/test1", "/tmp/test2.txt"]:
owner => "test",
group => "test",
mode => 777,
【远程执行命令】
如第五步,我们把nginx_install.sh推送过去后,这时候我们就可以执行了如下:
exec {
"/tmp/1.sh":
cwd => "/tmp",
command => "sh /tmp/1.sh",
user => root,
path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],
【配置文件更新】
例如更新/etc/sysctl.conf这个配置文件,先在主上,拷贝/etc/sysctl.conf到/etc/puppet/files,然后对内核配置文件进行修改,
然后修改site.php
file { "/etc/sysctl.conf":
source => "puppet:///share/sysctl.conf",
owner => "root",
group => "root",
mode => 644,
}
接着在从上面直接更新就行了
最后要让修改好的配置文件生效,还需要如下操作
"sysctl refresh kernel config":
path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],
command => "/sbin/sysctl -p",
subscribe => File["/etc/sysctl.conf"],
refreshonly => true
}
需要注意:他们之间不能用冒号,两边的框框不能去掉
path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],
这2个必须要写在一起,不能把exec单独放
【资源依赖】 --A依赖B,如果A没有的话,则B不会被执行
【资源依赖】
资源A依赖资源B,如果资源B不存在,资源A就不被执行。定义资源依赖的属性是require。
class base::cron
{
package { "crontabs":
ensure => installed,
}
service {
"crond":
ensure => running,
enable => true,
require => Package["crontabs"];
cron { ntpdate:
command => "/usr/sbin/ntpdate time.nist.gov 210.72.145.44 64.147.116.229 ",
user => root,
hour => 0,
minute => 0,
require => Package["crontabs"];
前提:cron已经被安装,否则后面的都不执行