美文网首页
puppet自动化之package与service

puppet自动化之package与service

作者: 尘曦的雨 | 来源:发表于2017-09-24 15:01 被阅读13次

    资源之安装包定义package

    package:
                        Manage packages.
                        
                        属性:
                            ensure:installed, present, latest, absent, any version string (implies present)
                            name:包名;
                            source:程序包来源,仅对不会自动下载相关程序包的provider有用,例如rpm或dpkg;
                            
    [root@centos7 ~]# cat pacx.pp 定义安装redis软件的资源
    package{'redis':
        ensure => present,
    }
    [root@centos7 ~]# puppet apply -v --noop pacx.pp 
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.42 seconds
    Warning: The package type's allow_virtual parameter will be changing its default value from fa
    lse to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') 这段信息没什么错无
    Info: Applying configuration version '1506259250'
    Notice: /Stage[main]/Main/Package[redis]/ensure: current_value absent, should be present (noop
    )Notice: Class[Main]: Would have triggered 'refresh' from 1 events
    Notice: Stage[main]: Would have triggered 'refresh' from 1 events
    Notice: Finished catalog run in 3.39 seconds
    [root@centos7 ~]# puppet apply -v --noop pacx.pp   费干跑而安装
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.42 seconds
    Warning: The package type's allow_virtual parameter will be changing its default value from fa
    lse to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
    Info: Applying configuration version '1506259250'
    Notice: /Stage[main]/Main/Package[redis]/ensure: current_value absent, should be present (noop
    )Notice: Class[Main]: Would have triggered 'refresh' from 1 events
    Notice: Stage[main]: Would have triggered 'refresh' from 1 events
    Notice: Finished catalog run in 3.39 seconds
    [root@centos7 ~]# rpm -q redis   查看安装是否成功
    redis-3.2.10-2.el7.x86_64
    [root@centos7 ~]# ls
    anaconda-ks.cfg  Downloads                pacx.pp    Videos
    chenxi.pp        initial-setup-ks.cfg     Pictures
    Desktop          jdk-8u144-linux-x64.rpm   jdk 包Public
    Documents        Music                    Templates
    
    [root@centos7 ~]# cat pacx.pp   安装jdk资源并制定版本号
    package{'redis':
        ensure => present,
    }
    package{'jdk':
        ensure => present,
        source => '/root/jdk-8u144-linux-x64.rpm',
        provider => rpm,
    }
    [root@centos7 ~]# puppet apply -v --noop pacx.pp  干跑运行
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.76 
    Warning: The package type's allow_virtual parameter will be changing its default 
    false to true in a future release. If you do not want to allow virtual packages, icitly set allow_virtual to false.   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
    Info: Applying configuration version '1506263349'
    Notice: /Stage[main]/Main/Package[jdk]/ensure: current_value absent, should be pr
    )Notice: Class[Main]: Would have triggered 'refresh' from 1 events
    Notice: Stage[main]: Would have triggered 'refresh' from 1 events
    Notice: Finished catalog run in 0.48 seconds
    [root@centos7 ~]# puppet apply -v  pacx.pp   正式安装jdk包
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.40 
    secondsWarning: The package type's allow_virtual parameter will be changing its default 
    value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
    Info: Applying configuration version '1506263498'
    Notice: /Stage[main]/Main/Package[jdk]/ensure: created
    Notice: Finished catalog run in 23.61 seconds
    [root@centos7 ~]#  ls /usr/java/ 检查是否安装成功
    default  jdk1.8.0_144  latest
    
    

    service 启动服务的资源定义

    [root@centos7 ~]# puppet describe service 查看其资源属性
    
    service
    =======
    Manage running services.  Service support unfortunately varies
    widely by platform --- some platforms have very little if any concept of a
    running service, and some have a very codified and powerful concept.
    Puppet's service support is usually capable of doing the right thing, but
    the more information you can provide, the better behaviour you will get.
    Puppet 2.7 and newer expect init scripts to have a working status command.
    If this isn't the case for any of your services' init scripts, you will
    need to set `hasstatus` to false and possibly specify a custom status
    command in the `status` attribute. As a last resort, Puppet will attempt to
    search the process table by calling whatever command is listed in the `ps`
    fact. The default search pattern is the name of the service, but you can
    specify it with the `pattern` attribute.
    **Refresh:** `service` resources can respond to refresh events (via
    `notify`, `subscribe`, or the `~>` arrow). If a `service` receives an
    event from another resource, Puppet will restart the service it manages.
    The actual command used to restart the service depends on the platform and
    can be configured:
    * If you set `hasrestart` to true, Puppet will use the init script's restart
    command.
    * You can provide an explicit command for restarting with the `restart`
    attribute.
    * If you do neither, the service's stop and start commands will be used.
    
    
    Parameters
    ----------
    
    - **binary**
        The path to the daemon.  This is only used for
        systems that do not support init scripts.  This binary will be
        used to start the service if no `start` parameter is
    provided.
    
    - **control**
        The control variable used to manage services (originally for HP-UX).
        Defaults to the upcased service name plus `START` replacing dots with
        underscores, for those providers that support the `controllable`
        feature.
    
    - **enable**
        Whether a service should be enabled to start at boot.
        This property behaves quite differently depending on the platform;
        wherever possible, it relies on local tools to enable or disable
        a given service.
        Valid values are `true`, `false`, `manual`. 
        Requires features enableable.
    
    - **ensure**
        Whether a service should be running.
        Valid values are `stopped` (also called `false`), `running` (also called
        `true`). 
    
    - **flags**
        Specify a string of flags to pass to the startup script.
        Requires features flaggable.
    
    - **hasrestart**
        Specify that an init script has a `restart` command.  If this is
        false and you do not specify a command in the `restart` attribute,
        the init script's `stop` and `start` commands will be used.
        Defaults to false.
    Valid values are `true`, `false`. 
    
    - **hasstatus**
        Declare whether the service's init script has a functional status
        command; defaults to `true`. This attribute's default value changed in
        Puppet 2.7.0.
        The init script's status command must return 0 if the service is
        running and a nonzero value otherwise. Ideally, these exit codes
        should conform to [the LSB's specification][lsb-exit-codes] for init
        script status actions, but Puppet only considers the difference
        between 0 and nonzero to be relevant.
        If a service's init script does not support any kind of status command,
        you should set `hasstatus` to false and either provide a specific
        command using the `status` attribute or expect that Puppet will look for
        the service name in the process table. Be aware that 'virtual' init
        scripts (like 'network' under Red Hat systems) will respond poorly to
        refresh events from other resources if you override the default behavior
        without providing a status command.
    Valid values are `true`, `false`. 
    
    - **manifest**
        Specify a command to config a service, or a path to a manifest to do so.
    
    - **name**
        The name of the service to run.
        This name is used to find the service; on platforms where services
        have short system names and long display names, this should be the
        short name. (To take an example from Windows, you would use "wuauserv"
        rather than "Automatic Updates.")
    
    - **path**
        The search path for finding init scripts.  Multiple values should
        be separated by colons or provided as an array.
    
    - **pattern**
        The pattern to search for in the process table.
        This is used for stopping services on platforms that do not
        support init scripts, and is also used for determining service
        status on those service whose init scripts do not include a status
        command.
        Defaults to the name of the service. The pattern can be a simple string
        or any legal Ruby pattern, including regular expressions (which should
        be quoted without enclosing slashes).
    
    - **restart**
        Specify a *restart* command manually.  If left
        unspecified, the service will be stopped and then started.
    
    - **start**
        Specify a *start* command manually.  Most service subsystems
        support a `start` command, so this will not need to be
    specified.
    
    - **status**
        Specify a *status* command manually.  This command must
        return 0 if the service is running and a nonzero value otherwise.
        Ideally, these exit codes should conform to [the LSB's
        specification][lsb-exit-codes] for init script status actions, but
        Puppet only considers the difference between 0 and nonzero to be
        relevant.
        If left unspecified, the status of the service will be determined
        automatically, usually by looking for the service in the process
        table.
        [lsb-exit-codes]:
        http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-
        generic/iniscrptact.html
    
    - **stop**
        Specify a *stop* command manually.
    
    Providers
    ---------
        base, bsd, daemontools, debian, freebsd, gentoo, init, launchd, openbsd,
        openrc, openwrt, redhat, runit, service, smf, src, systemd, upstart,
        windows
    ervice:
                        Manage running services.
                        
                        属性:
                            ensure:Whether a service should be running. Valid values are `stopped` (also called `false`), `running` (also called `true`).
                            enable:Whether a service should be enabled to start at boot. Valid values are `true`, `false`, `manual`.
                            name:
                            path:The search path for finding init scripts.  Multiple values should be separated by colons or provided as an array. 脚本的搜索路径,默认为/etc/init.d/;
                            hasrestart:
                            hasstatus:
                            start:手动定义启动命令;
                            stop:
                            status:
                            restart:Specify a *restart* command manually.  If left unspecified, the service will be stopped and then started. 通常用于定义reload操作
    `
    [root@centos7 ~]# cat servicecx.pp  定义启动Redis服务
    service{'redis':
        ensure => true,
        enable => true,
        hasrestart => true,
    }
    [root@centos7 ~]# puppet apply -v --noop servicecx.pp  干跑一下检查是否有问题
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.16 
    secondsInfo: Applying configuration version '1506264815'
    Notice: /Stage[main]/Main/Service[redis]/ensure: current_value stopped, should be
     running (noop)Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis]
    Notice: Class[Main]: Would have triggered 'refresh' from 1 events
    Notice: Stage[main]: Would have triggered 'refresh' from 1 events
    Notice: Finished catalog run in 0.09 seconds
    [root@centos7 ~]# puppet apply -v servicecx.pp    执行
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.14 
    secondsInfo: Applying configuration version '1506264854'
    Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'run
    ning'Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis]
    Notice: Finished catalog run in 0.27 seconds
    [root@centos7 ~]# ss -lnt 检查服务是否启动
    State       Recv-Q Send-Q Local Address:Port               Peer Address:Port     
             LISTEN      0      128    127.0.0.1:6379                     *:*                 
     LISTEN      0      128          *:111                      *:*                  
    LISTEN      0      5      192.168.122.1:53                       *:*             
         LISTEN      0      128          *:22                       *:*                  
    LISTEN      0      128    127.0.0.1:631                      *:*                 
     LISTEN      0      100    127.0.0.1:25                       *:*                 
     LISTEN      0      128         :::111                     :::*                  
    LISTEN      0      128         :::22                      :::*                  
    LISTEN      0      128        ::1:631                     :::*                  
    LISTEN      0      100        ::1:25                      :::*         
    [root@centos7 ~]# systemctl is-enabled redis 检查是否设置了开机启动
    enabled
    

    相关文章

      网友评论

          本文标题:puppet自动化之package与service

          本文链接:https://www.haomeiwen.com/subject/ioekextx.html