[root@sh-web1 ~]# cat /etc/passwd | grep ops
示例二:
安装nginx,普通资源定义:
init.pp文件.
class nginx {
include app::nginx
include web::nginx
}
app.pp文件.
class app::nginx {
package {"nginx":
ensure => 'present',
}
}
web.pp文件.
class web::nginx {
package {"nginx":
ensure => 'present',
}
}
node节点引用:
node base {
include admin
}
node /sh-(proxy|web)\d+/ inherits base {
case $::hostname {
/sh-proxy\d+/: {
# include nginx
}
"sh-web1": {
include nginx
}
}
}
puppet 更新:
[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: Package[nginx] is already declared in file /etc/puppet/modules/nginx/manifests/app.pp:4; cannot redeclare at /etc/puppet/modules/nginx/manifests/web.pp:4 on node sh-web1.localdomain
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
注释:报错资源重复定义.
解决方案:使用虚拟资源定义解决:
nginx模块下init.pp文件、app.pp文件、web.pp文件内容:
class nginx {
include app::nginx
include web::nginx
@package {"nginx": ensure => installed}
}
class app::nginx {
realize (Package['nginx'])
}
class web::nginx {
realize (Package['nginx'])
}
node节点引用:
node base {
include admin
}
node /sh-(proxy|web)\d+/ inherits base {
case $::hostname {
/sh-proxy\d+/: {
# include nginx
}
"sh-web1": {
include nginx
}
}
}
puppet agent端更新:
[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: Finished catalog run in 4.02 seconds
注释:适用于多版本的nginx定义.
示例三:
实例化一个虚拟资源除了系统提供的realize函数外,还可以用"<||>".
安装nginx为例:
nginx模块下的init.pp文件.
class nginx {
include app::nginx
include web::nginx
@package {"nginx": ensure => installed}
}
nginx模块下的app.pp文件.
class app::nginx {
Package<| title =='nginx' |>
}
nginx模板下的web.pp文件.
class web::nginx {
Package<| title =='nginx' |>
}
node节点文件node.pp文件.
node base {
include admin
}
node /sh-(proxy|web)\d+/ inherits base {
case $::hostname {
/sh-proxy\d+/: {
# include nginx
}
"sh-web1": {
include nginx
}
}
}
puppet agent端更新:
[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run