標籤:puppet
本文參考了劉宇的《puppet實戰》,並得到他的指導。在此,感謝劉宇。
前一篇文章提到,利用Puppet來給用戶端自動產生在服務端定義好的檔案。
我們這裡繼續引申,來從服務端定義用戶端需要安裝的軟體。
這裡我們以安裝httpd舉例。
在服務端,需要定義模組,我們定義一個httpd模組,名字就叫httpd。
目錄結構如下:
puppet/|-- auth.conf|-- environments| `-- example_env| |-- README.environment| |-- manifests| `-- modules|-- fileserver.conf|-- manifests| |-- nodes| | `-- cahou.linux.net.pp| `-- site.pp|-- modules| |-- httpd| | |-- files| | |-- mainfests| | | `-- init.pp| | `-- templates| `-- test| |-- files| |-- manifests| | `-- init.pp| `-- templates| `-- test.erb`-- puppet.conf15 directories, 9 files
我們這樣來建立目錄和所需的設定檔:
來看看puppet用戶端和服務端的互動過程:
-
puppet用戶端將節點名稱和facts資訊發送給master
-
puppet服務端master通過分類判斷請求的用戶端是誰,它要做什嗎?這個判斷依賴於site.pp中包含的node.pp設定檔完成
-
puppet服務端將用戶端所學的class資訊編譯到catalog裡面去,並把它發送給用戶端
-
用戶端對catalog進行驗證並執行
-
用戶端通過api把執行結果回報給master
site.pp這個檔案是指引去哪裡尋找並載入所有主機相關的配置。
這個檔案預設放在 /etc/puppet/mainfests中,在這個檔案裡面我們會定義一些全域變數。
而在site.pp裡面可以引用其他模組定義的*.pp檔案,以讓檔案結構顯得清晰。
需要提供給用戶端的配置資訊可以任意定義,只要在site.pp裡面import對應的設定檔即可。
我們採用的目錄結構配置是:
/etc/puppet/manifests/nodes
/etc/puppet/modules
#下面我們建立了一個叫做test的模組和叫做cahou.linux.net的節點mkdir -p /etc/puppet/manifests/nodesmkdir -p /etc/puppet/modules/test/{mainfests,templates,files}vi /etc/puppet/modules/test/manifests/init.ppclass test{ file { "/tmp/$hostname.txt": content => "hello,world 1234567890!\n"; }}#在上面的模組中配置了一個變數,叫做$hostname,這個變數需要傳給ERB檔案,這個檔案就放在test類這個目錄下的templates目錄中,檔案名稱與類名一致。cat /etc/puppet/modules/test/templatest/test.erb hostname <% fqdn %>
在上面的步驟裡,我們已經建立了模組test,現在我們需要建立測試節點
cat /etc/puppet/manifests/nodes/cahou.linux.net.ppnode ‘cahou.linux.net‘ { include test}
到此,節點資訊、模組資訊均已建立。
前面講過,puppet會首先讀取site.pp,我們需要讓site.pp能匯入節點資訊和模組資訊
vi /etc/puppet/manifests/site.ppcat site.pp Package {provider => "yum"}node default { file { "/tmp/puppettest1.txt": content => "hello,first puppet test1234567890!\n";}}import "/etc/puppet/modules/httpd/mainfests/init.pp"import "/etc/puppet/manifests/nodes/*.pp"
至此,用戶端可以來向服務端取資訊了,我們可以採用類比執行的方式,這樣,可以看到是否能執行(但並不真正執行)
puppet agent --server cahoa.linux.net --test --verbose --no-daemonize --noopInfo: Retrieving pluginfactsInfo: Retrieving pluginInfo: Caching catalog for cahou.linux.netWarning: The package type‘s allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default‘)Info: Applying configuration version ‘1413640549‘Notice: Finished catalog run in 0.38 seconds
上面的warning資訊,我們暫且不理會,可以看到,已經取到配置資訊了。
下面我們給它加個功能,讓服務端給用戶端安裝httpd。
為此,我們要添加一個模組,
mkdir -p /etc/puppet/modules/httpd/{mainfests,templates,files}
編輯httpd模組檔案,在裡面指定yum源
cat /etc/puppet/modules/httpd/mainfests/init.pp class httpd {yumrepo { "reposohu": descr => "sohu repo", baseurl => "http://mirrors.sohu.com/centos/5.11/os/i386/", gpgcheck => "0", enabled => "1"; } package { "httpd": ensure => installed, require => Yumrepo["reposohu"]; }}
修改節點資訊,添加引用httpd模組:
cat /etc/puppet/manifests/nodes/cahou.linux.net.pp node ‘cahou.linux.net‘ { include test include httpd}
至此,用戶端可以通過向服務端同步資訊來安裝httpd了。
[[email protected] ~]# puppet agent --server cahoa.linux.net --test --no-daemonizeInfo: Retrieving pluginfactsInfo: Retrieving pluginInfo: Caching catalog for cahou.linux.netWarning: The package type‘s allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default‘)Info: Applying configuration version ‘1413640549‘Notice: /Stage[main]/Httpd/Package[httpd]/ensure: createdNotice: Finished catalog run in 61.91 seconds[[email protected] ~]# rpm -q httpd[[email protected] ~]# ll -c /etc/httpd/conf/httpd.conf -rw-r--r-- 1 root root 33726 Oct 18 23:54 /etc/httpd/conf/httpd.conf#從上面的httpd.conf的ctime資訊,我們可以看到,這正是剛剛安裝的httpd。
本文出自 “linux與網路那些事” 部落格,請務必保留此出處http://khaozi.blog.51cto.com/952782/1565561
利用puppet自動給用戶端安裝httpd