一個具體實例來簡單說明puppet的具體結構
創建第一個配置
puppet的組成清單這主要包含這幾個部分
資源,文件,模板,節點,類,定義
puppet中有個模塊的定義,這個比較重要,基本是puppet的核心,這個模塊主要是由資源,文件,模板,類,定義來組成的。
puppet的清單文件是以.pp來結尾的,在載入文件的時候就不需要寫.pp的擴展名了。
現在我們來創建第一個模塊:
第一創建模塊
1 進入到這個目錄下 2 cd /etc/puppet/modules/ 3 mkdir motd #創建模塊 4 查看模塊的目錄樹,需要自己創建 5 [root@pup modules]# tree motd/ 6 motd/ 7 ├── files #存放文件目錄 8 │ └── etc 9 │ └── motd #文件 10 ├── manifests #存放模塊pp配置文件目錄 11 │ └── init.pp 12 └── templates #存放模板目錄 13 14 4 directories, 2 files
上面就是幾個基本模塊的目錄,init.pp是模塊的核心配置文件
第二下面我們來寫init.pp文件
1 [root@pup manifests]# cat init.pp 2 class motd{ #定義一個類 3 package{'setup': #定義package資源包 4 ensure => present, #要求setup這個包處於安裝狀態 5 } 6 file{'/etc/motd': #定義file資源 7 ensure => present, #要求文件存在 8 owner => 'root', #要求file屬主為root 9 group => 'root', #要求file屬組為root 10 mode => '0644', 11 source => "puppet:///modules/motd/etc/motd", #文件在服務器上的位置 12 require => Package['setup'], #要求文件在被配置之前執行package資源 13 } 14 }
簡單說明下:
require是個元參數,確保能夠先執行,上面就是在文件在被配置之前執行package資源,
在說明下source,告訴puppet去哪里尋找這個文件,注意路徑是“puppet:///modules/motd/etc/motd“ 而不是這個puppet:///modules/motd//files/etc/motd
”一定要注意沒有files。
為了便於觀察我們在motd文件中添加一點東西
1 [root@pup etc]# cat motd 2 --------------------- 3 this is puppet testing 4 -----------------------
第三來編寫site.pp文件
還記得site.pp文件的位置嗎?在這里
1 [root@pup puppet]# pwd 2 /etc/puppet 3 [root@pup puppet]# tree manifests/ 4 manifests/ 5 └── site.pp 6 7 0 directories, 1 file
1 $puppetserver = 'puppetmaster.com' 2 node 'agent1.puppetmaster.com'{ 3 include motd 4 } 5 #或者使用正則表達式來寫 6 #node /^(agent.*)\.puppetmaster\.com$/ 7 node /^agent\d+\.puppetmaster\.com$/{ 8 include motd #包含這個類 9 } 10 #或者更加簡潔的方式使用通配符 11 node /*.puppetmaster\.com/{ 12 include motd 13 }
第四應用第一個配置
客戶端上執行:

1 [root@agent1 ~]# puppet agent -t 2 Notice: Ignoring --listen on onetime run 3 Info: Retrieving pluginfacts 4 Info: Retrieving plugin 5 Info: Caching catalog for agent1.pup.yxnu 6 Info: Applying configuration version '1483969520' 7 Notice: /Stage[main]/Motd/File[/etc/motd]/content: 8 --- /etc/motd 2010-01-12 21:28:22.000000000 +0800 9 +++ /tmp/puppet-file20170109-2933-1q3m9uz-0 2017-01-09 21:45:21.074912907 +0800 10 @@ -0,0 +1,3 @@ 11 +--------------------- 12 +this is puppet testing 13 +----------------------- 14 15 Info: Computing checksum on file /etc/motd 16 Info: /Stage[main]/Motd/File[/etc/motd]: Filebucketed /etc/motd to puppet with sum d41d8cd98f00b204e9800998ecf8427e 17 Notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}b97562be6190ed905ab4e86a21c4e0c9' 18 Notice: Finished catalog run in 0.71 seconds 19 [root@agent1 ~]# cat /etc/mo 20 modprobe.d/ motd 21 [root@agent1 ~]# cat /etc/motd 22 --------------------- 23 this is puppet testing 24 ----------------------- 25 [root@agent1 ~]#
這樣一個簡單的實例就完成了,可以看到在客戶端上會創建文件/etc/motd,
很簡單吧,接下來會介紹puppet基礎知識,並且介紹如何構建更加復雜的配置