How to deal with code duplication in puppet modules?

一个人想着一个人 提交于 2019-12-06 22:26:57

To avoid puppet code duplication, I usually create "common class". I also recommend using hiera to parametrize common class. More about hiera: https://docs.puppetlabs.com/hiera/1/

As an example: class common::installer:

modules
    common
        installer.pp

with code:

class common::installer {
   $packages = hiera("common::installer::packages")
   package { $packages:
     ensure => latest,
   }
}

Next in all modules add

require common::installer #or include common::installer, with this you will avoid "already defined" errors in puppet. 

In hiera, depend on machine configuration you define proper value of common::installer::packages

e.g in machineA.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageC

in machineB.yaml

 common::installer::packages:
   - packageA
   - packageB
   - packageD
   - packageE

This also gives you opportunity to install both modules A and B on one machine without any conflicts.

Just in hiera create proper config machineAandB.yaml with

 common::installer::packages:
   - packageA
   - packageB
   - packageC
   - packageD
   - packageE

You can also consider using ensure_package, to avoid annoying puppet errors with duplicated definition of packages, but with the code above i will not happen.

Similar code you can use to ensure that required services are running.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!