
- Puppet 教程
- Puppet - 首頁
- 基礎 Puppet
- Puppet - 概述
- Puppet - 架構
- Puppet - 安裝
- Puppet - 配置
- Puppet - 環境配置
- Puppet - Master
- Puppet - Agent 設定
- Puppet - SSL 證書設定
- 安裝和配置 r10K
- Puppet - 驗證設定
- Puppet - 編碼風格
- Puppet - 清單檔案
- Puppet - 模組
- Puppet - 檔案伺服器
- Puppet - Facter 和事實
- 高階 Puppet
- Puppet - 資源
- Puppet - 資源抽象層
- Puppet - 模板
- Puppet - 類
- Puppet - 函式
- Puppet - 自定義函式
- Puppet - 環境
- Puppet - 型別和提供程式
- Puppet - RESTful API
- Puppet - 即時專案
- Puppet 有用資源
- Puppet - 快速指南
- Puppet - 有用資源
- Puppet - 討論
Puppet - 類
Puppet 類被定義為資源的集合,這些資源被分組在一起以使目標節點或機器達到期望的狀態。這些類定義在 Puppet 清單檔案中,該檔案位於 Puppet 模組內。使用類的主要目的是減少任何清單檔案或任何其他 Puppet 程式碼中相同的程式碼重複。
以下是一個 Puppet 類的示例。
[root@puppetmaster manifests]# cat site.pp class f3backup ( $backup_home = '/backup', $backup_server = 'default', $myname = $::fqdn, $ensure = 'directory', ) { include '::f3backup::common' if ( $myname == '' or $myname == undef ) { fail('myname must not be empty') } @@file { "${backup_home}/f3backup/${myname}": # To support 'absent', though force will be needed ensure => $ensure, owner => 'backup', group => 'backup', mode => '0644', tag => "f3backup-${backup_server}", } }
在上面的示例中,我們有兩個客戶端,使用者需要存在。可以注意到,我們重複使用了相同的資源兩次。一種不執行相同任務的方法是將這兩個節點組合起來。
[root@puppetmaster manifests]# cat site.pp node 'Brcleprod001','Brcleprod002' { user { 'vipin': ensure => present, uid => '101', shell => '/bin/bash', home => '/home/homer', } }
以這種方式合併節點來執行配置不是一個好的做法。這可以透過建立類並在節點中包含建立的類來簡單地實現,如下所示。
class vipin_g01063908 { user { 'g01063908': ensure => present, uid => '101', shell => '/bin/bash', home => '/home/g01063908', } } node 'Brcleprod001' { class {vipin_g01063908:} } node 'Brcleprod002' { class {vipin_g01063908:} }
需要注意的是類結構是什麼樣的,以及我們如何使用 class 關鍵字添加了一個新的資源。Puppet 中的每個語法都有其自己的功能。因此,選擇的語法取決於條件。
引數化類
如上例所示,我們已經瞭解瞭如何建立類並將其包含在節點中。現在,在某些情況下,我們需要在每個節點上具有不同的配置,例如,當需要在每個節點上使用相同的類擁有不同的使用者時。此功能在 Puppet 中使用引數化類提供。新類的配置將如下例所示。
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp class user_account ($username){ user { $username: ensure => present, uid => '101', shell => '/bin/bash', home => "/home/$username", } } node 'Brcleprod002' { class { user_account: username => "G01063908", } } node 'Brcleprod002' { class {user_account: username => "G01063909", } }
當我們在節點上應用上述 site.pp 清單時,每個節點的輸出將如下所示。
Brcleprod001
[root@puppetagent1 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent1.testing.dyndns.org Info: Applying configuration version '1419452655' Notice: /Stage[main]/User_account/User[homer]/ensure: created Notice: Finished catalog run in 0.15 seconds [root@brcleprod001 ~]# cat /etc/passwd | grep "vipin" G01063908:x:101:501::/home/G01063909:/bin/bash
Brcleprod002
[root@Brcleprod002 ~]# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for puppetagent2.testing.dyndns.org Info: Applying configuration version '1419452725' Notice: /Stage[main]/User_account/User[bart]/ensure: created Notice: Finished catalog run in 0.19 seconds [root@puppetagent2 ~]# cat /etc/passwd | grep "varsha" G01063909:x:101:501::/home/G01063909:/bin/bash
還可以設定類引數的預設值,如下面的程式碼所示。
[root@puppetmaster ~]# cat /etc/puppet/manifests/site.pp class user_account ($username = ‘g01063908'){ user { $username: ensure => present, uid => '101', shell => '/bin/bash', home => "/home/$username", } } node 'Brcleprod001' { class {user_account:} } node 'Brcleprod002' { class {user_account: username => "g01063909", } }
廣告