ITPub博客

首页 > Linux操作系统 > Linux操作系统 > puppet之java批量安装案例

puppet之java批量安装案例

原创 Linux操作系统 作者:wang_0720 时间:2013-11-11 16:44:52 0 删除 编辑
利用puppet对若干台linux服务器安装java,设置java环境变量,本文以一台agent为例安装java
一 服务端模块
新建目录
mkdir -p /etc/puppet/modules/java/manifests
在该目录下有关于安装java的若干个文件
tree /etc/puppet/modules/java/manifests/
/etc/puppet/modules/java/manifests/
|-- init.pp
|-- install_java.pp
`-- set_profile.pp
init.pp是每个模块的必需初始化文件,puppet会根据init.pp寻找其他文件中定义的类
[root@master puppet]# cat /etc/puppet/modules/java/manifests/init.pp
class java {
    include java::install_java
    include java::set_profile
}
install_java.pp中定义了一个install_java类,包含有file,exec资源。file资源是定义从puppet服务端取文件的,本文是从puppet的/etc/puppet/files取文件jdk-6u24-linux-i586.bin,放到agent的/usr/local/src文件名不变。文件属性是:属主root,属组root,权限是755.exec资源定义了执行jdk-6u24-linux-i586.bin这个动作。cwd说明进入某个目录,creates定义如果该文件存在则不执行该资源,command定义执行的命令。require定义了exec资源依赖于file:/usr/local/src/jdk-6u24-linux-i586.bin,必须有/usr/local/src/jdk-6u24-linux-i586.bin文件存在才执行该资源。
[root@master puppet]# cat /etc/puppet/modules/java/manifests/install_java.pp
class java::install_java {
    file {
       "/usr/local/src/jdk-6u24-linux-i586.bin": #copy file to remote machine,File paths must be fully qualified
       owner => "root",
       group => "root",
       mode => 755,
       source => "puppet://$puppetserver/files/jdk-6u24-linux-i586.bin",
    }

    exec {
          "sh /usr/local/src/jdk-6u24-linux-i586.bin":
           cwd => "/usr/local/src/", # end with "/"
           creates => "/usr/local/java", #if the file "/usr/local/java" is not found,do below command.
           command => "sh /usr/local/src/jdk-6u24-linux-i586.bin && mv /usr/local/src/jdk1.6.0_24 /usr/local/java",
           user => "root",
           path => ["/usr/local/sbin","/usr/local/bin","/sbin","/bin","/usr/sbin","/usr/bin"],
           require => File["/usr/local/src/jdk-6u24-linux-i586.bin"],
    }
}
设置java环境变量
unless定义了如果grep -i java_home /etc/profile返回的值为1,即结果为false才执行该资源。
[root@master puppet]# cat /etc/puppet/modules/java/manifests/set_profile.pp
class java::set_profile {
    file {
           "/usr/local/src/java_profile": #copy file to remote machine,File paths must be fully qualified
           owner => "root",
           group => "root",
           source => "puppet://$puppetserver/files/java_profile",
        }
        exec {
           "set profile":
           command => "cat /usr/local/src/java_profile >>/etc/profile && source /etc/profile",
           user => "root",
           path => ["/usr/local/sbin","/usr/local/bin","/sbin","/bin","/usr/sbin","/usr/bin"],
          unless => "grep -i java_home /etc/profile", #if the return value is 1,do this command.
          require => File["/usr/local/src/java_profile"],
        }
}
在/etc/puppet/manifests/定义了一些模块和节点的信息
[root@master manifests]# tree /etc/puppet/manifests/
/etc/puppet/manifests/
|-- modules.pp
|-- nodes.pp
`-- site.pp

0 directories, 3 files
site.pp是puppet主要入口,告诉puppet从哪里寻找载入指定的客户端配置
[root@master manifests]# cat site.pp
import "modules.pp"
import "nodes.pp"
模块导入借口
[root@master manifests]# cat modules.pp
import "cron"
import "java"
import "mysql"
节点文件,在节点文件里include模块
[root@master manifests]# cat nodes.pp
node 'agent1.andy.com' {
include cron
include java
#include mysql
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27181165/viewspace-776321/,如需转载,请注明出处,否则将追究法律责任。

上一篇: puppet之安装配置
请登录后发表评论 登录
全部评论

注册时间:2013-11-05

  • 博文量
    111
  • 访问量
    913621