puppet自动化之package与service

资源之安装包定义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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,755评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,369评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,799评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,910评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,096评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,159评论 3 411
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,917评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,360评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,673评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,814评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,509评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,156评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,123评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,641评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,728评论 2 351

推荐阅读更多精彩内容