{"id":2846,"date":"2012-12-13T01:37:49","date_gmt":"2012-12-13T00:37:49","guid":{"rendered":"http:\/\/www.devco.net\/?p=2846"},"modified":"2012-12-13T19:05:38","modified_gmt":"2012-12-13T18:05:38","slug":"simple-puppet-module-structure-redux","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2012\/12\/13\/simple-puppet-module-structure-redux.php","title":{"rendered":"Simple Puppet Module Structure Redux"},"content":{"rendered":"
Back in September 2009 I wrote a blog post titled “Simple Puppet Module Structure”<\/a> which introduced a simple approach to writing Puppet Modules. This post has been hugely popular in the community – but much has changed in Puppet since then so it is time for an updated version of that post.<\/p>\n As before I will show a simple module for a common scenario. Rather than considering this module a blueprint for every module out there you should instead study its design and use it as a starting point when writing your own modules. You can build on it and adapt it but the basic approach should translate well to more complex modules.<\/p>\n I should note that while I work for Puppet Labs I do not know if this reflect any kind of standard suggested approach by Puppet Labs – this is what I do when managing my own machines and no more.<\/p>\n The module layout I will present below is designed so that someone who is curious about the behaviour of the module only have to look in the init.pp<\/em> to see:<\/p>\n This design will never remove the need for documenting your modules but a clear design will guide your users in discovering the internals of your module and how they interact with it.<\/p>\n More important than what a module does is how accessible it is to you and others, how easy is it to understand, debug and extend.<\/p>\n To go from nowhere to having NTP on your machine you would have to do:<\/p>\n There is a clear implied dependency chain here and this basic pattern applies to most pieces of software.<\/p>\n These 3 points basically translate to distinct groups of actions and sticking with the above principal of single function classes I will create a class for each group.<\/p>\n To keep things clear and obvious I will call these class install<\/em>, config<\/em> and service<\/em>. The names don’t matter as long as they are descriptive – but you really should pick something and stick with it in all your modules.<\/p>\n <\/code><\/p>\n Here I have 3 classes that serve a single purpose each and do not have any details like relationships, ordering or notifications in them. They roughly just do the one thing they are supposed to do.<\/p>\n Take a look at each class and you will see they use variables like $ntp::version<\/em>, $ntp::ntpservers<\/em> etc. These are variables from the the main ntp<\/em> class, lets take a quick look at that class:<\/p>\n <\/code><\/p>\n This is the main entry point into the module that was mentioned earlier. All the variables the module use is documented in a single place, the basic design and parts of the module is clear and you can see that the service class can be notified and the relationships between the parts.<\/p>\n I use the new chaining features to inject the dependencies and relationships here which surfaces these important interactions between the various classes back up to the main entry class for users to see easily.<\/p>\n All this information is immediately available in the obvious place without looking at any additional files or by being bogged down with implementation details.<\/p>\n Line 26 here requires some extra explanation – This ensures that all the NTP member classes are applied before this main NTP class so that cases where someone say require => Class[“ntp”]<\/em> elsewhere they can be sure the associated tasks are completed. This is a light weight version of the Anchor Pattern<\/a>.<\/p>\n Let’s look at how you might use this module from knowing nothing.<\/p>\n Ideally simply including the main entry point on a node should be enough:<\/p>\n <\/code><\/p>\n This does what you’d generally expect – installs, configures and starts the NTP service.<\/p>\n After looking at the init.pp<\/em> you can now supply some new values for some of the parameters to tune it for your needs:<\/p>\n <\/code><\/p>\n Or you can use the new data bindings in Puppet 3 and supply new data in Hiera to override these variables by supplying data for the keys like ntp::ntpservers<\/em>.<\/p>\n Finally if for some or other related reason you need to restart the service you know from looking at the ntp<\/em> class that you can notify the ntp::service<\/em> class to achieve that.<\/p>\n There’s a huge thing to note here in the main ntp<\/em> class. I specify all relationships and notifies on the classes and not the resources themselves.<\/p>\n As personal style I only mention resources by name inside a class that contains that resource – if I ever have to access a resource outside of the class that it is contained in I access the class.<\/p>\n I would not write:<\/p>\n <\/code><\/p>\n These are many issues with this approach that mostly come down to maintenance headaches. Here I require the ntp config file but what if a service have more than one file? Do you then list all the files? Do you later edit every class that reference these when another file gets managed?<\/p>\n These issues quickly multiply in a large code base. By always acting on class names and by creating many small single purpose classes as here I effectively contain these by grouping names and not individual resource names. This way any future refactoring of individual classes would not have an impact on other classes.<\/p>\n So the above snippet would rather be something like this:<\/p>\n <\/code><\/p>\n Here I require the containing class<\/em> and not the resource. This has the effect of requiring all resources<\/em> inside that class. This has the effect of isolating changes to that class and avoiding a situation where users have to worry about the internal implementation details of the other class. Along the same lines you can also notify a class – and all resources inside that class gets notified.<\/p>\n I only include other classes at the top ntp<\/em> level and never have include statements in my classes like ntp::confg<\/em> and so forth – this means when I require the class ntp::config<\/em> or notify ntp::service<\/em> I get just what I want and no more.<\/p>\n If you create big complex classes you run the risk of having refreshonly execs that relate to configuration or installation associated with services in the same class which would have disastrous consequences if you notify the wrong thing or if a user do not study your code before using it.<\/p>\n A consistant style of small single purpose classes named descriptively avoid these and other problems.<\/p>\n There is a lot to learn here and much of it is about soft issues like the value of consistency and clarity of design and thinking about your users – and your future self.<\/p>\n On the technical side you should learn about the effects of relationships and notifications based on containing classes and not by naming resources by name.<\/p>\n And we came across a number of recently added Puppet features:<\/p>\n Parameterized Classes are used to provide multiple convenient methods for supplying data to your module – defaults in the module, specifically in code, using Hiera and (not shown here) an ENC.<\/p>\n Chaining Arrows are used in the main class to inject the dependencies and notifications in a way that is visible without having to study each individual class.<\/p>\n These are important new additions to Puppet. Some new features like Parameterised classes are not quite ready for prime time imho but in Puppet 3 when combined with the data bindings a lot of the pain points have been removed.<\/p>\n Finally there are a number of useful things I did not mention here. Specifically you should study the Puppet Style Guide<\/a> and use the Puppet Lint<\/a> tool to validate your modules comply. You should consider writing tests for your modules using rspec-puppet<\/a> and finally share it on the Puppet Forge<\/a>.<\/p>\n And perhaps most importantly – do not reinvent the wheel, check the Forge first.<\/p>\n","protected":false},"excerpt":{"rendered":" Back in September 2009 I wrote a blog post titled “Simple Puppet Module Structure” which introduced a simple approach to writing Puppet Modules. This post has been hugely popular in the community – but much has changed in Puppet since then so it is time for an updated version of that post. As before I […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7],"tags":[121,85,21],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/2846"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=2846"}],"version-history":[{"count":28,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/2846\/revisions"}],"predecessor-version":[{"id":2875,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/2846\/revisions\/2875"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=2846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=2846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=2846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}The most important deliverables<\/H2>
\nWhen writing a module I have a few things I keep in mind – these are all centered around down stream users of my module and future-me trying to figure out what is going on:<\/p>\n\n
\n
Thinking about your module<\/H2>
\nFor this post I will write a very simple module to manage NTP – it really is very simple, you should check the Forge<\/a> for more complete ones.<\/p>\n\n
Writing the module<\/H3>
\nI’ll show the 3 classes that does the heavy lifting here and discuss parts of them afterwards:<\/p>\n<\/p>\n
\r\nclass ntp::install {\r\n package{'ntpd':\r\n ensure => $ntp::version\r\n }\r\n}\r\n\r\nclass ntp::config {\r\n $ntpservers = $ntp::ntpservers\r\n\r\n File{\r\n owner => root,\r\n group => root,\r\n mode => 644,\r\n }\r\n\r\n file{'\/etc\/ntp.conf':\r\n content => template('ntp\/ntp.conf.erb');\r\n\r\n '\/etc\/ntp\/step-tickers':\r\n content => template('ntp\/step-tickers.erb');\r\n }\r\n}\r\n\r\nclass ntp::service {\r\n $ensure = $ntp::start ? {true => running, default => stopped}\r\n\r\n service{\"ntp\":\r\n ensure => $ensure,\r\n enable => $ntp::enable,\r\n }\r\n}\r\n<\/pre>\n
<\/p>\n
\r\n# == Class: ntp\r\n#\r\n# A basic module to manage NTP\r\n#\r\n# === Parameters\r\n# [*version*]\r\n# The package version to install\r\n#\r\n# [*ntpservers*]\r\n# An array of NTP servers to use on this node\r\n#\r\n# [*enable*]\r\n# Should the service be enabled during boot time?\r\n#\r\n# [*start*]\r\n# Should the service be started by Puppet\r\nclass ntp(\r\n $version = \"present\",\r\n $ntpservers = [\"1.pool.ntp.org\", \"2.pool.ntp.org\"],\r\n $enable = true,\r\n $start = true\r\n) {\r\n class{'ntp::install': } ->\r\n class{'ntp::config': } ~>\r\n class{'ntp::service': } ->\r\n Class[\"ntp\"]\r\n}\r\n<\/pre>\n
Using the module<\/h2>\n
<\/p>\n
\r\ninclude ntp\r\n<\/pre>\n
<\/p>\n
\r\nclass{\"ntp\": ntpservers => [\"ntp1.example.com\", \"ntp2.example.com\"]}\r\n<\/pre>\n
Using classes for relationships<\/h2>\n
<\/p>\n
\r\nclass ntp::service {\r\n service{\"ntp\": require => File[\"\/etc\/ntp.conf\"]}\r\n}\r\n<\/pre>\n
<\/p>\n
\r\nclass ntp::service {\r\n service{\"ntp\": require => Class[\"ntp::config\"]}\r\n}\r\n<\/pre>\n
What we learned and further links<\/h2>\n
\n