1.puppet apply單機基礎:
參考:http://www.zsythink.net/archives/363
目标:使用puppet apply在單機上實作檔案同步,安裝軟體,開關服務,執行shell。
在/etc/puppet/manifests下生成test1.pp檔案,并進行編輯:
#将resolve.erb檔案同步到/etc/resolv.conf
file{'dns1':
path => '/etc/resolv.conf',
ensure => file,
source => '/etc/puppet/manifests/resolve/resolve.erb',
mode => 0644,
}
#安裝tree軟體
package{'install':
name => 'tree',
ensure => 'installed',
}
#開啟ntpd服務
service{"stop":
name => 'ntpd',
ensure => 'running',
}
#檢測是否存在/etc/resol.conf檔案,如果有,删除
exec{'del':
path => '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin',
cwd => '/etc',
command => 'rm -f resol.conf',
onlyif => 'test -e /etc/resol.conf'
}
2.使用puppet安裝httpd服務的配置檔案:
系統為centos6.5,puppet版本3.8.7。server側共有4個配置檔案,其中主配置檔案/etc/puppet/manifests/site.pp相關配置如下:
node 'puppetclient2'{
include httpd
}
/etc/puppet/modules/httpd/manifests下有init.pp install.pp service.pp 3個配置檔案。
[[email protected] manifests]# cat init.pp
class httpd{
include httpd::install
include httpd::service
}
[[email protected] manifests]# cat install.pp
class httpd::install{
package{'httpd':
ensure => present,
}
}
[[email protected] manifests]# cat service.pp
class httpd::service{
service {'httpd':
ensure => running,
hasstatus => true,
hasrestart => true,
enable => true,
require => Class["httpd::install"],
}
}
在agent上進行驗證:
[[email protected] useless]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppetclient2
Info: Applying configuration version '1551171577'
Notice: /Stage[main]/Httpd::Install/Package[httpd]/ensure: created
Notice: /Stage[main]/Httpd::Service/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Httpd::Service/Service[httpd]: Unscheduling refresh on Service[httpd]
Notice: Finished catalog run in 5.84 seconds
httpd服務已經啟動了:
[[email protected] useless]# /etc/init.d/httpd status
httpd (pid 5598) is running...
網上找到的一段話:
一個子產品目錄下面通常包括三個目錄:files,manifests,templates。manifests 裡面必須要包括一個init.pp的檔案,這是該子產品的初始(入口)檔案,導入一個子產品的時候,會從init.pp開始執行。可以把所有的代碼都寫到init.pp裡面,也可以分成多個pp檔案,init 再去包含其他檔案。files目錄是該子產品的檔案釋出目錄,puppet提供一個檔案分發機制,類似rsync的子產品。templates 目錄包含erb模型檔案,這個和file資源的template屬性有關。