单机模式
定义资源 资源申报
常用资源类型
group,user
package
file
service
yumrepo
exec
cron
mount
打印字符
[root@kcw ~]# cat test.pp notify {'notify': message => 'hello puppet.' } 执行 [root@kcw ~]# puppet apply test.pp warning: Could not retrieve fact fqdn notice: hello puppet. notice: /Stage[main]//Notify[notify]/message: defined 'message' as 'hello puppet.' notice: Finished catalog run in 0.04 seconds
安装nginx 并且启动
常用资源属性::
package 常用属性:
ensure:程序包的目标状态
name:资源名字
provider:软件包管理器
source:指定程序包文件路径
install_options :安装选项,最常用的是通过INSTALLDIR 来制定安装目录(一般用来window安装)
package{‘mysql’:
ensure => 'installed',
provider=>'msi',
source => 'D:\software\mysql-5.5.36.msi',
install_options => {‘INSTALLDIR=>'D:\mysql'’},
}
service :
ensure:服务的目标状态,true和false
enable:是否开机自动启动
name:服务名称
path:服务脚本名称
start :启动命令
stop
restart
status
file:
文件、目录、符号连接
生成文件内容
管理文件属性、权限
通过source属性到指定位置下载
通过resurce 属性来获取目录
常用属性:
ensure:目标状态 present absent file directory
backup:通过filebucker资源来备份文件:值通常为filebucker资源的名称
mtime:
content:文件内容:生成方式有三种 (content source target) 彼此互斥
source:通过制定的URL下载至本地,获取方式通常为puppet url ,格式:puppet:///modules/MODULE_NAME/file_names;
target:为符号链接制定目标:
links:文件为符号链接 {follow | manage}
path:文件路径,文件路径必须使用双引号
mode :定义权限
owner ;属组
force:强制执行删除文件链接 或 目录:仅用于ensure为absent时:
purge:清空指定目录中存在的,但未在资源中定义的文件;
recurse:目录递归:值true fasle inf remote
replace:替换:本地存在的文件与资源中文件内容不同时是否执行替换,默认为否
exec: 执行命令,通常在不得不用时才使用,通常用于无法完成puppet自身无法完成的功能
常用属性:
command:要执行的命令,通常为执行文件的路径;
group:以谁为属组执行
user: 以谁为属主来执行
path:命令搜索路径
onlyif: 0 ,表示仅在命令的状态返回值为0时才执行此命令
refresh:接受到其他资源的通知时,如何重新执行此命令。
refreshonly:仅当被依赖的资源发生改变时才会触发
tries:尝试的次数 默认为1
try_sleep :多从尝试之间的时间间隔
group:管理系统上用户组
ensure:目标状态,present absent
name:祖名
gid:GID
system:系统组
user:管理用户
常用属性
ensure :目标状态
name:
uid:
system:
home:
shell:
gid:
password;
cron:管理cron
常用属性
ensure:目标命令
command:命令或者脚本
environment:运行时的环境
hour
minute
month
monthday
weekday
name
user
notify:调试输出
常用参数
message:信息
name:信息名称
软链接 [root@kcw ~]# cat link.pp file {'fstab.cf': ensure => present, target => "/etc/fstab", path => "/tmp/fstab.cf", links => follow, } 测试 [root@kcw ~]# puppet apply link.pp warning: Could not retrieve fact fqdn notice: /Stage[main]//File[fstab.cf]/target: target changed 'notlink' to '/etc/fstab' notice: Finished catalog run in 0.04 seconds [root@kcw ~]# ll /tmp/ 总用量 80 -rw-------. 1 root root 0 6月 4 03:49 file.oR1lP lrwxrwxrwx. 1 root root 10 6月 16 02:39 fstab.cf -> /etc/fstab [root@kcw ~]# cat test1.pp package {'nginx': ensure => installed, } service {'nginx': ensure => running, 或者ture } 执行测试 [root@kcw ~]# puppet apply test1.pp warning: Could not retrieve fact fqdn notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: Finished catalog run in 0.77 seconds [root@kcw ~]# netstat -antlp |grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29088/nginx
资源引用:
metaparameters
类型[‘资源名称’]
引用时资源的首字母要大写
before => 资源引用
require =>资源引用
notify=> 配置改变后立即通知我
subscribe=》订阅 有新版本发给我一份
[root@kcw ~]# cat test1.pp package {'nginx': ensure => installed, # before => Service['nginx'], #必须在服务启动之前 首字母必须大写 # notify => Service['nginx'], #文件改变通知nginx 定义在前资源 } service {'nginx': ensure => true, require => Package['nginx'], #服务启动前确保nginx应用 常用 # subscribe =File['/etc/nginx/nginx.conf'] , #订阅 } 测试执行 [root@kcw ~]# rpm -q nginx nginx-1.0.15-11.el6.x86_64 [root@kcw ~]# service nginx stop 停止 nginx: [确定] [root@kcw ~]# rpm -e nginx [root@kcw ~]# puppet apply test1.pp warning: Could not retrieve fact fqdn notice: /Stage[main]//Package[nginx]/ensure: created notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: Finished catalog run in 6.57 seconds [root@kcw ~]# /etc/init.d/nginx status nginx (pid 29701) 正在运行... [root@kcw ~]# netstat -antlp |grep :80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29701/nginx tcp 0 0 192.168.1.6:41768 115.28.122.210:80 TIME_WAIT -
group
group {'kang': ensure => present, gid =>1001, } 测试 [root@kcw ~]# puppet apply test3.pp warning: Could not retrieve fact fqdn notice: /Stage[main]//Group[kang]/ensure: created notice: Finished catalog run in 0.06 seconds 查看 [root@kcw ~]# tail /etc/group stapsys:x:157: stapdev:x:158: sshd:x:74: tcpdump:x:72: slocate:x:21: apache:x:48: mysql:x:27: puppet:x:52: nginx:x:493: kang:x:1001: 添加用户 [root@kcw ~]# cat test3.pp group {'kang': ensure => present, gid => 1001, } user {'kang': gid => 1001, uid => 1001, home => '/home/kang', password => '$1$v3PuG$1YZMOxlwnrLunxz1J6ePo1', #密码生成方式有2中1、 openssl passwd -1 -salt `openssl rand -hex 4` 2、 grub-md5-crypt managehome => true, ensure => present, require => Group['kang'], } 测试 [root@kcw ~]# id kang uid=1001(kang) gid=1001(kang) 组=1001(kang) [root@kcw ~]# su - kang
file
[root@kcw ~]# cat file.pp file {'/etc/nginx/nginx.conf': ensure => file, source => '/backup/nginx/nginx.conf', #事先要有这个文件 并且修改好属性 mode =>'0640', owner=>'root', group=>'root', } 测试 [root@kcw ~]# puppet apply file.pp warning: Could not retrieve fact fqdn notice: /File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}b1de7b8f5f09371a466aa56a3e41abe7' notice: /File[/etc/nginx/nginx.conf]/mode: mode changed '0644' to '0640' notice: Finished catalog run in 0.05 seconds
exec
[root@kcw ~]# cat exec.pp exec {'test': path=>'/bin:/sbin:/usr/bin:/usr/sbin', #command=>'mktemp /tmp/file.XXXXX', command=>'chkconfig --add mysqld;chkconfig mysqld on', user=>'root', group=>'root', }
资源间的应用次序琏 -> 次序链 ~> 通知链
puppet变量:
1、使用$开头,无论是定义还是引用
puppet 正则表达式 if,case ,selector
$package = $operatingsystem ? { /(?i-mx:^(centos|redhat|fedoar))/=>'httpd', /(?i-mx:^(debin|ubuntu)) / => 'apache2', #m表示把. 当作换行符来使用 x 忽略模式中的空白字符和注释。i忽略字母大小写 有-号是禁用后面的选项 ,没有-号是启动 ?是固定字符 } notify {'notice': message => "Install $package", }
在不同的操作系统安装不同的程序包
字符型,数值型,数组[,,],布尔型,映射{key1=>value,...},undef,正则表达式(只能用于支持使用=~,!~这种符号的场景中)
puppet 条件判断 单分支 双分支 多分支
单分支 if condition{ statement .... } 双分支 if condition{ statement .... } else { statement .... } 多分支 if condition { statement .... } elsif condition{ statement .... } 测试: [root@kcw ~]# cat if.pp $test=25 if $test > 30 { notice('old man') } else { notice('Yong man') } 结果 [root@kcw ~]# puppet apply if.pp warning: Could not retrieve fact fqdn notice: Scope(Class[main]): Yong man notice: Finished catalog run in 0.04 seconds
多分支判断
[root@puppet ~]# cat if2.pp if $operatingsystem == 'CentOS' { notify {'centos': message => "welcome to CentOS linux.",} } elsif $operatingsystem == 'RedHat' { notify {'centos': message => "welcome to RedHat linux.",} } elsif $operatingsystem == 'fedora' { notify {'centos': message => "welcome to fedora linux.",} } else { notify {'unknown':message=>"unknown operating system",} } 测试 [root@puppet ~]# puppet apply -v if2.pp info: Applying configuration version '1434858217' notice: welcome to CentOS linux. notice: /Stage[main]//Notify[centos]/message: defined 'message' as 'welcome to CentOS linux.' notice: Finished catalog run in 0.02 seconds
变量引用selector
[root@puppet ~]# cat test4.pp $webserver = $operatingsystem ? { /^(?i-mx:centos|fedora|redhat)/ => 'httpd', /^(?i-mx:ubuntu|debian)/ => 'apache2', } $webprovider = $operatingsystem ? { /^(?i-mx:centos|fedora|redhat)/ => 'yum', /^(?i-mx:ubuntu|debian)/ => 'apt', } package {"$webserver": ensure => present, provider=>$webprovider, } 测试 [root@puppet ~]# puppet apply -v test4.pp info: Applying configuration version '1434861834' notice: Finished catalog run in 5.60 seconds
case
[root@puppet ~]# cat case.pp case $operatingsystem { /^(?i-mx:redhat|centos|fedora)/: {package {'httpd' : ensure=> present,provider=>yum,}} /^(?i-mx:ubuntu|debian)/: {package {'apache2' : ensure=> present,provider=>apt,}} default: {notify {'notice':message => "unknown system . *_*",}} } 测试 [root@puppet ~]# puppet apply -v case.pp info: Applying configuration version '1434862734' notice: /Stage[main]//Package[httpd]/ensure: created notice: Finished catalog run in 28.13 seconds
定义类
[root@puppet ~]# cat class.pp class nginx { package {'nginx': ensure=>present, } service {'nginx': ensure=>true, require=>Package['nginx'], } } #include nginx class {'nginx':} 测试: [root@puppet ~]# puppet apply class.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: Finished catalog run in 5.11 seconds
实例,带参数的类
[root@puppet ~]# cat aa.pp $webserver = $operatingsystem ? { /^(?i-mx:redhat|centos|fedora)/ =>'httpd', /^(?i-mx:ubuntu|debian)/ =>'apache2', } class httpd ($pkgname = 'apache2'){ package {"$pkgname": ensure=>present, } service {"$pkgname": ensure => true, require => Package["$pkgname"], } } class {'httpd': pkgname=>$webserver, } 测试 [root@puppet ~]# puppet apply -v aa.pp info: Applying configuration version '1434871006' notice: /Stage[main]/Httpd/Package[httpd]/ensure: created notice: /Stage[main]/Httpd/Service[httpd]/ensure: ensure changed 'stopped' to 'running' info: /Stage[main]/Httpd/Service[httpd]: Unscheduling refresh on Service[httpd] notice: Finished catalog run in 5.05 seconds
#文件改变通知记录 [root@puppet ~]# cat j.pp file {'/tmp/testok.txt': ensure=>file, content=>"helo word/n", notify=>Exec['monitor'], } exec {'monitor': command=>'echo "/tmp/testok.txt is changed" >> /tmp/monitor.txt', # refreshonly=>true, path=>'/bin:/sbin:/usr/bin:/usr/sbin', }