请1我的 发表于 2016-1-14 08:27:49

puppet资源类型详解(02)

常用的资源类型:
                        notify, cron, exec, service, file, package, group, user

(1) notify:利用puppet定义一个信息。
                                message:通知的信息内容

1
2
3
notify {'warning':
message=> "From warning notify resource.",
}





(2) cron
                                ensure: 目标状态
                                command: 命令
                                hour
                                minute
                                month
                                monthday
                                weekday
                                name: 名称
                                user: 接收此任务的用户
                                environment: 运行时的环境变量


1
2
3
4
5
6
cron {'message':
minute=> '*/10',
command=> '/bin/echo "puppet"',
name=> 'echo something',
ensure=> present,
}





(3) exec
                                command (NameVar):要执行命令;必须幂等的;
                                creates:此属性指定的文件不存在时才执行此资源;
                                onlyif: 此属性指定的测试条件返回成功状态码时,才运行此资源指定的命令;
                                unless: 此属性指定的测试条件返回错误状态码时,才运行此资源指定的命令;
                                user: 以此属性指定的用户身份运行资源指定的命令;

                                group:
                                path: 命令的搜索路径合集;
                                cwd:在此属性指定的路径下运行此命令;
                                refresh: 定义如何refresh此资源;当此exec资源接收到其它资源的事件通知时的默认行为是再运行一次此资源,refresh属性就用于改变这种默认行为;
                                refreshonly:仅在收到refresh通知时才执行此exec资源指定的命令;
                                timeout: 命令运行超时时长;
                                tries: 尝试运行的次数;

1
2
3
4
5
exec {'test':
command=> 'echo "hi from exec again" >> /tmp/hello.puppet',
path => '/bin:/sbin:/usr/bin:/usr/sbin',
unless=> 'test -f /tmp/hello.puppet',
}





                                注意:如果不指定path属性,则命令必须为绝对路径,且shell内建命令可能无法运行;

(4) file
                                content:直接给定文件内容;
                                source: 复制此属性指定的文件为此file定义的文件的内容;
                                recurse: 如果source指定的路径为目录可递归传输整个目录;true or false;

                                checksum: 指定使用何种方式检查文件内容是否发生改变;
                                ctime:
                                mtime:
                                ensure: 文件存在与否及其文件类型
    如果文件本来不存在是否要新建文件,可以设置的值是 absent和present,file和directory. 如果指定 present,就会检查该文件是否存在,如果不存在就新建该文件,如果指定是 absent, 就会删除该文件(如果recurse => true ,就会删除目录)
                                        file, directory, link, present, absent
                                target: 当ensure为link时,指定链接的源文件;
                                force:强制运行与否;
                                owner: 属主
                                group: 属组
                                links:复制时如何处理链接文件,follow, ignore, manage;
                                mode: 权限
                                path: NameVar,文件路径;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
file {'/tmp/mydir':
ensure=> directory,
}
file {'/tmp/mydir/test.txt':
content=> 'hello from file resource',
ensure=> file,
}
file {'/tmp/mydir/fstab.puppet':
source=> '/etc/fstab',
ensure=> file,
}
file {'/tmp/mydir/fstab.link':
target=> '/tmp/mydir/fstab.puppet',
ensure=> link,
}
file {'/tmp/pam.puppet':
source=> '/etc/pam.d',
recurse=> true,
ensure=> directory,
}





(5) group
                                name: 组名,NameVar
                                gid: GID
                                system: 是否为系统组;

1
2
3
4
group {'mygrp':
gid=> 2000,
system=> false,
}





(6) user
                                comment:注释
                                ensure:
                                expiry: 过期时间
                                gid: 基本组
                                groups: 附加组
                                managehome: 是否让家目录具有“可管理性”;
                                home: 家目录路径
                                shell: 默认shell;
                                system: 是否为系统用户;
                                uid: UID
                                name: NameVar,用户名;
                                password:密码
                                password_max_age:
                                password_min_age:


1
2
3
4
5
group {'mysql':
ensure=> present,
system=> true,
gid=> 306,
}





1
2
3
4
group {'dbusers':
ensure=> present,
gid=> 3306,
}





1
2
3
4
5
6
7
user {'mysql':
ensure=> present,
uid=> 306,
gid=> 306,
groups=> 'dbusers',
system=> true,
}





(7) package
                                ensure: present, absent, latest, installed或版本号;
                                name: 程序包名,NameVar;
                                source:程序包来源;
                                provider: 指定要使用包管理器;

1
2
3
package{'zsh':
ensure=> installed,
}





1
2
3
4
5
package{'nginx':
ensure=> installed,
provider => rpm,
source=> '/tmp/nginx-1.6.2-1.el6.ngx.x86_64.rpm',
}





(8) service
                                ensure:running, true; stopped, false;
                                enable: 是否开机自动启动;
                                hasrestart: 告诉puppet服务脚本是否运行使用“restart”参数;
                                hasstatus:告诉puppet服务脚本是否运行使用“status”参数;
                                name: 脚本名称;
                                path: 脚本查找路径
                                pattern: 指明搜索服务相关的进程的模式;用于当脚本不支持使用restart/status参数时帮助判定服务是否运行;
                                restart:手动指定用于服务“重启”的命令;
                                start:
                                stop:
                                status:

1
2
3
4
5
package{'nginx':
ensure=> installed,
provider => rpm,
source=> '/tmp/nginx-1.6.2-1.el6.ngx.x86_64.rpm',
}





1
2
3
4
5
file {'/etc/nginx/conf.d/default.conf':
ensure=> file,
source=> '/tmp/default.conf',
notify=> Service['nginx'],
}





1
2
3
4
5
file {'/etc/nginx/nginx.conf':
ensure=> file,
source=> '/tmp/nginx.conf',
notify=> Service['nginx'],
}





1
2
3
4
5
6
service{'nginx':
ensure=> running,
enable=> true,
require=> Package['nginx'],
restart=> '/etc/rc.d/init.d/nginx reload',
}





页: [1]
查看完整版本: puppet资源类型详解(02)