Puppet自动化运维-资源介绍篇(4)

1.什么是资源?

   资源是Puppet最基础的元素,每个资源的定义都具有标题,类型,以及一系列的属性。

 资源定义有如下的特性

    (1) Puppet使用title在编译时区分每个资源,使用命名变量在执行时区分资源

  (2)在同一类型的资源中title和namevar都是唯一的

   (3)每个类型都有部分属性有默认值

  (4)如果不指定namevar,则默认赋予其title的值

 

2.常用的核心资源类型

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

 

  (1)资源介绍-Package安装

  puppet的安装方式有yum,rpm,ports,gem,msi,dpkg,pkg等多种方式 

   Puppet软件管理采用的是package资源,依赖不同的provider来完成各项功能。通常可以选择采用默认的软件包管理来解决各种依赖问题,这些都由Puppet根据操作系统自行判断。

Packages常用的参数:
    ensure:程序的目标状态
    absent:协助安装包
    latest:更新到最新的安装状态
    name:资源的名称
    provider:软件包安装管理器
    source:指定程序包文件路径
     install_options:安装选项,最常用的就是通过INSTALLDIR来指定安装目录

 

          例子1:使用Package安装包

 

[root@puppet-master manifests]# vim install.pp
package { 'mysql-server':
       ensure   => 'present',    ###定义目标是安装,或者使用installed
       source   => "puppet://$puppetserver/modules/mysql-server-5.1.73-5.el6_6.x86_64.rpm",   ###指定安装源路径   
}
 
$puppetserver:是在/etc/puppet/manifests/site.pp上定义的Master主机   
【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet-agent1.nlf.com
Info: Applying configuration version '1482310850'
Notice: /Stage[main]/Mysql::Install/Package[mysql-server]/ensure: created
Notice: Finished catalog run in 12.05 seconds
[root@puppet-agent1 ~]#

 

 

         例子2:使用Package卸载安装包

[root@puppet-master manifests]# cat install.pp
class mysql::install{
package{'mysql-server':
	ensure => 'absent',    ###卸载mysql服务
 }
}
【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet-agent1.nlf.com
Info: Applying configuration version '1482312419'
Notice: /Stage[main]/Mysql::Install/Package[mysql-server]/ensure: removed
Notice: Finished catalog run in 0.38 seconds
[root@puppet-agent1 ~]#

      

      例子3:确保软件包为仓库中最新的版本

[root@puppet-master manifests]# vim install.pp
class mysql::install{
package{'mysql-server':
           ensure => latest,   ###安装最新的mysql安装包
          }
}
【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482371186' Notice: /Stage[main]/Mysql::Install/Package[mysql-server]/ensure: created Notice: Finished catalog run in 23.35 seconds [root@puppet-agent1 ~]#

      例子4:安装多个软件包


[root@puppet-master manifests]# vim install.pp
class mysql::install{
package{
    ['nginx','httpd','mysql-server']:  ###安装多个安装包,采用数组形式安装
    ensure => installed,
  }
}
【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482373219' Notice: /Stage[main]/Mysql::Install/Package[nginx]/ensure: created Notice: /Stage[main]/Mysql::Install/Package[mysql-server]/ensure: created
Notice: /Stage[main]/Mysql::Install/Package[httpd]/ensure: created
[root@puppet-agent1 ~]#

  

 

      例子5:不同操作系统选择不同的软件包

 

[root@puppet-master manifests]# vim install.pp
   case $::operatingsystem{
        CentOS:{
              $package_list = 'mysql'
        }
        solaris:{
              $package_list = 'httpd'
        }
        default:{
              fail("Module does not support ${::operatingsystem}")
        }
   }
   package { $package_list:
        ensure =>present,
   }
}

注释:
$::operatingsystem:puppet调用系统类型参数类型,如Centos,solaris,SUSE等类型,可以使用命令#facter -p查看系统相关的信息
$package_list:做个别名,方便后面package的针对系统情况调用

 

  (2)资源介绍-service管理

 

  service 常用参数;
    ensure:服务的目标状态(true,false两种)
    enable:是否开机自启动服务(true,false两种)
    name:服务名称
    path:服务脚本路径

  服务执行操作【start,stop,restart,status,hasrestart,hasstatus】
    hasrestart:指出管理脚本是否支持restart参数,若不支持,就使用stop/start实现restart的效果。可设置为true或者false
    hasstatus:指出管理脚本是否支持status参数,可设置为true或者false
    pattern:设置搜索进程的匹配字符串,当init脚本不支持restart/status命令用来在进程列表中找到此服务进程以重启或者确定状态
    restart:重启命令
    start:启动命令
    status:状态命令
    stop:停止命令

 

 

        例子1:启动服务命令

 

[root@puppet-master manifests]# vim service.pp

class mysql::service{
     service{'mysqld':
           ensure => "running",      ###启动mysql服务
      enable => true,            ###开机启动服务,不启动使用true改成false
      path   => "/etc/init.d",   ###指定路径,确保服务运行并随系统启动而启动
     }
}
【Agent】下发catalog

[root@puppet-agent1 ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet-agent1.nlf.com
Info: Applying configuration version '1482374624'
Notice: /Stage[main]/Mysql::Service/Service[mysqld]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Mysql::Service/Service[mysqld]: Unscheduling refresh on Service[mysqld]
Notice: Finished catalog run in 1.58 seconds
[root@puppet-agent1 ~]#

 

 

      例子2:触发服务重启

 

[root@puppet-master manifests]# vim service.pp

class mysql::service{
     service{'httpd':
           ensure     => "running",
           enable     => true,
           hasrestart => true,
           hasstatus  => true,
           restart    => "/etc/init.d/httpd reload",            ###重启httpd服务
           subscribe  => File["/etc/httpd/conf/httpd.conf"],    ###监听配置文件,当其有修改,立即出发重启httpd服务
     }
     file{"/etc/httpd/conf/httpd.conf":
           ensure     => file,
           source     => 'puppet:///modules/mysql/httpd.conf',  ###指向配置文件的存放路径
           notify     => Service["httpd"],              ###通知service,当配置文件有修改的时候
     }
}

【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482376003' Notice: /Stage[main]/Mysql::Service/Service[httpd]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Mysql::Service/Service[httpd]: Unscheduling refresh on Service[httpd] Notice: Finished catalog run in 0.40 seconds Agent端测试httpd服务是否开启 [root@puppet-agent1 ~]# lsof -i:80 ###查看端口服务是否开启成功 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 6396 root 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6400 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6401 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6402 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6403 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6404 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6405 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6406 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) httpd 6407 apache 4u IPv6 26943 0t0 TCP *:http (LISTEN) 修改配置文件httpd.conf(存放在/etc/puppet/modules/httpd/files下面,files是专门存放模块文件的地方),将端口80改成8080端口,测试触发service重新启动 [root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482377523' Notice: /Stage[main]/Mysql::Service/File[/etc/httpd/conf/httpd.conf]/content: --- /etc/httpd/conf/httpd.conf 2014-08-15 14:57:48.000000000 +0800 +++ /tmp/puppet-file20161222-7971-ilhkw2-0 2016-12-22 19:32:02.573667445 +0800 @@ -133,7 +133,7 @@ # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 -Listen 80 +Listen 8080 # # Dynamic Shared Object (DSO) Support Info: Computing checksum on file /etc/httpd/conf/httpd.conf Info: /Stage[main]/Mysql::Service/File[/etc/httpd/conf/httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 27a5c8d9e75351b08b8ca1171e8a0bbd Notice: /Stage[main]/Mysql::Service/File[/etc/httpd/conf/httpd.conf]/content: content changed '{md5}27a5c8d9e75351b08b8ca1171e8a0bbd' to '{md5}b7ca7a0e786418ba7b5ad84efac70265' Info: /Stage[main]/Mysql::Service/File[/etc/httpd/conf/httpd.conf]: Scheduling refresh of Service[httpd] Info: /Stage[main]/Mysql::Service/File[/etc/httpd/conf/httpd.conf]: Scheduling refresh of Service[httpd] Notice: /Stage[main]/Mysql::Service/Service[httpd]: Triggered 'refresh' from 2 events Notice: Finished catalog run in 0.59 seconds [root@puppet-agent1 ~]# lsof -i:80 ###检测原来的httpd服务80端口已被关闭 [root@puppet-agent1 ~]# lsof -i:8080 ###修改后的httpd服务的8080端口已被开启 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 6396 root 6w IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8178 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8179 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8180 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8181 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8182 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8183 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8184 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) httpd 8185 apache 6u IPv6 32082 0t0 TCP *:webcache (LISTEN) [root@puppet-agent1 ~]#

  

    (3)资源介绍-User管理

      常用属性:

          ensure:目标状态

          选项:home shell gid uid password system comment

  

       例子1:创建普通用户cfj

 

[root@puppet-master manifests]# vim install.pp

user {'cfj':
     ensure     => present,    ###建立用户
     uid        => '502',      ###指定uid号
     groups     => 'system',      ###指定用户组,此用户组需要先创建
     home       => '/home/cfj',   ###指定用户的家目录
     shell      => '/bin/bash',   ###指定shell类型
     expiry     => '2016-12-26',  ###指定用户过期日期
  }
}

【Agent】下发catalog
[root@puppet-agent1 ~]# groupadd system ###创建system [root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482724108' Notice: /Stage[main]/Mysql::Install/User[cfj]/ensure: created Notice: Finished catalog run in 0.13 seconds [root@puppet-agent1 ~]# id cfj ###验证用户是否创建 uid=502(cfj) gid=502(cfj) groups=502(cfj),500(system) [root@puppet-agent1 ~]#

       

      例子2:删除用户cfj

 

[root@puppet-master manifests]# vim install.pp

user {'cfj':
    ensure => absent,    ###删除用户
  }
}

[root@puppet-agent1 ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet-agent1.nlf.com
Info: Applying configuration version '1482725099'
Notice: /Stage[main]/Mysql::Install/User[cfj]/ensure: removed
Notice: Finished catalog run in 0.12 seconds
[root@puppet-agent1 ~]# id cfj     ###测试用户是否删除
id: cfj: No such user
[root@puppet-agent1 ~]#

  

    (4)资源介绍- Group管理

        Group组使用方法跟User类似 

        常用属性:
          ensure:目标状态
          name:组名称
          gid:GID
          system:系统组

  

      例子1:创建用户组

[root@puppet-master manifests]# vim install.pp

group {'company':                 ###创建company组
     ensure   =>  present,
     name     => 'company',       ###指定组名称
     gid      => '5560',          ###指定GID号
     members  => ['www','yyy'],   ###指定加入改组的用户成员
 }
}

【Agent】下发catalog
[root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1482725855' Notice: /Stage[main]/Mysql::Install/Group[company]/ensure: created Notice: Finished catalog run in 0.11 seconds [root@puppet-agent1 ~]# id www    ###验证用户是否添加到company组 uid=500(www) gid=5561(www) groups=5561(www) [root@puppet-agent1 ~]# id yyy ###验证用法是否添加到conpany组 uid=501(yyy) gid=501(yyy) groups=501(yyy) [root@puppet-agent1 ~]#

     

    例子2:删除用户组

[root@puppet-master manifests]# vim install.pp

group {'company':
    ensure => absent,
 }
}

【Agent】下发catalog

[root@puppet-agent1 ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppet-agent1.nlf.com
Info: Applying configuration version '1482730985'
Notice: /Stage[main]/Mysql::Install/Group[company]/ensure: removed
Notice: Finished catalog run in 0.15 seconds

  

  (5)资源介绍- Cron管理

  定时脚本用于安装和管理系统cron作用,每一个cron的资源要有命令和至少一个周期的属性(hour小时,minute分钟,month月,monthday月的第一天,special特殊)。定义脚本这些属性都由Puppet存储与检索。

  Puppet会将任务写入/var/spool/cron目录中与用户对应的文件中,/etc/crontab中原有的任务不会变,如果Puppet指定的任务与信啊有任务完全一致,Puppet不会对此有任何的修改。建议cron完全有Puppet管理,不然可能会出现难以预料的情况,其中特别要注意的就是出现定义了不同的任务而任务目的完全一致的情况。

    cron资源常用参数:

        ensure;   目标状态(present,abset)
        command:  需要执行的命令,通常使用双引号引起来
        environment:运行的环境变量配置,例如PATH=/bin/:/usr/bin/;/usr/sbin
        hour:     小时(0-23)
        minute:  分钟(0-59)
        month:   月(1-12)
        monthday: 月中的天(1-31)
        weekday:  周中的天(0-7)
        name:  名称
        user:  指定运行的用户,默认是root
        target:  指定用户的cron项,默认为用户的shell
        special: 特殊的配置
        provider:指定后端使用cron资源,通常不指定

  

 

     例子1:假设每天早上6点执行一次ntpdate

 

【crontab写法如下】
#crontab -l
0 6 * * * /usr/sbin/ntpdate ntpserver.nlf.com

    在Puppet的写法如下:

[root@puppet-master manifests]# vim install.pp

cron {'ntpdate':
    command   => "/usr/sbin/ntpdate ntpserver.nlf.com",
    user      => root,
    hour      => 6,
    minute    => 0,
  }
}

  

      例子2:假设每两个小时执行一次ntpdate

 

【crontab 写入如下】
#crontab -l
0 */2 * * * /usr/sbin/ntpdate ntpserver.nlf.com

    在Puppet的写法如下:

[root@puppet-master manifests]# vim install.pp

cron {'ntpdate':
    command   => "/usr/sbin/ntpdate ntpserver.nlf.com",
    user      => root,
    hour      => '*/2',
    minute    => 0,
  }
}

    

     例子3:假设晚上11点到早上8点之间每两个小时,以及早上8点执行一次ntpdate

 

【crontab 写入如下】
#crontab -l
0 23-7/2,8 * * * /usr/sbin/ntpdate ntpserver.nlf.com

    在Puppet的写法如下:

[root@puppet-master manifests]# vim install.pp

cron {'ntpdate':
    command   => "/usr/sbin/ntpdate ntpserver.nlf.com",
    user      => root,
    hour      => '23-7/2,8',
    minute    => 0,
  }
}

  

 

    (6)资源介绍- exec管理

  执行外部命令,通常建议减少使用或者尽量避免exec资源,Puppetlabs也不建议使用,创建exec资源是为完成一些Puppet无法完成的事情。

  尽量少使用exec资源,同时在使用exec资源时尽量使用refreshonly和creates属性,少用onlyif和unless属性。即使要使用exec资源也要用简单的系统命令,因为每次Puppet执行时候所编译的Catalog中的命令都会去执行,这样会带来大量的胸痛开销与不必要的资源浪费。

 

   exec常用的属性:

      creates:  文件不存在才执行
      refreshonly:收到同时时才执行
      onlyif:  命令执行成功返回0执行
      unless:  命令执行不成功返回非0时执行

    exec资源常用的参数:

      command:  执行的命令,必须是完整的,合法的命令
      creates:  指定命令生成的文件;如果提供了这个参数,那么命令只会在所指定的文件不存在情况下才会被执行
      cwd:    指定命令执行的当前目录,如果目录不存在,则会被执行失败
      environment:为命令设置额外的环境变量,指定path,会覆盖path的定义,多个环境变量应该
      group:  运行命令的用户组
      logoutput:是否记录输出,默认为on_failure,还可以定义为true或false
      onlyif:  参数中设置的命令返回0才执行
      path:   执行命令的搜索路径,如果不指定时命令必须是全部路径,可以采用全路径数组,使用冒号进行分隔
      refresh:  定义如何更新此命令,在exec接收到其他资源并通知刷新,默认在执行一次,但可以定义一个不同的命令
      refreshonly:该参数可以使命令变成仅刷新触发的,也就是说只有在一个依赖的对象被改变时,命令才会被执行,仅当命令与其他对象有依赖关系,这个参数才有意义。当要触发某个行为时,这个参数会显得很有用。
      timeout: 命令执行超时时间,默认300s
      returns: 命令返回码,默认返回0,可指定返回码或单个值的数组
      tires:  尝试执行次数,默认为1,如果命令返回值与returns返回码不一致将会重试
      unless:  结束代码,正常情况下命令返回值是标准输出内容,为0才执行
      user:    定义执行命令的用户

 

  

      例子1:在Linux下执行echo “foo” 输出 

[root@puppet-master manifests]# vim install.pp

exec {'echo foo':
    command  => 'echo "foo"',
    path     => "/usr/bin:/usr/sbin:/bin",    ###需要指定path路径,否则Puppet会找不到执行命令的路径
 }
}

 

  

      例子2:解压xxx.tar.gz文件到/tmp目录,如果“/tmp/myfile”,文件不存在,则执行exec命令;如果文件存在,则不执行。

[root@puppet-master manifests]# vim install.pp

exec {'tar -zxf /tmp/nginx-1.3.8.tar.gz':
     cwd      => '/tmp',       ###进入tmp目录
     creates  => '/tmp/myfile',   ###当/tmp/myfile不存在时候,执行exec命令
     path     => ['/usr/bin/','/usr/sbin','/bin'],
  }
}

 

 

      例子3:当命令返回值为0时候,exec资源命令才会被执行

[root@puppet-master manifests]# vim install.pp

exec {'nginx -s reload':
     path         => '/usr/bin:/usr/sbin:/bin',
     refreshonly  => true,
     onlyif       => 'nginx -t /etc/nginx/nginx.conf',
     tries        => 3,
     try_sleep    => 5,
}
}

以上代码定义一条命令“nginx -s reload”,只有当"nginx -t /etc/nginx/nginx.conf"命令返回0时,”nginx -s reload“ 才会执行,同样定义了命令重复次数为3次,每次相隔5秒

  

    

     例子4:当文件被更新触发该资源被执行

[root@puppet-master manifests]# vim install.pp

file {'/etc/aliases':
     source => 'puppet://server/modules/aliases',
 }
exec {'newaliases':
     path       => ["/usr/bin","/usr/sbin"],
     subscribe  => File["/etc/aliases"],
     refreshonly  => true,
 }
}


以上代码中,只有当文件"/etc/aliases" 再次被更新时才会触发newaliases动作,需要注意的是,只有subscribe和notify可以触发行为,所有refreshonly需要配置subscribe和notify才有意义;
refreshonly属性的默认值为false,可接受的值有true和false

 

      (7)资源介绍- file管理

  File资源也是我们最常用的资源之一,其配置方法具有多元化,它也是影响Puppet执行效率的关键。

     File作用:

      (1)管理文件,目录,符号链接

      (2)管理文件内容,属性,权限

      (3)通过属性来指定文件来源,也可以通过source属性从远处服务器下载

      (4)设置recurse属性来true或local来直接传输整个目录

 

  常用属性:

        ensure;目标状态(present,absent,file,directory)
        backup:通过filebucket资源来备份文件,值通常为filebucket资源的名称
        content:文件内容,生成方式三种(content,source,target)
        source:通过制定的url下载到本地,获取方式通常为puppet url,格式为puppet://modules/MODULE_name/file_name
        target:指定符号链接
        link:文件为符号链接
        path:文件路径
        mode:定义权限
        owner:属主
        group:属组
        force:强制执行删除文件
        purge:清空指定目录中的存在的,但未在资源中定义的文件
        recurse:目录递归,值为true,false,inf,remote
        replace:替换,本地存在的文件与资源中指定的文件内容不同时是否执行替换,默认是否

 

  

     例子1:file创建文件  

  方式1:
[root@puppet-master manifests]# cat install.pp
class mysql::install{ file{'/tmp/test': ensure => file, } } 【Agent】下发catalog

[root@puppet-agent1 ~]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppet-agent1.nlf.com Info: Applying configuration version '1483501191' Notice: /Stage[main]/Mysql::Install/File[/tmp/test]/ensure: created Notice: Finished catalog run in 0.10 seconds

  

  方式2:
[root@puppet-master manifests]# cat install.pp
class mysql::install{
file{'test':
   path   => '/tmp/file',
   ensure => present,
 }
}

以上代码中的写法为简写方式,也是相对比较规范的写法,这种简写方式可以运用于其他资源;
在使用require等元参数进行资源依赖的时候可以直接指定资源的title,不用写太长的path内容,例如代码如下:
  require => File['/tmp/test'],

  

    例子2: 创建一个目录
file {'/tmp/testdir':
  ensure => directory,
}

     例子3: 创建一个符号链接

file {'/tmp/test':
  ensure  => 'link',
  target   =>'/tmp/testlink',
}
    定义ensure属性代码为link,代表该文件资源是软链接,使用target属性指定目标文件,即为软链接

          

     例子4:将服务器文件同步至本地

 

file {'test':
  name   => ‘/tmp/test’,
  ensure => present,
  source  => 'puppet://$fileserver/modules/test/test'
}
在以上代码中,定义了从Puppet Master服务器上读取文件至本地,变量$fileserver在site.pp文件中定义“$fileserver=puppet.domain.com”.这个时候test文件需要创建在/etc/puppet/files/"{modules_name}"/test中

          

  

 

  

 

posted @ 2016-12-22 09:19  善战者求之于势  阅读(2287)  评论(0编辑  收藏  举报