當我們配置完畢puppet相關資訊時,我們需要更近一步來部署puppet真正應用,那puppet怎麼使用呢?希望幫助到初學的朋友,一起交流和學習,歡迎大家分享!
- Puppet常用資源:
- 常用的資源主要有以下幾個:
- file:主要負責管理檔案
- package:軟體包的安裝管理
- service:系統服務的管理
- cron:配置自動任務計劃
- exec:遠端執行運作指令
- 更多資源詳細資料,可參見:http://puppet.wikidot.com
- 寫一個預設配置:vi /etc/puppet/manifests/site.pp
一、File資源:
- file {
- "/tmp/test.txt":
- content => "hello";
- }
- 意思是在/tmp建立一個test.text檔案,檔案内容為hello。
二、Package資源:
- package {
- ["screen","ntp"]:
- ensure => "installed";
- "pppoe":
- ensure => "absent";
- }
- 定義的意思是yum install screen 和ntp服務,并且解除安裝pppoe安裝包。
三、Service資源:
- service {
- "sshd":
- ensure => running;
- "nfs":
- ensure => stopped;
- }
- 意思是定義啟動sshd服務,停止nfs服務。
四、 Cron資源:
- cron{
- "ntpdate":
- command => "/usr/sbin/ntpdate pool.ntp.org",
- user => root,
- hour => 0,
- minute => 0,
- }
- 意思是在用戶端寫入一個計劃任務:0 0 * * * /usr/sbin/ntpdate pool.ntp.org 自動同步時間!
五、向用戶端推送本地腳本:
- 首先修改vi /etc/puppet/fileserver.conf 檔案,添加如下三行:
- [files]
- path /etc/puppet/files
- allow *
- 然後cp所需要的腳本到 /etc/puppet/files目錄,沒有這個目錄則建立!
- file {
- "/tmp/nginx_install.sh":
- source => "puppet://master.puppet.com/files/nginx_install.sh",
- group => root,
- owner => root,
- mode => "755"
- }
- 意思是把 /etc/puppet/files/nginx_install.sh這個腳本推送到用戶端的/tmp/下!
六、Exec遠端執行腳本:
如第五步,我們把nginx_install.sh推送過去後,這時候我們就可以執行了如下:
- exec {
- "/tmp/nginx_install.sh":
- cwd => "/tmp",
- user => root,
- path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],
- }
這樣的配置後,你可以在用戶端測試,測試結果已經自動安裝完nginx!如下結果
- [root@master tmp]# puppetd --server=master.puppet.com --test
- info: Caching catalog for master.puppet.com
- info: Applying configuration version '1337450399'
- notice: /Stage[main]//Node[default]/Exec[/tmp/nginx_install.sh]/returns: executed successfully
- notice: Finished catalog run in 49.96 seconds
- 但是這裡有個地方需要注意,這每次同步都會執行這個腳本,是以我們要設定一個參數,如果nginx_install.sh有更新才執行:
- exec {
- "/tmp/nginx_install.sh":
- cwd => "/tmp",
- user => root,
- path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],
- subscribe => File["/tmp/nginx_install.sh"],
- refreshonly => true;
- }
七、執行指令:
- ###更新sysctl.conf
- file { "/etc/sysctl.conf":
- source => "puppet://master.puppet.com/files/sysctl.conf",
- owner => "root",
- group => "root",
- mode => 644,
- }
- exec {
- "sysctl refresh kernel config":
- path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],
- command => "/sbin/sysctl -p",
- subscribe => File["/etc/sysctl.conf"],
- refreshonly => true
- }