Puppet 將 Hiera 取代 Hardcode variables 的理由

2017-09-03 Puppet

Puppet Blog 中提到一篇「Hiera, data and Puppet code: your path to the right data decisions」,裡面提到在以前使用 Hardcoding variables 的時候,你很難處理在不同環境下的給予不同的值

Hardcoding has a negative connotation because typically, when someone would hardcode a value in a script, it represented a workaround where a data item is injected into the code — and mixing data and code means that your code is no longer as generic and extensible as it once was.

 

第二個階段是出現了 variables:

$confdir = '/etc/puppetlabs/puppet'

file { "${confdir}/puppet.conf":
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/mymodule/puppet.conf',
}

file { "${confdir}/puppetdb.conf":
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/mymodule/puppetdb.conf',
}

 

第三階段則是出現了 facts 來處理不同的環境:

$confdir = $facts['kernel'] ?
  'windows' => 'C:\\ProgramData\\PuppetLabs\\puppet\\etc',
  default   =>  '/etc/puppetlabs/puppet',

}

file { "${confdir}/puppet.conf":
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/mymodule/puppet.conf',
}

file { "${confdir}/puppetdb.conf":
  ensure => file,
  owner  => 'root',
  group  => 'root',
  mode   => '0644',
  source => 'puppet:///modules/mymodule/puppetdb.conf',
}

然後有一陣子 Puppet 就衍生了 params.pp 這個 manifests 來特別寫變數。

 

然而現在 Puppet 用 Hiera 來取代了變數的寫法,把所有的 data 都存在 Hiera,幾乎不會有 Hardcoding,而且 Hiera 5 更支援了

three tree 的架構讓參數被覆寫,解決了 data 的彈性,所以更多的 module 開始可以支援所有的平台。

給 Mr. 沙先生一點建議

彙整

分類

展開全部 | 收合全部

License

訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱