As you might already know I am a huge fan of Cfengine (2/3). But I do realize that there are also other configuration management systems, such as Puppet or Chef and they might have their advantages over Cfengine 3. I have published a few Cfengine 3 snippets and am planning to re-write those snippets in […]

Author:

As you might already know I am a huge fan of Cfengine (2/3). But I do realize that there are also other configuration management systems, such as Puppet or Chef and they might have their advantages over Cfengine 3.

I have published a few Cfengine 3 snippets and am planning to re-write those snippets in Puppet. Afterwards I will be able to compare both configuration management systems and form an opinion.

For better results I will clone my virtual machine (which hosts the Cfengine 3 snippets).

Installing Puppet on Linux Mint 12 (Ubuntu)

apt-get install libopenssl-ruby rdoc libopenssl-ruby1.8 libreadline-ruby1.8 libruby1.8 rdoc1.8 ruby1.8 augeas-lenses augeas-tools augeas-ruby1.8 libaugeas-ruby 
apt-get install puppet puppet-common puppetmaster puppetmaster-common puppet-testsuite vim-puppet

Note: The last command is installing Puppet 2.7.1. I noticed that Puppet 3 is already released and maybe I will switch later.

The official puppet website advices you to install Puppet from source; however, I do like the Ubuntu packages more.

The next step would be to create the first manifest, a Puppet code snippet. Please note that I will not try to explain the Puppet model to you (e.g. RAL, molecules/resources, manifests, providers, client/server model..) since I am still new to them. I will only write the code snippet for best comparison to Cfengine 3:

Writing the Puppet manifest for creating a file with a single text line

cd /etc/puppet/manifests
touch xenuser_org-001-simple_editing_files.pp

Now edit the file with your favorite editor:

file {'example2-puppet':
        path    => '/etc/example2-puppet',
        ensure  => present,
        mode    => 0640,
        content => "Hi there, this is Puppet.",
     }

Afterwards make sure that the new manifest is applied to the system:

puppet apply xenuser_org-001-simple_editing_files.pp
warning: Could not retrieve fact fqdn
notice: /Stage[main]//File[example2-puppet]/ensure: created
notice: Finished catalog run in 0.04 seconds

Let’s have a look at the freshly created text file:

cat /etc/example2-puppet 
Hi there, this is Puppet.

Unfortunately the line is missing a line break (note: the file /etc/example2, created with Cfengine 3, is containing one).

Now for real – how to edit files?
The sample above showed how to create a file from scratch. However, in my first Cfengine 3 example I showed how to edit a file if it already exists.

As far as I know, Puppet does not contain a function for editing existing lines in a file. Instead you have to use exec calls and run grep/sed/perl and so on.

Now edit the file “xenuser_org-001-simple_editing_files.pp” until it looks like this:

# This file has two versions; the commented version below only
# creates the file with one line in it.

#file {'example2-puppet':
#       path    => '/etc/example2-puppet',
#       ensure  => present,
#       mode    => 0640,
#       content => "Hi there, this is Puppet.",
#     }


#This version only edits a file and does not make sure if it exists or not.
define replace($file, $pattern, $replacement) {
  exec { "/usr/bin/perl -pi -e 's/$pattern/$replacement/' '$file'":
      onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0 if
/$pattern/ && ! /$replacement/ ; END { exit \$ret; }' '$file'",
   }
}


replace { 'example2-puppet':
   file => '/etc/example2-puppet',
   pattern => "Hi there, this is Puppet.",
   replacement => "Hi there, this is Puppet again.",
}

Afterwards apply and verify the changes:

puppet apply xenuser_org-001-simple_editing_files.pp
warning: Could not retrieve fact fqdn
notice: /Stage[main]//Replace[example2-puppet]/Exec[/usr/bin/perl -pi -e 's/Hi there, this is Puppet./Hi there, this is Puppet again./' '/etc/example2-puppet']/returns: executed successfully
notice: Finished catalog run in 0.20 seconds
cat /etc/example2-puppet 
Hi there, this is Puppet again.

As you can see our method works – however, I find it quite disturbing that there is no direct way of editing an existing file. An huge disadvantage of the way used above is that you have to know what you want to replace.

Summary
I find the concepts of Puppet very interesting and believe that it could be at least as good as Cfengine 3. But still I do not understand why there is no direct text editing function – please feel free to comment if you know more details.
Another aspect is that there is no automated line break after text was added to a file.

The next blog post about Puppet will show how to check for running services and restart them if necessary.

As usual, you can download the Puppet code snippet here.

Update 5th January 2012 #1: Nick pointed me to Augeas, a tool-framework which can be used for editing files. However, I did not find out how to edit files which only contain text lines (but no variables, e.g. var = val1).

Update 5th January 2012 #2: As Nick pointed out, it is a different thing to define a file content or add new lines (like I did in Cfengine 3). Indeed adding “\n” to the end of the text content did the job.

Sources
http://bitfieldconsulting.com/puppet-tutorial
http://www.mailinglistarchive.com/html/puppet-users@googlegroups.com/2010-05/msg00529.html

Comments on this entry (4 comments)

Did you like this post? You can share your opinion with us! Simply click here.

I think augeas is the tool puppet interfaces with to handle file editing.

Reply

On the line break…. In your CFEngine example you “insert_lines” which is slightly different semantically than stating a files “content”. I suspect adding a \n will give you the line break you want.

Reply

Thanks for your comments, Nick. Will update the blog post accordingly!

Reply

Add Your Comment

Powered by sweet Captcha


+ five = 8