puppet成长日记四 Exec资源详细介绍及案例分析

一、系统环境
1、puppet服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Release:RHEL6.4
HOSTNAME: puppetserver.rsyslog.org
TCP/IP: 172.16.200.100/24
Packages: 
puppet-server-2.7.21-1.el6.noarch
mcollective-client-2.2.4
activemq-5.5.0
2、puppet节点
Release: RHEL5.8
HOSTNAME: agent1.rsyslog.org
TCP/IP: 172.16.200.101/24
Packages:
puppet-2.7.21-1.el5
mcollective-2.2.4-1.el5
3、puppet节点
Release: RHEL6.4
HOSTNAME: agent3.rsyslog.org
TCP/IP: 172.16.200.103/24
Packages:
puppet-2.7.21-1.el6
mcollective-2.2.4-1.el6

二、资源介绍
1、实现功能
1.1 远程执行系统命令,其实就是shell的调用
1.2 由于exec是一次性执行资源,在不同类里面exec名字可相同。
2、支持参数
2.1 command => "mkdir /tmp/rhel5/nginx ", 被执行的命令,必须为被执行命令的绝对路径。

2.2 cwd =>"/tmp/rhel5/nginx", 指定命令执行的目录。如果目录不存在,则命令执行失败。

2.3 environment => "PATH=/home/puppetfans", 为命令设定额外的环境变量。要注意的是如果你用这个来设定PATH,那么PATH的属性会被覆盖。多个环境变量应该以数组的形式来设定。

2.4 group =>  定义运行命令的用户组。在不同的平台下的运行的结果无法确定,由于不同用户运行命令的时候,变量是不变的,所以这是平台的问题,而不是Ruby或Puppet的问题。

2.5 logoutput => on_failure|true|false  是否记录输出。默认会根据exec资源的日志等级(loglevel) 来记录输出。若定义为on_failure,则仅在命令返回错误的时候记录输出。可取的值为:true,false和其他合法的日志等级。

2.6 onlyif =>"/bin/ls /usr/local/nginx/conf", 如果这个参数被设定了,则exec只会在onlyif设定的命令返回0时才执行。

2.7 path => [ "/bin/", "/sbin/" , "/usr/bin/", "/usr/sbin/" ] 命令执行的搜索路径。如果path没有被定义,命令需要使用绝对路径。路径可以以数组或以冒号分隔的形式来定义。

2.8 creates => "/tmp/rhel5/nginx", 指定命令所生成的文件。如果提供了这个参数,那么命令只会在所指定的文件不存在的情况的被执行:

2.9 refresh =>true|false 定义如何更新命令。当exec收到一个来自其他资源的事件时,默认只会重新执行一次命令。不过这个参数允许你定义更新时执行不同的命令。

2.10 refreshonly =>true|false 该属性可以使命令变成仅刷新触发的,也就是说只有在一个依赖的对象被改变时,命令才会被执行。只有同时使用subscribe或notify才有意义

2.11 returns 指定返回的代码。如果被执行的命令返回了其他的代码,一个错误(error)会被返回。默认值是0,可以定义为一个由可以接受的返回代码组成的数组或单值。

2.12 timeout => 0 命令运行的最长时间。如果命令运行的时间超过了timeout定义的时间,那么这个命令就会被终止,并作为运行失败处理。当定义为负值时就会取消运行时间的限制。timeout的值是以秒为单位的。

2.13 unless => "/bin/ls /usr/local/nginx/conf",如果这个变量被指定了,那么exec会执行,除非unless所设定的命令返回0

2.14 user => "nginx",  定义运行命令的用户。 注意如果你使用了这个参数,那么任何的错误输出不会在当下被捕捉,这是Ruby的一个bug。

三、资源示例
1、示例一
1.1 实现功能

*要求实现使用chkconfig命令将节点的iptables和ip6tables服务关闭,并记录错误日志
1.2 配置说明

1
2
3
4
5
6
7
8
9
10
class motd::exec {
        include motd::exec1
}
class motd::exec1 {
        exec {  [  "chkconfig iptables off",
                   "chkconfig ip6tables off",]:
                path  => ["/usr/bin","/usr/sbin","/bin","/sbin"],
                logoutput => on_failure,
        }
}

1.3 客户端agent3测试

1
2
3
4
5
6
7
8
9
10
11
[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply1.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply3.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378284783'
notice: /Stage[main]/Motd::Exec1/Exec[chkconfig iptables off]/returns: executed successfully
notice: /Stage[main]/Motd::Exec1/Exec[chkconfig ip6tables off]/returns: executed successfully
notice: Finished catalog run in 0.17 seconds

2、示例二
2.1 实现功能

*要求节点上创建用户和组nginx,UID和GID都为1000
*要求从服务器下载nginx-0.8.42.tar.gz源码包到节点/tmp/rhel5/nginx目录下
*要求解压源码包,并编译安装到指定目录下
2.2 配置说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class source {
    include source::file1,source::exec1,source::exec2,source::exec3,source::user
    notify { "nstallation nginx package through the source code nginx-0.8.42.tar.gz":
           withpath => true,
        }
}
class source::user{
    group { "nginx":   #建立组nginx
            ensure => present,
            gid => 1000
}
    user { "nginx":   #建立用户nginx
            ensure => present,
            uid => 1000,
                gid => 1000,
            groups => ["nginx"],
#           membership => minimum,
            shell => "/sbin/nologin",
            require => Group["nginx"]
    }
}
class source::file1{  #远程下载nginx源码包
    file{ "nginx":
        name => "/tmp/rhel5/nginx/nginx-0.8.42.tar.gz",
        owner => "root",
                group => "root",
                mode => 0700,
                source => "puppet://$puppetserver/modules/source/nginx-0.8.42.tar.gz",
                backup => 'main',
                require => Class["source::exec1"],
        }
}
class source::exec1{
    exec {"create nginx_pag":
        command => "mkdir /tmp/rhel5/nginx ",
        path => ["/usr/bin","/usr/sbin","/bin","/sbin"],
        creates => "/tmp/rhel5/nginx", #目录或文件不存在的情况下执行command
    }
}
class source::exec2{
    exec { "install nginx":
        cwd =>"/tmp/rhel5/nginx",  #目录存在的情况下执行command
            command =>"tar -zxvf nginx-0.8.42.tar.gz && cd nginx-0.8.42 &&./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --without-http-cache && make&&make install",
        path => ["/usr/bin","/usr/sbin","/bin","/sbin"],
        logoutput => on_failure,
        unless => "/bin/ls /usr/local/nginx/conf",  #命令返回值不为0的情况下执行commond
        require => Class[source::file1,source::user]
        notify => Class["source::exec3"],
    }
}
class source::exec3{
    exec { "updatedb":
        command => "updatedb",
        path => ["/usr/bin","/usr/sbin","/bin","/sbin"],
        refreshonly => true,  #触发更新的时候执行command
        subscribe => Class["source::exec2"],
    }
}
[root@puppetserver manifests]#

2.3 客户端agent3测试

第一次执行:

测试前:

1
2
3
4
5
[root@agent3 rhel5]# cat /etc/passwd | grep nginx
[root@agent3 rhel5]# cat /etc/group | grep nginx
[root@agent3 rhel5]# ll /tmp/rhel5/
total 0
[root@agent3 rhel5]# ll /usr/local/ |  grep nginx

测试中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply1.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply3.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378366520'
notice: /Stage[main]/Source::Exec1/Exec[create nginx_pag]/returns: executed successfully
notice: /Stage[main]/Source/Notify[nstallation nginx package through the source code nginx-0.8.42.tar.gz]/message: nstallation nginx package through the source code nginx-0.8.42.tar.gz
notice: /Stage[main]/Source/Notify[nstallation nginx package through the source code nginx-0.8.42.tar.gz]/message: defined 'message' as 'nstallation nginx package through the source code nginx-0.8.42.tar.gz'
notice: /Stage[main]/Source::File1/File[nginx]/ensure: defined content as '{md5}2818e8b03512b239f1238d702703bcf3'
notice: /Stage[main]/Source::User/Group[nginx]/ensure: created
notice: /Stage[main]/Source::User/User[nginx]/ensure: created
notice: /Stage[main]/Source::Exec2/Exec[install nginx]/returns: executed successfully
info: /Stage[main]/Source::Exec2/Exec[install nginx]: Scheduling refresh of Class[Source::Exec3]
info: Class[Source::Exec2]: Scheduling refresh of Exec[updatedb]
info: Class[Source::Exec3]: Scheduling refresh of Exec[updatedb]
notice: /Stage[main]/Source::Exec3/Exec[updatedb]: Triggered 'refresh' from 2 events
notice: Finished catalog run in 18.83 seconds

测试后:

1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@agent3 rhel5]# cat /etc/passwd | grep nginx
nginx:x:1000:1000::/home/nginx:/sbin/nologin
[root@agent3 rhel5]# cat /etc/group | grep nginx
nginx:x:1000:nginx
[root@agent3 rhel5]# ll /tmp/rhel5/nginx/
total 632
drwxr-xr-x. 8 nginx nginx   4096 Sep  5 14:29 nginx-0.8.42
-rwx------. 1 root  root  642593 Sep  5 14:29 nginx-0.8.42.tar.gz
[root@agent3 rhel5]# ll /usr/local/nginx/
total 16
drwxr-xr-x. 2 root root 4096 Sep  5 14:30 conf
drwxr-xr-x. 2 root root 4096 Sep  5 14:30 html
drwxr-xr-x. 2 root root 4096 Sep  5 14:30 logs
drwxr-xr-x. 2 root root 4096 Sep  5 14:30 sbin

第二次执行:

由于设置了 unless => "/bin/ls /usr/local/nginx/conf", 当命令返回结果为0的时候,exec是不会执行的。其次设置了refreshonly => true,配合notify和subscrive只有在更改的情况下才会触发更新

1
2
3
4
5
6
7
8
9
10
11
[root@agent3 ~]# puppet agent --test
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply1.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply3.rb
info: Loading facts in /var/lib/puppet/lib/facter/my_apply2.rb
info: Caching catalog for agent3.rsyslog.org
info: Applying configuration version '1378366520'
notice: /Stage[main]/Source/Notify[nstallation nginx package through the source code nginx-0.8.42.tar.gz]/message: nstallation nginx package through the source code nginx-0.8.42.tar.gz
notice: /Stage[main]/Source/Notify[nstallation nginx package through the source code nginx-0.8.42.tar.gz]/message: defined 'message' as 'nstallation nginx package through the source code nginx-0.8.42.tar.gz'
notice: Finished catalog run in 0.32 seconds

本文转自凌激冰51CTO博客,原文链接:http://blog.51cto.com/dreamfire/1289475,如需转载请自行联系原作者

puppet成长日记四 Exec资源详细介绍及案例分析相关推荐

  1. puppet成长日记二 Package资源详细介绍及案例分析

    puppet成长日记二 Package资源详细介绍及案例分析 一.系统环境 1.puppet服务端 Release:RHEL6.4 HOSTNAME: puppetserver.rsyslog.org ...

  2. Computer:计算机测试理论(开发/测试/上线)之DEV、SIT、UAT、PRD四套环境详细介绍之详细攻略

    Computer:计算机测试理论之DEV.SIT.UAT.PRD四套环境详细介绍之详细攻略 目录 测试理论 1.DEV.SIT.UAT.PRD四套环境的简介 SIT测试 和UAT测试对比

  3. 第四次作业——个人作业——软件案例分析

    第四次作业--个人作业--软件案例分析 同步更新csdn() 目录(?)[-] 关于 微软必应词典客户端 的案例分析 测试人员郑家兴031302331 测试软件微软必应词典桌面版win7 软件版本35 ...

  4. 三次握手和四次挥手详细介绍

    相对于SOCKET开发者,TCP创建过程和链接折除过程是由TCP/IP协议栈自动创建的.因此开发者并不需要控制这个过程.但是对于理解TCP底层运作机制,相当有帮助. 而且对于有网络协议工程师之类笔试, ...

  5. Cesium中图元Primitive详细介绍及案例

    Cesium从入门到项目实战总目录: 点击 文章目录 Cesium中图元Primitive详细介绍 Cesium中Primitive案例 Cesium中图元Primitive详细介绍 在Cesium中 ...

  6. CICD介绍与案例分析

    一.什么是CI(持续集成)? 可以帮助开发人员更加频繁地(有时甚至每天)将代码更改合并到共享分支或"主干"中. 系统会通过自动构建应用并运行不同级别的自动化测试来验证这些更改,确保 ...

  7. ·工业 4.0 和第四次工业革命详细介绍

    工业 4.0 是制造/生产及相关行业和价值创造过程的数字化转型. 目录 工业 4.0 指南 工业 4.0 与第四次工业革命互换使用,代表了工业价值链组织和控制的新阶段. 网络实体系统构成了工业 4.0 ...

  8. 查看此docker网络连接模式_Docker 网络模式(四种)详细介绍

    Docker 网络模式 本文首先介绍了Docker自身的4种网络工作方式, Docker作为目前最火的轻量级容器技术,有很多令人称道的功能,如Docker的镜像管理.然而,Docker同样有着很多不完 ...

  9. WMframework成长日记(一)——框架介绍

    早打算给wmframework写一个成长日志啦,可是一直没时间,也不晓得,我的时间都被用了干什么啦.wmframework是我自己的一个框架,一个自己琢磨出来的,居于j2EE的系统的解决方式.开始筹划 ...

  10. 以太网 TCP三次握手、TCP四次挥手详细介绍与报文简要分析。

    2.7.0 以太网 TCP协议(三次握手.四次挥手) 传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的.可靠的传输层通信协议. 面向连接:指使用TCP ...

最新文章

  1. OKR能够为企业带来什么价值?如何正确制定OKR?
  2. 排查 Node.js 服务内存泄漏,没想到竟是它?
  3. Java怎么测并发量_如何测试一个web网站的性能(并发数)?
  4. 三、【线性表】线性表概述
  5. FJOI2018二试游记
  6. 2021新交规超速处罚规定
  7. .net程序员安全注意代码及服务器配置
  8. mysql 命令desc tables_oracle中与mysql中的命令 show databases, show tables, desc table类似的命令集...
  9. struct和union,enum分析
  10. Ubuntu设置终端相对短路径
  11. Atitti.dw cc 2015 绿色版本安装总结
  12. java手机编程软件_手机java编程软件下载
  13. java pdf 阅读器_纯Java文档阅读器(word、pdf等)
  14. asp.net panel 加html,ASP.NET中 Panel 控件的使用方法
  15. linux qt qpa linuxfb,Qt 5.4带有Tslib的Linux触摸屏输入在Raspberry Pi上无法使用LinuxFB QPA平台插件...
  16. 向量空间相关概念总结-基
  17. 20.数据集成、数据整合、数据融合
  18. Teigha 40010 保存设置Wipeout时的边界显示问题
  19. GitLab CI/CD Pipeline 与 jobs的概念
  20. 服务器地址怎么查 如何远程登录服务器教程

热门文章

  1. Android检测版本更新
  2. 在vs中创建Analysis Services项目
  3. Observer模式(观察者设计模式)
  4. bzoj2151: 种树
  5. 51nod1122 机器人走方格 V4
  6. STM32+uCOS-II+uc/GUI移植 (uC/GUI API函数学习一)
  7. csp 201712-1 最小差值
  8. Java学习环境的搭建
  9. css实现面包屑导航
  10. Nodejs中cluster模块的多进程共享数据问题