Puppet默认资源和虚拟资源介绍(三十一)


puppet的默认资源node

        默认资源能够为资源初始化属性和值,一般默认资源声明在site.pp文件首部,代码以下:linux

[root@sh-web1 ~]# cat site.pp 
Exec { path => '/usr/bin:/bin:/usr/sbin:/sbin'}


声明默认资源注意事项以下:nginx

一、声明默认资源时首字母须要大写,如exec声明默认资源Exec、package声明默认资源Package等.web

二、若是声明资源有一个名称空间资源"::",它的每一个环节都须要首字母大写,如Concat::Fragment.shell


Exec默认资源的声明方法以下:bash

Exec { path => '/usr/bin:/bin:/usr/sbin:/sbin'}


经过Exec默认资源声明path属性的环境变量值,在后续声明exec资源时能够直接调用系统命令而不用担忧环境变量的问题.app

Package {provider => 'rpm'} #Package首字母大写
package {"nginx":}


在默认资源中声明provider属性,指定包的安装方式为rpm,后续package资源中provider属性均为rpm.dom


puppet虚拟化资源ide

        虚拟化资源与普通资源的区别,虚拟化资源定之后要先实例化再使用,而普通资源定义后直接能够使用,定义虚拟化资源的方法是在资源前追加@,如@user,这时的user资源就是一个虚拟化资源.在代码文件中将资源转换为虚拟资源后,puppet在执行的时候并不会调用它,若是想执行,须要经过realize函数或者"<||>"来实例化一个虚拟资源.函数


示例一:

但愿在本机只建立test用户.


建立用户的puppet代码以下:

class user {
        @user {"ops":
                ensure => present,
                home => '/data/home/ops',
                shell => '/bin/bash',
        }
        @user {"test":
                ensure => present,
                home => '/data/home/test',
                shell => '/bin/bash',
        }
}

node节点调用:

node base {
    include admin
}
node /sh-(proxy|web)\d+/  inherits base {
case $::hostname {
    /sh-proxy\d+/: {
         include nginx
      }
     "sh-web1": {
        include user
        realize (User['test'])
        } 
    }
}


注意:若是是普通资源的话include user时应该是上面定义的2个用户都被建立,可是定义为虚拟资源时realize实例化只建立了1个用户.


puppet运行的结果:

[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509554205'
Notice: /Stage[main]/User/User[test]/ensure: created
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: Finished catalog run in 0.22 seconds
[root@sh-web1 ~]# cat /etc/passwd | grep test
test:x:502:502::/data/home/test:/bin/bash
[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
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509555656'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
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}
}


749ba063f34ba3ad16ed0d654633f382.png


nginx模块下的app.pp文件.

class app::nginx {
    Package<| title =='nginx' |> 
}


37b0f8a46cdc16efbbd4b5dc009f8687.png

nginx模板下的web.pp文件.

class web::nginx {
    Package<| title =='nginx' |>
}


b11898419fe507678482fdabd350db47.png

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
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509704319'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: Finished catalog run in 9.20 seconds
[root@sh-web1 ~]# rpm -qa nginx
nginx-1.10.2-1.el6.x86_64
相关文章
相关标签/搜索