<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Heston Snodgrass</title>
    <description>The latest articles on Forem by Heston Snodgrass (@hsnodgrass).</description>
    <link>https://forem.com/hsnodgrass</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F13879%2F2471064d-685c-49c9-b3ae-668ce52b5e70.jpeg</url>
      <title>Forem: Heston Snodgrass</title>
      <link>https://forem.com/hsnodgrass</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hsnodgrass"/>
    <language>en</language>
    <item>
      <title>Features, the forgotten feature of Puppet</title>
      <dc:creator>Heston Snodgrass</dc:creator>
      <pubDate>Mon, 15 Nov 2021 17:12:29 +0000</pubDate>
      <link>https://forem.com/puppet/features-the-forgotten-feature-of-puppet-12p2</link>
      <guid>https://forem.com/puppet/features-the-forgotten-feature-of-puppet-12p2</guid>
      <description>&lt;p&gt;When you write enough Puppet code, you will eventually find yourself in need of a Facter fact or Puppet resource type that doesn’t exist in Puppet itself. Then, if you’re like me, you go to the &lt;a href="https://puppet.com/ecosystem/forge/" rel="noopener noreferrer"&gt;Puppet Forge&lt;/a&gt; and see if someone else has written what you need. Oftentimes, you find what you need, add a new module to your Puppetfile or module metadata, and move on with your life.&lt;/p&gt;

&lt;p&gt;However, sometimes your search turns up blank and you are confronted with a choice: abandon what you are trying to do, or; write a new custom fact or a new custom type / provider. &lt;/p&gt;

&lt;p&gt;When you choose the latter, writing a custom feature or two can save you a lot of heartache. Don’t know what a custom feature is? No worries, this post will walk you through what a custom feature is, when you should use them, and how to write them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The scenario
&lt;/h2&gt;

&lt;p&gt;A development team just finished writing some new software, AwesomeApp. You, being the resident Puppet aficionado, have been tasked with writing the Puppet code to deploy and configure this software across select machines in your diverse fleet of nodes. Since AwesomeApp is brand new and awesomely different from other software you currently manage, you realize that you will need to write custom Puppet code to deploy and configure the software. &lt;/p&gt;

&lt;p&gt;As you continue reading over the requirements from the developers, you realize that you will also need a custom fact and custom type and provider to deploy and configure AwesomeApp. The catch is that the custom fact and the custom type / provider will need AwesomeApp dependencies installed on the node, and not every node will have AwesomeApp installed on it. This means that you will need to confine your fact and type / provider to suitable nodes only so you don’t install those dependencies where they don’t belong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Confinement and suitability
&lt;/h2&gt;

&lt;p&gt;Custom Facter facts and custom types / providers provide the concepts of confinement and suitability to help you accomplish this task. Confinement means what you think it does: confine this code to only execute if a condition is met. Suitability is also fairly self-descriptive: make sure this node is suitable for this code before this code is executed, often by evaluating any confinement conditions. &lt;/p&gt;

&lt;p&gt;Here’s what confinement looks like in the code of a custom Facter fact (it looks nearly identical in custom type / provider code as well):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require ‘puppet’

Facter.add(:cem_inetd) do
  confine kernel: 'Linux'
  ...
  setcode do
    ...
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Confining a custom Facter fact or custom type / provider involves using the confine function to, typically, only allow resolution of the fact on nodes that can satisfy the parameters you have given it. In the case of the example above, we will only resolve this fact on nodes that have a Facter fact called &lt;code&gt;kernel&lt;/code&gt; that resolves to &lt;code&gt;Linux&lt;/code&gt; because that is the only value that the fact has deemed suitable. In other words, only Linux nodes will have this fact; Windows nodes will not, because Puppet will evaluate the condition to prove suitability before execution.&lt;/p&gt;

&lt;p&gt;Back to our example scenario. You now know that you can leverage confinement and suitability for the AwesomeApp fact and type / provider, but you don’t have a fact to use with &lt;code&gt;confine&lt;/code&gt; that states whether or not the dependencies are installed. You start thinking over your options. &lt;br&gt;
You could isolate those nodes in their own environments, but that would introduce more complexity to your node classification.&lt;/p&gt;

&lt;p&gt;You could bake the check into the custom fact and custom type / provider code itself, but that would introduce a lot of conditional statements and make your new code more error-prone. &lt;/p&gt;

&lt;p&gt;If only Puppet had some sort of feature besides facts that you could use in this situation. A lightweight, easy to use feature that could briefly evaluate suitability on a node and be used with &lt;code&gt;confine&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;What would you even call a feature like that, though?&lt;/p&gt;
&lt;h2&gt;
  
  
  Feature is the feature
&lt;/h2&gt;

&lt;p&gt;Fortunately for us, Puppet does provide this feature in the form of Features. I know, the terminology can be a bit confusing. Features are small snippets of Ruby code that evaluate suitability on a node and expose a function that can be used with &lt;code&gt;confine&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Trust me, it’s much simpler than it sounds. &lt;/p&gt;

&lt;p&gt;To prove this, let’s take a look at an actual feature used by the Compliance Enforcement Module for Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# lib/puppet/feature/cem_inetd.rb
require 'puppet/util/feature'

Puppet.features.add(:cem_inetd) do
  inetd = `sh -c 'command -v inetd'`.strip
  xinetd = `sh -c 'command -v xinetd'`.strip
  inetd.empty? &amp;amp;&amp;amp; xinetd.empty? ? false : true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all there is to a feature. Features are nothing but some Ruby code that declare a feature name and return &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, or &lt;code&gt;nil.&lt;/code&gt; Features can then be used with &lt;code&gt;confine&lt;/code&gt; like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# lib/facter/cem_inetd.rb
require 'puppet'

Facter.add(:cem_inetd) do
  confine kernel: 'Linux'
  confine { Puppet.features.cem_inetd? }
  setcode do
    ...
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy right? Now for some details.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where features are found
&lt;/h3&gt;

&lt;p&gt;Features can be added to a Puppet module by adding a ruby file to the path &lt;code&gt;lib/puppet/feature/.&lt;/code&gt; The convention is to name your ruby file after the feature name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Feature return values
&lt;/h3&gt;

&lt;p&gt;Each of the three acceptable feature return values don’t just determine suitability; they also influence how Puppet caches the value of the feature.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A feature that returns &lt;code&gt;true&lt;/code&gt; has that value cached and the feature code will not be executed again on that node&lt;/li&gt;
&lt;li&gt;A feature that returns &lt;code&gt;false&lt;/code&gt; has that value cached and the feature code will not be executed again on that node&lt;/li&gt;
&lt;li&gt;A feature that returns &lt;code&gt;nil&lt;/code&gt; &lt;strong&gt;does not cache&lt;/strong&gt; that value, and the feature code will execute on that node at every Puppet run.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, when writing features it is important to think about what exactly you are checking. You should only return &lt;code&gt;false&lt;/code&gt; if the check will never be true in the future. You should return &lt;code&gt;nil&lt;/code&gt; if the check could possibly be true in the future.&lt;/p&gt;

&lt;h3&gt;
  
  
  Confine with features
&lt;/h3&gt;

&lt;p&gt;When Puppet detects that a new feature has been added, it automatically creates a new function that is available to use: &lt;code&gt;Puppet.features.&amp;lt;feature name&amp;gt;?&lt;/code&gt;. What this function does is return the cached value of the feature, or run the feature code and cache / return the value. When you use this function with &lt;code&gt;confine&lt;/code&gt;, it is important to remember to pass the feature function inside of a block (the curly braces) because otherwise you will get an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  An awesome feature for AwesomeApp
&lt;/h2&gt;

&lt;p&gt;So now that we know all about features, let’s write the features we need for AwesomeApp. Since AwesomeApp depends on &lt;code&gt;xinetd&lt;/code&gt; because it listens for network requests to spawn system services (AwesomeApp may not actually be that awesome…), we can reuse the feature from the example code above. Remember, since &lt;code&gt;xinetd&lt;/code&gt; is not something we can reasonably expect to exist on a node at some time in the future, we just return &lt;code&gt;false&lt;/code&gt; if we can’t find it.&lt;/p&gt;

&lt;p&gt;The next feature we need has to validate that two Ruby gems, awesome_gem and gem_awesome, are installed and available to Puppet. Fortunately, features have one more feature that makes this super easy. Here is our new feature code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# lib/puppet/feature/awesome_ruby_deps.rb
require 'puppet/util/feature'

Puppet.features.add(:awesome_ruby_deps, libs: [‘awesome_gem’, ‘gem_awesome’])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. The &lt;code&gt;add()&lt;/code&gt; function provides a convenience parameter &lt;code&gt;libs&lt;/code&gt; that is specifically used for checking that Puppet has the specified Ruby libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;Instead of filling custom facts and types / providers full of complex conditional logic to ensure that they only run where they are supposed to and have all they need to run, write features to use with &lt;code&gt;confine&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn more
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Check out the docs for &lt;a href="https://puppet.com/docs/puppet/7/fact_overview.html#fact_overview" rel="noopener noreferrer"&gt;writing custom facts&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Check out the docs for &lt;a href="https://puppet.com/docs/puppet/7/types_and_providers_method.html" rel="noopener noreferrer"&gt;writing custom types and providers&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Check out the &lt;a href="https://github.com/puppetlabs/puppet/blob/main/lib/puppet/util/feature.rb#L9" rel="noopener noreferrer"&gt;excellently documented code that implements features&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>puppet</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
