puppet常用资源说明
cron //任务计划exec //执行脚本
file //定义文件
group //定义组
host //定义host性息
package //定义安装包
service //定义服务状态
user //定义用户
mount //定义mount状态
sshkey //定义密钥
ssh_authorized_key //定义密钥
yumrepo //定义yum源
tidy
schedule
augeas
-----------------------------------------------------------------------------------------------
user 用户管理
支持类型
- allow duplicates 允许用户拥有相同的uid
- manages expiry 管理用户过去日期
- manages homedir 管理用户家目录
- system users 共过低UID值的方式创建
通过password属性,但是默认不加密的,所有最好填写加密过后的密码
manage home仅相当与useradd -m 参数,不保证家目录一直存在
例子:
user { "tomcat":
ensure => present,
uid => 501,
allowdupe => true,
groups => 'tomcat',
managehome => true,
home => "/home/tomcat",
shell => "/bin/bash"
expiry=> "2012-11-12",
}
---------------------------------------------------------------------------------------------------
group 组管理
在多台机器环境下,规定UID和GID,避免同用户名不同UID,GID情况
system为true并且gid没有制定的时候,puppet默认给组一个低于500的gid。
例子:
group { "ntp":
ensurce => present,
gid => 15,
allowdupe => false,
system => true,
}
------------------------------------------------------------------------------------------------------------
file 文件管理
包含3中文件类型
- 文件
- 链接文件
- 文件夹
包含3种文件内容源
- content => "something",
- source => "puppet://puppet.xxx.com/xxxx"
- content => template("modulesname/templatefilename")
注意,filed的宿主在puppet中管理,file资源自动依赖。如果file的父目录也被管理着,也会自动依赖。
文件夹资源定义中 ,mode的644=755,
例子:
file {"test.sh": //定义执行的脚本
source=>"puppet:///modules/memcached/test.sh", //脚本的位置,需要从服务器下载
path=>"/tmp/yum/test.sh", //传输到客户端的位置
ensure=>file, //定义是一个文件
owner=>"root", //文件拥有者
mode=>775,
group=>"root",
}
或者。
file {"httpd.conf":
content =>template("apache/httpd.conf.erb"),
path =>"/etc/httpd/conf/httpd.conf",
require =>PACKAGE["httpd"],
ensure=>present,
}
或者,
file { "/root/install.log": //定了一个文件
owner => "puppet", //文件的属性:拥有用户
group => "puppet", //文件的用户 组
mode => 777, //文件的模式
}
-------------------------------------------------------------------------------------------------------------------
package 软件包管理
常用的管理方式:
yum/apt
rpm,gem等
自动依赖
ensure的方法
present,installed,latest,absent,purged,held,版本
source的地址必须是本地路径或者包管理可以理解的url。
package { "httpd":
ensure => "5.4.54"
}
package { "rak":
ensure => installed,
source => "/home/ruby/rack-1.2.1.gem",
}
--------------------------------------------------------------------------------------------------------------------------
service 服务管理
redhat中默认使用service命令和/etc/init.d/目录中的服务
puppet都期望init脚本提供状态。
没有状态需要在hasstatus设置为false或者制定status的属性命令。
service从其他资源收到事件消息后将会重启。
-重启命令用restart属性来制定
-使用hasrestart为true来使用init脚本来重启(现在默认为true,执行失败后会使用stop/start方式)
-如果不设置,使用stop/start方式
例子:
service { “ntp”:
enable true, ensure => running,
hasrestart => false,
path => “/etc/init.d”,
}
或者
service { "tomcat":
ensure => running,
enable => true,
start => "$tomcat_home/bin/catalina.sh start",
stop => "$tomcat_home/bin/catalina.sh stop"
hasrestart => false,
hasstatus => false,
}
------------------------------------------------------------------------------------------------------------------------------
exec 执行外部命令
能不用就不用,能少用就少用
不设置阀值,加载一次就会执行一次
exec的不执行条件。
- creates 文件/目录不存在时才执行
- refreshonly 接受到刷新时间后才执行
- onlyif 仅仅这个属性命令返回0时,才执行
- unless 这个属性命令执行不成功,返回不为0才执行
exec中命令可能不会每次执行,但是onlyif/unless中会,所有经理写的简单。
puppet在执行onlyif/unless命令时候,会调用系统的sh。所有使用ps查看系统使用进程,要过滤掉sh。
例如:
exec { "test.sh": //执行的命令
# cwd=>"/tmp/yum/",
creates => "/root/a",
command=>"/bin/bash /tmp/yum/test.sh", //命令
path=>"/usr/local/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin", //定义环境变量PATH
require=>FILE["test.sh"], //需要上面file中test.sh文件,如果文件已经在客户端就需要,不再的话,就不需要。每次执行同步都会默认去执行该shell。
}
exec { "nginx-s>
user => "root",
refreshonly => true,
onlyif=> "nginx -t /etc/nginx.conf",
tries => 3,
try_sleep => 5,
}
------------------------------------------------------------------------------------------------------------------------------------------------
cron 脚本
任务保存在/var/spool/cron/username
不指定用户默认为root,
例子:
cron {"cron-shell": //定义个crontab
command=>"sh /opt/cloudiya/test.sh", //定义命 令
user=>"root", //用户
minute=>"40", //时间
hour=>"15", //小时
#ensure=>absent, //不用选择,如果需要删除的话,使用
}
------------------------------------------------------------------------------------------------------------------------------------------------
host 管理主机
包括主机名,ip,别名,注释
host { “puppet.xxx.com”:
ensure =>present,
ip =>“172.16.0.1”,
host_aliases [“puppet”,”master”],
comment => "puppet master",
}
--------------------------------------------------------------------------------------------------------------------
mount 管理挂载
-mount 资源收到notify事件后,ensure是mounted,它尝试重新挂载
-ensure
--umounted 确保文件系统存在fstab中但是未挂载
-- absent 确保此文件系统在fstab中不存在
-- mounted, 确保存台fstab中并且已经挂载
-- present 区别它在fstab中不改变状态。
例子:
mount {"/home/apache/download":
atboot => false,
ensure => mounted,
name => "fileserver:/download",
fstype => "nfs",
options => "ro",
}
--------------------------------------------------------------------------------------------------------------------
yumrepo yum源管理
yumrepo { "IUS":
baseurl => "http://dl.iuscommunity.org/pub/ius/stable/
$operatingsystem/$operatingsystemrelease/$architecture",
descr => "IUS Community repository",
enabled => 1,
gpgcheck => 0
}
---------------------------------------------------------------------------------------------------------------------
sshkey 管理主机的key
只会安装key到/etc/ssh/ssh_kown_host文件中
sshkey {"web001":
ensure => present,
key => "密钥",
type => "ssh-dss",
}
-------------------------------------------------------------------------------------------------------------------------------
Ssh_authorized_key
管理SSH授权密钥
ssh_authorized_key { “hostname”:
ensure => present,
type => “ssh-dss”,
key => “密钥值”,
nam e => “hostname”,
target => “/home/sky/.ssh/authorized_keys”,
user => ‘sky’,
}
-------------------------------------------------------------------------------------------------------------------------------------------------
tidy 删除制定条件文件
不制定age,和size,所有符号规定的条件都被删除
例子,删除1周的大于100M的文件
tidy { "/tmp":
age => "1w", //
recurse => 1, // 目录层数
matches => ["pub*.tmp","*.temp"]
rmdir => true //文件夹为空且满足条件是才删除
>
}
-------------------------------------------------------------------------------------------------------------------------------
schedule 周期行任务
与cron不同,cron使由操作系统发起,schedule由puppet满足条件后发起.
--------------------------------------------------------------------------------------------------------------------------------
页:
[1]