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

puppet的默认资源

默认资源可以为资源初始化属性和值,通常默认资源声明在site.pp文件首部,代码如下:

[[email protected] ~]# cat site.pp 
Exec { path => ‘/usr/bin:/bin:/usr/sbin:/sbin‘}

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

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

2、如果声明资源有一个名称空间资源"::",它的每个环节都需要首字母大写,如Concat::Fragment.

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

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

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

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

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

puppet虚拟化资源

虚拟化资源与普通资源的区别,虚拟化资源定以后要先实例化再使用,而普通资源定义后直接可以使用,定义虚拟化资源的方法是在资源前追加@,如@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运行的结果:

[[email protected] ~]# 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
[[email protected] ~]# cat /etc/passwd | grep test
test:x:502:502::/data/home/test:/bin/bash
[[email protected] ~]# 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 更新:

[[email protected] ~]# 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端更新:

[[email protected] ~]# 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}
}

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端更新:

[[email protected] ~]# 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
[[email protected] ~]# rpm -qa nginx
nginx-1.10.2-1.el6.x86_64
时间: 2024-07-30 19:42:58

Puppet默认资源和虚拟资源介绍(三十一)的相关文章

Puppet 命令参数介绍(三)

Puppet 命令参数介绍 前言: Puppet的工作原理: puppet master启动默认是监听tcp协议的8140端口.通过ruby的webrick web接收agent端的请求,根据请求内容与master的统一接口文件site.pp文件匹配,将匹配到的主机资源编译成catalog向agent分发,agent接收到请求后在本地应用. Puppet 命令分为独立命令和集成命令,puppet3.0版本后就没有了独立命令,集成命令也是未来的一个趋势,所以只写puppet集成命令. 通常查看帮助

虚拟主机中三种网络模式介绍

cocos2d-x升级到3.0后变化不小,除了API的变化(主要是函数和类名称变化,以及使用了C++11的不少特性,function/bind, lamda, std::thread-),创建和编译工程也做了一些简化调整.本文主要讨论一下cocos2d-x3.0 在android平台开发的环境设置及工程创建编译流程. 1.   初始设置 除了2.x所需要的python,jdk, android sdk和ndk之外,还需要部署apache-ant. 1)      在path中设置好java环境变

Liferay 6.2 改造系列之十一:默认关闭CDN动态资源

在行业客户中,一般无法提供CDN服务,因此默认关闭CDN动态资源功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to enable serving dynamically generated CSS, JavaScript, # and images via a CDN. Setting this to false allows the usage of CDNs that

restful架构风格设计准则(三)资源识别和资源设计

restful风格的设计中,首先要识别系统中的资源,然后用HTTP规范表示这些资源. 一.资源识别 1.以资源为中心 vs 以动作为中心 以动作为中心 在传统的软件分析设计中,常常要分析业务要使用的业务逻辑,并为业务逻辑的执行提供一系列接口.如:将商品放入购物车,提交订单等.这一系列接口组合在一起就可以组成完成目标所需要执行的业务逻辑. 在需要调用这些接口的时候,软件开发人员需要向这些接口所在的URL发送一个请求,从而驱使服务执行该动作. 以资源为中心 而在restful风格的软件分析设计中,我

linux程序分析工具介绍(三)——sar

本文要介绍的sar,是linux下用来分析系统本身运行情况的非常有用的工具.我们知道,程序在操作系统上要运行,要关注的点不外乎内存,CPU和IO(包括磁盘IO和网络IO).我们的应用程序在操作系统中运行前,我们需要了解系统当前的内存,cpu和IO的使用状况,还需要明白我们的应用程序运行时自身所需要的内存,cpu和IO资源的情况.只有操作系统剩余的内存,cpu和IO资源能够满足应用程序所需要的,才能保证应用程序在操作系统中正常的运行.sar就是用来帮助我们了解操作系统当前内存,cpu和IO等资源的

Lucene.Net 2.3.1开发介绍 —— 三、索引(二)

原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(二) 2.索引中用到的核心类 在Lucene.Net索引开发中,用到的类不多,这些类是索引过程的核心类.其中Analyzer是索引建立的基础,Directory是索引建立中或者建立好存储的介质,Document和Field类是逻辑结构的核心,IndexWriter是操作的核心.其他类的使用都被隐藏掉了,这也是为什么Lucene.Net使用这么方便的原因. 2.1 Analyzer 前面已经对Analyzer进行了很详细的讲解,Ana

容器资源需求、资源限制(二十二)

官网:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ 容器的资源需求,资源限制 Request:需求,最低保障: Limits:限制,硬限制 CPU: 1颗逻辑CPU 1=1000,millcores 500m=0.5CPU 内存: E.P.T.G.M.K.Ei.Pi Request保障容器CPU资源可用,limits限制资源 编写Demo测试, 查看CPU压缩使用情况 [

k8s之dashboard认证、资源需求、资源限制及HeapSter

1.部署dashboard kubernetes-dashboard运行时需要有sa账号提供权限 Dashboard官方地址:https://github.com/kubernetes/dashboard # 在node1上下载镜像 docker pull googlecontainer/kubernetes-dashboard-amd64:v1.10.1 docker tag googlecontainer/kubernetes-dashboard-amd64:v1.10.1 k8s.gcr.

14.容器资源需求、资源限制及HeapSter

一.容器资源需求及资源限制: 1.概念 Requests:资源需求,最低保障. Limits:资源限额,硬限制.限制容器无论怎么运行都不能超过的资源阈值 一般来讲,requests <= limits CPU:可压缩资源.一颗逻辑CPU,即一核.1=1000,millicores 内存:不可压缩资源.Ei,Pi,Ti,Gi,Mi,Ki ==> 以1024为进制. 2.定义资源需求及限制 资源需求和资源限制都是定义在容器上的. [[email protected] ~]# kubectl exp