天天看點

puppet java_puppet之java批量安裝案例

利用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尋找其他檔案中定義的類

[[email protected] 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檔案存在才執行該資源。

[[email protected] 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才執行該資源。

[[email protected] 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/定義了一些子產品和節點的資訊

[[email protected] manifests]# tree /etc/puppet/manifests/

/etc/puppet/manifests/

|-- modules.pp

|-- nodes.pp

`-- site.pp

0 directories, 3 files

site.pp是puppet主要入口,告訴puppet從哪裡尋找載入指定的用戶端配置

[[email protected] manifests]# cat site.pp

import "modules.pp"

import "nodes.pp"

子產品導入借口

[[email protected] manifests]# cat modules.pp

import "cron"

import "java"

import "mysql"

節點檔案,在節點檔案裡include子產品

[[email protected] manifests]# cat nodes.pp

node 'agent1.andy.com' {

include cron

include java

#include mysql

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27181165/viewspace-776321/,如需轉載,請注明出處,否則将追究法律責任。