Linux Puppet基础知识

Posted guowei-linux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Puppet基础知识相关的知识,希望对你有一定的参考价值。

Linux Puppet基础知识

         Puppet可以批量配置多个节点,减少运维人员在重复的、繁琐的工作中花费的时间;可以轻松的管理几千个节点;

                   官网地址:https://puppet.com/

         Puppet相对于ansible来说,其不仅要运行master端,且在每个被管控节点上还要安装并运行agent端;

                   master:puppet server

                   agent:agent周期性(默认为30分钟)的到master端请求与自己相关的配置,进而在本地执行(所以说agent才是真正管理节点的部件),然后将执行结果报告给master端;

                   Note:master与agent使用自己的私有认证机制进行认证,其实也就是ssl,只不过master自建了一个私有CA进行证书签发;

         Puppet工作模式:

                   定义:使用puppet配置语言定义基础配置信息(资源状态);

                   模拟:先在本地模拟执行一遍,如果执行没有错误再应用到各个节点;

                   强制:比对节点状态与定义的资源状态是否一致,如果不一致则agent执行相关配置;强制使当前与定义的目标状态保持一致;

                   报告:通过puppet API将日志发送到第三方监控工具,进行监控;还会向master报告相应的执行结果;

         Puppet的三层模型:

                   顶层(Configuration Language,配置语言):定义基础配置信息;

                   中层(Transactional Layer,事务层):解决资源之间的依赖关系;比如要想启动某个服务,必须要先安装这个服务才行;

                   底层(Resource Abstraction Layer,资源抽象层):将主机上每个可被管理的对象都被定义为资源;

                          这样抽象实现以后,可以将每个对象与其实现方式隔离开来;比如在系统中安装一个软件,在Ubuntu(apk)中安装和在Centos(yum)中安装是不同的,因为它们使用的工具不同,但是通过这个层的隔离,就可以将具体的实现支持透明化,仅给出要安装的软件包,具体的实现交给puppet就可以了;

                            资源类型:例如用户、组、文件、服务、cron任务等;

                            属性及状态与其实现方式相分离;

                            每个资源的期望状态;

         Puppet的核心组件:资源

                   资源清单:manifests,即使用到的资源的列表文件;还有一个叫站点清单:即资源清单的清单,可以理解为是应用到某一远端被管控节点的资源清单的组合;

                   模块:资源清单及清单中的资源定义的所依赖的文件、模板等数据按特定结构组织起来即为模块;模块最大的好处就是可以实现代码重用,从而简化了繁杂的工作;

 

 

         Puppet工作过程:

                   类型1:仅在本地应用

                            Puppet可以设置多个资源清单,每个资源清单包含不同资源,用于实现不同配置功能;设置完资源清单以后,Puppet会对资源清单进行编译,进而生成伪代码(catalog),然后才能在本地节点上应用(执行);

                   类型2:在具有被控的远端节点上应用

                            agent节点会周期性向master发送请求信息并且发送自己的facts信息(包括主机名、IP地址、CPU核心数等),请求与自己的相关的配置;当master收到agent的请求时,会根据其主机名在本地进行查找,搜寻与请求agent相关的资源清单(manifest),如果找到相匹配的资源清单,则在本地进行编译,生成伪代(catalog),然后将伪代码发送给请求的agent,接着agent会在本地应用这个伪代码,最后将应用结果报告给master端;

 

 

 

         Puppet的master与agent是通过HTTPS协议通信的;因为master端自带CA,所以我们可以通过master的CA给每个受其管理的agent端发放证书,从而实现安全通信;

         Puppet安装:

                   可以在官网下载安装,也可以直接使用yum安装;

                  agent节点需要安装puppet和facter包;

                   master节点需要安装puppet、facter和puppet-server包;

                  puppet命令用法:具体用法在下面使用时再解释;

                            帮助:puppet help [subcommand]

                            查看puppet的各种资源类型的信息:puppet describe

                            Usage: puppet <subcommand> [options] <action> [options]

                   定义资源:  

                            格式:

                                     type ‘title’:

                                               attribute1 => value,

                                               attribute2 => value,

                                               ……

                                              

                                     示例:

                                               user ‘guowei’:

                                                        uid => 1000,

                                                        gid => 1000,

                                                        shell => ‘/bin/bash’,

                                                        home => ‘/home/guowei’,

                                                        ensure => present,

                                              

                            特殊变量:

                                     Name/Namevar

                                               大多数资源都有一个Namevar属性,如果过不指出,则title则为此种资源的Namevar变量的值;比如user的Namevar为name,file的Namevar为file;

                                     Ensure

                                               大多数资源都支持的属性,用于控制此种资源的存在性;其取值一般根据资源的不同而不同,大体有:present表示存在或者创建、absent表示不存在或者删除;

                                     Metaparameters

                                              

                            常用资源类型:

                                     user,group,file,package,service,exec,cron,notify等;

 

                                     ~]# puppet describe user

                                               常用属性:

                                                        comment:描述信息;

                                                        ensure:创建或删除用户,有两个取值:present、absent;

                                                        expiry:账户的过期期限;

                                                        gid:账号所属的基本组;

                                                        groups:账号所属的附加组;

                                                        home:家目录;所指定的目录需要事先创建,且权限正确;

                                                        name:用户名,且为Namevar;

                                                        password:账号密码;

                                                        password_max_age:密码最长使用期限;

                                                        password_min_age:密码最短使用期限;

                                                        shell:账号使用的shell类型;

                                                        system:账号是否为系统用户,有四个取值:true|yes,false|no;;

                                                        uid:UID,无需解释;

                                     ~]# puppet describe group

                                               常用属性:

                                                        ensure:创建或者删除组,有两个取值:present、absent;

                                                        gid:GID,无需解释;

                                                        name:组名,且为Namevar;

                                                        system:是否为系统组;有四个取值:true|yes,false|no;

                                                        members:指定组成员,自己为组成员的附加组;

                                               示例:

                                                        ~]# cat test1.pp            此类文件约定成俗以”.pp”结尾

                                                                  group ‘distro‘:

                                                                           gid => 2000,

                                                                           ensure => present,

                                                                 

                                                                  user ‘centos‘:

                                                                           uid => 2000,

                                                                           gid => 2000,

                                                                           shell => ‘/bin/bash‘,

                                                                           home => ‘/home/centos‘,     如果是创建用户,则需要此目录已经事先存在,且权限正确;

                                                                           ensure => present,          表示创建这个用户,如果是absent的话就是删除这个用户;

                                                                 

                                                        使用了两个资源类型(group和user),分别应用了几个其特有的属性,属性的作用就是字面意思,很好理解;

                                     puppet apply:在本地直接应用资源清单定义的资源,无需经过master管理;

                                              用法:puppet apply [-h|--help] [-v|--verbose] [--noop]  <file>

                                                        -h:查看详细的帮助信息;

                                                        -v:应用时显示详细信息;

                                                        --noop:测试应用(执行),而不是真正的应用(执行);

                                     ~]# puppet apply -v test1.pp

                                     ~]# puppet describe file

                                               用途:主要用于管理文件及其内容|从属关系|权限、目录、符号链接;

                                               常用属性:

                                                        checksum:校验文件完整性;

                                                        source:从指定路径下载文件,可以是远程也可以是本地,一般用于agent从master下载相关配置文件;如果是远端路径一般格式为:puppet:///modules/MODULE_ANME/FILE_ANME;

                                                        content:编写文件内容,使用”\n”作为换行符、”\t”作为制表符;

                                                        ensure:取值为:present、absent、file、directory、link;

                                                        recurse:递归处理目录或其中的文件,只有当ensure为directory时才起作用;

                                                        force:强制执行,有四个取值:true|yes,false|no;;

                                                        group:属组;

                                                        owner:属主;

                                                        mode:权限:

                                                                 格式:<who><op><perm>

                                                                     * "who" should be u (user), g (group), o (other), and/or a (all)

                                                                     * "op" should be = (set exact permissions), + (add select permissions),or - (remove select permissions)

                                                                     * "perm" should be one or more of:

                                                                         * r (read)

                                                                         * w (write)

                                                                         * x (execute/search)

                                                                         * t (sticky)

                                                                         * s (setuid/setgid)

                                                                         * X (execute/search if directory or if any one user can execute)

                                                                         * u (user‘s current permissions)   使用用户的当前权限

                                                                         * g (group‘s current permissions)

                                                                         * o (other‘s current permissions)

                                                                 Note:或者使用数字表示权限;

                                                        mtime:最近修改时间;

                                                        path:目标路径,且为Namevar;

                                                        target:被创建链接文件的文件;不能与source和content一起使用且ensure需为link;此时path或title指定的文件为链接文件;

                                               示例:

                                                        ]# cat test2.pp

                                                                 file ‘/tmp/mydir/‘:

                                                                ensure => directory,

                                                                

 

                                                                 file ‘/tmp/puppet.file‘:

                                                                content => ‘puppet testing\nhello world!‘,

                                                                ensure => file,

                                                                owner => ‘centos‘,

                                                                group => ‘distro‘,

                                                                mode => 0400,

                                                                

 

                                                                 file ‘fstab‘:

                                                                path => ‘/tmp/fstab.puppet‘,

                                                                source => ‘/etc/fstab‘,

                                                                ensure => file,

                                                                

 

                                                                 file ‘/tmp/puppet.link‘:

                                                                ensure => link,

                                                                target => ‘/tmp/puppet.file‘,

                                                                

                                                        ]# puppet apply -v -d test2.pp

                                     ~]# puppet describe exec

                                               用途:用于运行系统中的命令且要求所运行的命令具有幂等性,也就是同样的命令运行多次都是相同的结果;

                                                        幂等性:

                                                                 1.命令本身具有幂等性;

                                                                 2.资源有onlyif、unless、creates等属性以实现命令的条件式运行;

                                                                 3.资源有regreshonly属性,以实现只有订阅的资源发生变化时才运行;

                                               常用属性:

                                                        command:指定要运行的命令,且为Namevar;

                                                        creates:此属性指定的文件不存在时才执行此命令;

                                                        cwd:指定在哪个目录下执行命令;

                                                        environment:指定环境变量,一般是不在PATH环境变量中的;

                                                        path:指定环境变量;如果不使用path属性,则需要使用命令的绝对路径;

                                                        group:以哪个组的身份运行命令;

                                                        user:以哪个用户的身份运行命令;

                                                        onlyif:只有此处指定的命令的运行结果为成功(即返回值状态码为0)才执行command中的命令;

                                                        unless:只有此处指定的命令的运行结果为失败(即返回值状态码为非0)才执行command中的命令;

                                                        refresh:如果接收到其他资源发来的refresh通知时,默认是重新执行exec定义的command,但是refresh属性可以改变这种行为,即可指定仅在资源refresh时运行的命令;比如某个服务的配置文件发生了改变,我们一般都会让服务重载或者重启,这种情况下就可以通过这个属性来实现,但是refresh需要接收到某种通知以后才能启动相应的操作;

                                                        refreshonly:只有接收到refresh时才执行command中的命令,否则不做任何操作;可取值为:true、force;

                                                        timeout:命令执行的超时时长;

                                               示例:

                                                        ]# cat test3.pp

                                                                 exec ‘modprobe ext4‘:

                                                                           path => ‘/usr/sbin:/usr/bin‘,

                                                                           user => root,

                                                                           group => root,

                                                                           refresh => ‘modprobe -r ext4 && modeptobe ext4‘,

                                                                           timeout => 8,

                                                                           tries => 2,

                                                                

                                                                 exec ‘/bin/echo world > /tmp/hello.txt‘:

                                                                           user => root,

                                                                           group => root,

                                                                           creates => ‘/tmp/hello.txt‘,

                                                                

                                                                 exec ‘/bin/echo centos7 > /tmp/hello2.txt‘:

                                                                           user => root,

                                                                           group => root,

                                                                           unless => ‘/usr/bin/test -f /tmp/hello2.txt‘,

                                                                

                                     ~]# puppet describe notify

                                               用途:用于显示一些提示信息;

                                               常用属性:

                                                        message:要发送的消息的内容;

                                     ~]# puppet describe cron

                                               用途:设定周期性任务;

                                               常用属性:

                                                        command:指定要运行的命令;

                                                        ensure:添加或删除cron任务,取值为:present、absent;

                                                        minute:指定分钟;

                                                        hour:指定小时;

                                                        month:指定月份;

                                     monthday:指定某月的第几天,最大取值范围为1-31;

                                     weekday:指定星期,取值为0-7且0与7是同一天;

                                                        user:指定是哪个用户的cron任务;

                                                        name:cron任务的名称;

                                                        target:指定存储cron任务的文件路径;

                                                        environment:运行cron任务的环境变量;

                                               示例:

                                                        ]# cat test5.pp

                                                                 cron ‘sync time‘:

                                                                           command => ‘/usr/sbin/ntpdate api.api.bz &> /dev/null‘,

                                                                           ensure => present,

                                                                           hour => ‘*/3‘,

                                                                

                                     ~]# puppet describe package

                                               用途:管理程序包;

                                               常用属性:

                                                        configfiles:指定安装新的程序包或者重新安装程序包时,对于之前已经存在配置文件的处理方式;有两个取值:keep(保存)、replace(替换);

                                                        ensure:安装或卸载程序包;取值为:latest(安装最新版)、指定程序包的版本(VERSION(2.5.3-2.el7))、absent、present|installed、purged(清除);

                                                        install_options:指定安装选项;

                                                        name:指定安装包的名称;

                                                        source:指定程序包的安装源,可以是本地路径或者URL;取值可以为yum、apt、rpm、dpkg,如果是后两者的话,需要指明具体路径;

                                                        provider:指定安装方式;可取值为rpm、dpkg、yum、apt;

                                               示例:

                                                        ]# cat test6.pp

                                                                 package ‘zsh’:

                                                                           ensure => latest,

                                                                

                                                                 package ‘jdk’:

                                                                           ensure => installed,

                                                                           source => ‘/usr/local/src/jdk-8u25-linux-x64.rpm’,

                                                                           provider => rpm,

                                                                

                                     ~]# puppet describe service

                                               用途:管理服务;

                                               常用属性:

                                                        binary:指定启动特定服务的启动脚本;

                                                        enable:指定是否为开机自动启动;可取值为:true、false、manual;

                                                        ensure:指定服务状态;可取值为stopped|false、running|true;

                                                        hasrestart:说明当前启动服务的机制是否自带restart功能;如果没有可以通过先停止在启动来重新启动;

                                                        hasstatus:类似于上面;

                                                        name:指定服务名称;

                                                        path:指定启动服务所用到的脚本的路径;

                                                        restart:如果服务的启动脚本不支持restart选项,则可以通过这个属性手动设置重启方式;

                                                        start:类似于上面;

                                                        stop:类似于上面;

                                                        status:类似于上面;

                                                        pattern:用于搜索于此服务相关的进程的模式;当脚本不支持restart/status时,用于确定服务是否处于运行状态;

                                               示例:

                                                        ~]# cat test7.pp

                                                                 package ‘nginx‘:

                                                                           ensure => latest,

                                                                

 

                                                                 service ‘nginx‘:

                                                                           ensure => running,

                                                                           enable => true,

                                                                           hasrestart => true,

                                                                           hasstatus => true,

                                                                           restart => ‘systemctl reload nginx.service‘,

                                                                

                            特殊属性:Metaparameters

                                     puppte提供了before(在….之前)、require(在….之后)、notify(自己发生改变以后通知其他资源)和subscribe(自己监控其他资源,如果其改变,则进行相应的操作)四个元参数来定义资源间的依赖/通知关系;

                                               这四个参数都以另外其他资源或资源数组作为其值,这也成为资源引用;

                                               资源引用通”Type [title]”(资源引用时,其类型名称的首字母要大写)这样的方式进行,比如User[‘centos’];

                                     1.定义依赖关系(before、require)

                                               用于解决资源之间的依赖关系,比如要启动一个服务,就要先安装这个服务,否则就会执行错误;

                                     示例:

                                               ~]# cat test8.pp

                                                        group ‘king‘:

                                                                 gid => 2500,

                                                                 ensure => present,

                                                                 before => User[‘man‘],       在user之前运行;

                                                       

 

                                                        user ‘man‘:

                                                                 uid => 2500,

                                                                 gid => 2500,

                                                                 shell => ‘/bin/bash‘,

                                                                 home => ‘/home/centos‘,

                                                                 ensure => present,

                                                                 #require => Group[‘king’]    在group之后运行;

                                                       

                                               Note:在表示依赖关系时,还可以使用”A ->B ”符号表示A先于B;

                                     2.定义通知关系(notify、subscribe)

                                               用于实现资源之间的信息传递,比如当一个服务的配置文件发生改变时,我们要通知相关的服务脚本重新启动服务;

                                              示例:

                                                        ~]# cat test9.pp

                                                                 package ‘nginx‘:

                                                                           ensure => latest,

                                                                

 

                                                                 file ‘/etc/nginx/nginx.conf‘:

                                                                           ensure => file,

                                                                           source => ‘/root/modules/nginx/files/nginx.conf‘,

                                                                           require => Package[‘nginx‘],

                                                                           notify => Service[‘nginx‘],

                                                                

 

                                                                 service ‘nginx‘:

                                                                           ensure => running,

                                                                           enable => true,

                                                                           hasrestart => true,

                                                                           hasstatus => true,

                                                                           require => [ Package[‘nginx‘],File[‘/etc/nginx/nginx.conf‘] ],

                                                                

                                              Note:在表示依赖关系时,还可以使用”A ~>B ”符号表示A通知B;

                            变量:

                                     1.Puppte中的变量须以”$”开头,且使用”=”作为赋值操作符;

                                     2.任何正常数据类型(除了正则表达式)的值都可以被赋予puppet中的变量;如字符串、数值、布尔值、数组、hash以及特殊的undef值(即变量未被赋值);

                                     3.Puppet的每个变量都有两个名字:简短名称和长格式完全限定名称(FQN),完全限定名称的格式为”$scope::variable”;

                                               作用域:定义代码的生效范围,以实现代码间隔离;

                                                        仅能隔离变量、资源的默认属性;

                                                        不能隔离资源的名称、及其引用;

                                     4.变量的引用方式:

                                               相对路径: 即直接使用变量名;

                                               绝对路径:$scope::scope::variable

                                     5.变量的赋值符号:

                                               =:直接赋值;

                                               +=:追加赋值(在原来的基础上新增内容);

 

                                     Puppet的数据类型;

                                               字符型:

                                                        非结构化的文本字符串,可以使用引号,也可以不用;

                                                        单引号中的变量不会替换,而双引号中的能够进行变量替换;

                                                        字符型值也支持使用转义符;

                                               数值型:

                                                        可为整数或浮点数,不过,Puppet只有在数值上下文(即做数值运算时)才能把数值当数值对待,其他情况下一律以字符型处理;

                                               数组:

                                                        数组值为中括号”[ ]”中的以逗号分隔的项目列表,最后一个项目后面可以有逗号;

                                                        数组中的元素可以为任意可用数据类型,包括hash或其他数组;

                                                        数组索引为从0开始的整数,也可以使用负数索引;

                                              布尔型:

                                                        true或false,使用时不能加引号;

                                                        if语句的测试条件和比较表达式都会返回布尔型值;

                                                        另外,其他数据类型也可以自动转换为布尔型,如空字符串为false;

                                               undef:

                                                        从未被声明的变量的值的类型即为undef;

                                                        也可手动为变量赋予undef值,即直接使用不加引号的undef字符串;

                                               hash(类似于Python中的字典):

                                                        即为键值数据类型,键和值之间使用”=>”分隔,键值对儿定义在” ”中,彼此间以逗号间隔;比如:‘A’ => ‘12’,’B’ => ‘abc’

                                                        其键值为字符型数据,而值可以为Puppet支持的任意数据类型;

                                                        访问hash类型的数据元素要使用”键”当做索引进行;

                                               正则表达式:

                                                        属于Puppet的非标准数据类型,不能被赋予给变量,仅能用于有限的几个接收正则表达式的的地方,即接受使用”=~”及”!~”匹配操作符的位置,通常包括case语句中的selector,以及节点名称匹配的位置;

                                                        不能传递给函数或用于资源属性的定义;

                                                        Puppet中的正则表达式支持使用(?<ENABLE OPTION>:<SUBPATTERN>)和(?-<DISABLED OPTION>:<SUBPATTERN>)两个特殊的符号;

                                                                 OPTION:

                                                                           i:表示忽略字符大小写;

                                                                           m:表示把.当做换行符;

                                                                           x:忽略模式中的空白字符和注释;

 

                                                        示例:

                                                                 $packages = $operatingsystem ?

                                                                           /(?i-mx:ubuntu|debian)/   => ‘apache2’,

                                                                           /(?i-mx:centos|fedora|redhat)/  => ‘httpd’,

                                                                   #如果$operatingsystem与ubuntu|debian| centos|fedora|redhat中任何一个键相匹配的话$packages就被赋予键所对应的值;

                                     Puppet支持的表达式:

                                               比较操作符:==、>、<、>=、<=、!~、=~、!=、in;

                                               逻辑操作符:and、or、!;

                                               算数操作符:+、-、*、/、%、>>、<<;

                                     示例:

                                               ~]# cat test10.pp

                                                        $webserver=httpd

                                                        package $webserver :

                                                       ensure => latest,

                                                       

                                     Puppet中变量的种类:

                                               自定义变量:即上面所述的变量

                                               facter变量:agent自身支持的变量,一般是有关自己本身属性的变量,比如主机名、IP地址、CPU颗数等;可通过facter -p查看所有支持的变量;

                                               内置变量:

                                                        客户端内置变量:

                                                                 $clientcert

                                                                 $clientversion

                                                        服务器端内置变量:

                                                                 $servername

                                                                 $serverip

                                                                 $serverversion

                                                                 $module_name

                                     Puppet支持的条件判断语句:

                                               if:

                                                        if CONDITION

                                                                 statement

                                                                 ……

                                                                       ------->  到这为单分支;

                                                        else

                                                                 statement

                                                                 ……

                                                                       ------->到这为双分支;

 

                                                        if CONDITION

                                                                 statement

                                                                 ……

                                                       

                                                        elsif CONDITION

                                                                 statement

                                                                 ……

                                                       

                                                        else

                                                                 statement

                                                                 ……

                                                                         ------->到这为多分支;

                                                        CONDITION的用法:

                                                                 1.比较表达式;

                                                                 2.变量引用;

                                                                 3.有返回值的函数调用;

                                               示例:

                                                        ~]# cat test11.pp

                                                                 if $processorcount > 5

                                                                           notice("SMP PC")       这是puppet的一个内置函数,功能类似于notify资源;

                                                                

                                                                 else

                                                                           #notice("KunKun")

                                                                           notify ‘message‘:

                                                                                    message => "KunKun",

                                                                          

                                                                

                                                                 if $operatingsystem =~ /^(?i-mx:(centos|redhat))/

                                                                           notice(“Welcome to $1 linux server”)

                                                                

                                               case:

                                                        case CONTROL_EXPRESS

                                                                 Case1,… : statement…

                                                                 Case2,… : statement…

                                                                 ……

                                                                 default : statement…

                                                       

                                                        CONTROL_EXPRESS:

                                                                 1.比较表达式;

                                                                 2.变量引用;

                                                                 3.有返回值的函数调用;

                                                        Case#:

                                                                 1.比较表达式;

                                                                 2.变量引用;

                                                                 3.仅有一个返回值的函数调用;

                                                                 4.正则表达式;

                                                                 5.default;

                                                        示例:

                                                                 ~]# cat test18.pp

                                                                           case $operatingsystem

                                                                                    ‘Solaris’ :        notice(“welcome to Solaris”)

                                                                                    ‘Redhat’,’Centos’ : notice(“welcome to Redhat OSFamily”)

                                                                                    /^(Debian|Ubuntu)$/ : notice(“welcome to $1 linux”)

                                                                                    default :           notice(“welcome,alien *_*”)

                                               selector:

                                                        用于返回一个值,而不是执行一个代码块;可以将这个值赋值给其他变量;

 

                                                        CONTROL_VARIABLE ?

                                                                 case1 => value1,

                                                                 case2 => value2,

                                                                 ……

                                                                 default => value#

                                                       

                                                        示例:

                                                                 $webservice = $operatingsystem ?

                                                                           /(?i-mx:ubuntu|debian)/   =>   ‘apache2’,

                                                                           /(?i-mx:centos|redhat)/    =>   ‘httpd’,

                                                                     

                                               unless:类似于if;

                            Puppet中的类(class):

                                     用于公共目的的一组资源的集合,类似于代码块;创建后可在puppet的全局进行调用,所以类名在全局应该是唯一的,其可以被应用到所有被管理的节点上;并且类可以被继承;

                                     语法格式:

                                               class class_name

                                                        … puppet code …

                                                        变量

                                                        赋值

                                                        条件判断

                                                        资源声明

                                                        ……

                                              

                                     类的名称只能以小写字母开头,可以包含小写字母、数字、下划线;

                                     每个类都会引入一个新的变脸scope,这意味着在任何时候访问类中的变量时,都得使用其完全限定名称;不过本地scope可以重新为top scope中的变量赋予一个新值;

                                     类也能够接收参数;

                                               class class_name($arg1[=’value1’],$arg2[=‘value2’])

                                                        … puppet code …

                                              

                                     类只有被声明以后才可以使用;

                                               声明变量的方法:

                                                        include方式:

                                                                 include class_name1,class_name,…

                                                        require方式:

                                                                 require class_name1,class_name2,…

                                                        像声明一个资源一样声明一个类:

                                                                 class ‘class_name’ :

                                                                           arg1 => ‘value1’,

                                                                           agr2 => ‘value2’,

                                                                           ……

                                                                

                                                        使用ENC的风格声明一个类

                                     示例:

                                               ~]# cat test13.pp

                                                        class nginx($webserver=’nginx’)    --->默认安装nginx;

                                                                 package $webserver :

                                                                           ensure => latest,

                                                                

 

                                                                 file ‘/etc/nginx/nginx.conf‘:

                                                                           ensure => file,

                                                                           source => ‘/root/modules/nginx/files/nginx.conf‘,

                                                                           require => Package[‘nginx‘],

                                                                           notify => Service[‘nginx‘],

                                                                

 

                                                                 service ‘nginx‘:

                                                                           ensure => running,

                                                                           enable => true,

                                                                           hasrestart => true,

                                                                           hasstatus => true,

                                                                           #restart => ‘systemctl reload nginx.service‘,

                                                                           require => [ Package[‘nginx‘],File[‘/etc/nginx/nginx.conf‘] ],

                                                                

                                                       

                                                       #include nginx

                                                        class ‘nginx’ :

                                                                 webservice => ‘tengine’,     ----->指定安装tengine;

                                                       

                                     类继承:

                                               定义方式:

                                                        class base_class

                                                                 … puppet code …

                                                       

                                                        class [base_class::]calss_name inherits base_class

                                                                 … puppet code …

                                                       

                                               作用:继承一个已有的类,并实现覆盖资源属性或向资源属性追加额外值;覆盖使用”=>”,追加使用“+>”;

                                              类继承时:

                                                        1.声明子类时,其基类(父类)会被自动首先声明;

                                                        2.基类成为了子类的父作用域,基类中的变量和属性默认值会被子类复制一份;

                                                        3.子类可以覆盖父类中同一资源的相同属性的值;

                                              Note:我们可以将具有会被重复使用的资源放在基类(父类)中,将特有的资源设置在子类中;

                                               示例:

                                                                 ~]# cat test14.pp

                                                                           class nginx

                                                                                    $services = $operatingsystemmajrelease ?

                                                                                             /(?i-mx:6)/ => ‘service nginx reload‘,

                                                                                             /(?i-mx:7)/ => ‘systemctl reload nginx.service‘,

                                                                                   

                                                                                    package ‘nginx‘:

                                                                                             ensure => latest,

                                                                                             name => nginx,

                                                                                    ->

        

                                                                                    service ‘nginx‘:

                                                                                             enable => true,

                                                                                             ensure => running,

                                                                                             hasrestart => true,

                                                                                             restart => $services,

                                                                                   

                                                                          

 

                                                                           class nginx::webserver inherits nginx

                                                                                    Package [‘nginx’]

                                                                                             #name +> varnish,   引用父类的package资源,并且追加安装varnish;

                                                                                             name => tengine,    #引用父类的package资源,然后覆盖父类的nginx,改成安装tengine;

                                                                                   

                                                                                    file ‘/etc/nginx/nginx.conf‘:

                                                                                             source => ‘/root/modules/nginx/files/nginx_web.conf‘,

                                                                                             ensure => file,

                                                                                              notify => Server[‘nginx‘],

                                                                                   

                                                                          

 

                                                                           class nginx::proxy inherits nginx

                                                                                    file ‘/etc/nginx/nginx.conf‘:

                                                                                             source => ‘/root/modules/nginx/files/nginx_proxy.conf‘,

                                                                                             ensure => file,

                                                                                             notify => Server[‘nginx‘],

                                                                                   

                                                                          

                                                                           include nginx::webserver

                            Puppet中的模板(基于ERB模板语言):

                                     在静态文件中使用变量等编程元素,生成适用于多种不同环境的文本文件(配置文件);ERB即Embedded Ruby,用于实现在文本文件中嵌入ruby代码,应用时原来的文本文件不会被改变,但ruby代码会被执行,执行结果将直接替换原来的代码;

                                     嵌入代码的方式:

                                               <%= RubyExpression %>:替换为表达式的值;

                                               <% Ruby Expression %>:仅执行代码,而不替换;

                                               <% comment %>:文本注释信息;

                                              <%%:输出为”<%”;

                                               %%>:输出为”%>”;

                                               <%- Ruby Expression %>:执行时忽略空白字符;

                                               <% Ruby Expression -%>:执行时忽略空白行;

                                     在模板中使用的变量,包括Puppet中的任意可用变量,但要以”@”开头;

                                     示例:

                                               ~]# cat /root/modules/nginx/files/nginx.conf

                                                        worker_processes <%= @processorcount %>;

                                                                 将变量嵌入到配置文件的模板中;

                                               ~]# cat test15.pp

                                                        $webserver=nginx

                                                        package $webserver :

                                                                 ensure => latest,

                                                       

 

                                                        file ‘/etc/nginx/nginx.conf‘:

                                                                 ensure => file,

                                                                 content => template(‘/root/modules/nginx/files/nginx.conf‘),

                                                                           将配置文件模板使用puppet的内置函数输出为字节流,然后传输给目标配置文件,只有这样才可以解析配置文件模板中的变量;

                                                                 require => Package[‘nginx‘],

                                                                 notify => Service[‘nginx‘],

                                                       

 

                                                        service ‘nginx‘:

                                                                 ensure => running,

                                                                 enable => true,

                                                                 hasrestart => true,

                                                                 hasstatus => true,

                                                                 #restart => ‘systemctl reload nginx.service‘,

                                                                 require => [ Package[‘nginx‘],File[‘/etc/nginx/nginx.conf‘] ],

                                                       

                                     模块路径:

                                               Puppet中的模块,其实就是一个目录,然后将需要的文件都已规定的格式放在这个目录的特定子目录中,自建的模块一般都会放在/etc/puppet/modules/中;如果想自建模块,只需要在此目录下创建子目录,这个子目录的名称就是模块的名称;

                                               模块目录(module_name/)包含的文件:

                                                        manifests/:用于存放模块使用的资源清单;且至少要包含init.pp这个文件,且这个文件至少要包含一个与当前模块名称同名的类,从而实现自包含、自识别等功能;

                                                        files/:用于存放模块使用的静态文件;并且在管理agent时,agent需要获得这些只有在master端才具有的文件时,master会自动开启一种可以传输文件的功能,通过设置puppet:///modules/module_name/file_name(红色部分为固定格式)将文件传输到agent;

                                                        templates/:用于存放模块使用的模板文件;template(‘module_name/template_name’);

                                                        lib/:插件目录;

                                                        tests/:当前模块的使用帮助及示例文件;

                                                        spec/:类似于tests/目录,存储lib/目录下定义的插件的使用帮助及示例文件;

                                     查找、安装、管理模块:

                                               puppet module <action> [--environment production ]

[--modulepath $basemodulepath ]

                                                        ACTIONS:

                                                               build        Build a module release package.

                                                                changes      Show modified files of an installed module.

                                                               generate     Generate boilerplate for a new module.

                                                               install      Install a module from the Puppet Forge or a release archive.

                                                               list         List installed modules

                                                               search       Search the Puppet Forge for a module.

                                                               uninstall    Uninstall a puppet module.

                                                               upgrade      Upgrade a puppet module.

                                                        详细使用方法请查看:puppet help module

                                     示例:

                                               ~]# mkdir -pv /etc/puppet/modules/nginx/manifests,files,templates,tests,lib,spec

                                               ~]# tree /etc/puppet/modules/

                                                        /etc/puppet/modules/

                                                        └── nginx

                                                        ├── files

                                                      │   ├── nginx_proxy.conf

                                                         │   └── nginx_web.conf

                                                         ├── lib

                                                         ├── manifests

                                                          │   └── init.pp

                                                          ├── spec

                                                          ├── templates

                                                          │   └── nginx.conf.erb

                                                         └── tests

                                               ~]# cd /etc/puppet/modules/nginx/

                                               ~]# cat manifests/init.pp

                                                        class nginx

                                                                 $services = $operatingsystemmajrelease ?

                                                                           /(?i-mx:6)/ => ‘service nginx reload‘,

                                                                           /(?i-mx:7)/ => ‘systemctl reload nginx.service‘,

                                                                

                                                                 package ‘nginx‘:

                                                                           ensure => latest,

                                                                 ->

          

                                                                 service ‘nginx‘:

                                                                           enable => true,

                                                                           ensure => running,

                                                                           hasrestart => true,

                                                                           restart => $services,

                                                                

                                                       

 

                                                        class nginx::webserver inherits nginx

                                                                 file ‘/etc/nginx/nginx.conf‘:

                                                                           #source => ‘/etc/puppet/modules/nginx/files/nginx_web.conf‘,

                                                                           source => ‘puppet:///modules/nginx/nginx_web.conf‘,

                                                                           ensure => file,

                                                                          notify => Service[‘nginx‘],

                                                                

                                                       

                                                        class nginx::proxy inherits nginx

                                                                 file ‘/etc/nginx/nginx.conf‘:

                                                                           content =>template(‘nginx/nginx.conf.erb‘),

                                                                           ensure => file,

                                                                           notify => Service[‘nginx‘],

                                                                

                                                       

                                               ~]# puppet apply -v -e ‘include nginx::webserver‘

                                                        指定要应用的的类;

 

 

      注:根据马哥视频做的学习笔记,如有错误,欢迎指正;侵删

以上是关于Linux Puppet基础知识的主要内容,如果未能解决你的问题,请参考以下文章

Puppet之基础篇

puppet基础篇(练习篇)

45 puppet基础资源详解配置语言puppet类与模板及模块

Puppet基础应用

puppet知识简记

自动化管理工具puppet