A collection of task oriented solutions in Puppet

 

Why have trailing commas in resources?

Challenge

You've noticed how messy I am, leaving trailing commas throughout my examples and you'd like to know how I can be so inattentive. Well, there are a couple of reasons for leaving those stray little commas.

Solution

class trailing_comma {

  file { '/path/to/filename':
    owner => 'root',
    group => 'wheel',  # <-- Unneeded comma?
  }

}

Explanation

While that last comma is not actually needed for the manifest to work I like to leave it in for two related reasons. The first is that they reduce the chance of a simple syntax error when you later go back and add additional settings to a resource. By always leaving a trailing comma you avoid all those niggling little errors.

The second reason is that it makes running diff over your version controlled manifests easier, and you are keeping them under version control aren't you? Here we have a very simple file resource that doesn't have a trailing comma:

file { '/path/to/filename':
  owner => 'root',
  group => 'wheel'
}

We then update this file resource to include a mode, so it looks like this:

file { '/path/to/filename':
  owner => 'root',
  group => 'wheel',
  mode  => '0755'
}

Now when we diff the changes, as we always do before committing them, we see that apparently two lines have changed. The line we made a real difference to and the line where the only amendment is the addition of a comma.

$ diff -u no-trailing  updated
--- no-trailing 2010-12-29 16:36:32.000000000 +0000
+++ updated     2010-12-29 16:38:24.000000000 +0000
@@ -1,4 +1,5 @@
file { '/path/to/filename':
    owner => 'root',
 -  group => 'wheel'
 +  group => 'wheel',
 +  mode  => '0755'
}

On the other hand, if we leave the trailing comma at the end of the line the diff becomes smaller and the actual change becomes more obvious -

file { '/path/to/filename':
  owner => 'root',
  group => 'wheel',
}
$ diff -u trailing  updated

--- trailing    2010-12-29 16:39:16.000000000 +0000
+++ updated     2010-12-29 16:39:22.000000000 +0000
@@ -1,4 +1,5 @@
file { '/path/to/filename':
    owner => 'root',
    group => 'wheel',
 +  mode  => '0755',
}

While this may not seem like a huge difference (pun intended) when you consider that you, and possibly others, will be re-reading these logs in the future, probably when somethings gone wrong, keeping changes and the resulting diffs as simple as possible is worth the little bit of additional effort.