puppet之java批量安裝案例

wang_0720發表於2013-11-11
利用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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章