Home » Home » PUPPET PART 2: A Complete File & Directory Processing In Puppet

This article is a continuation of my previous scholarly in Puppet. I have to say that Puppet is quite different from traditional Linux scripting,  for an instant you could argue that there’s not much difference between the job performed by shell script and Puppet, if you think so then here is an example.

Difference between Shell and Puppet Script

Traditional Linux Shell Script

Slave$ FILE=/tmp/hello
Slave$ chmod 640 $FILE
Slave$ chown foo $FILE
Slave$ chgrp bar $FILE
Slave$ wget -O $FILE “http://my.puppet.server/dist/$FILE”               #URL  Contains “Hello World” Text

What happens if the wget fails in the above script? How will the script handle that? And what happens if there are something that requires $FILE to be there but with the incorrect contents?

Puppet Script in Ubunut

file { “/tmp/hello”:
   mode => 640,
   owner => foo,
   group => bar,
   content => “Hello world”,
}

In case of Puppet, the entire configuration management script runs on the target client, whereas  puppet compiles all of these on the server. If you change the content, it requires to change it on the server and that will reflect on as many systems as you want. Moreover puppet handles the dependencies and transfer problems for you automatically.

If you still not convinced, you will understand the difference in further articles. Let’s get back to the track…

Complete Files & Directories Usage in Puppet

Puppet can handle files with all their functionality in broader, it can manage files with their content, ownership, permission and lot other.  Creating directory can be done by specifying the path in the first line and setting ensure to the “directory”. To create a new directory “site”  in /tmp path you need to enter the following script in your favorite text editor and save it as File_Example1.pp

file { “/tmp/site”:
   ensure => “directory”,
}

Slave$ puppet apply File_Example1.pp

Puppet script to create a directory with permissions and ownership

file { “/var/log/admin-log”:
   ensure => “directory”,
   owner => “root”,
   group => “wheel”,
   mode => 777,
}

Slave$ puppet apply File_Example2.pp

The above script will create a directory called “admin-log” in “/var/log” path, the owner of the directory is “root”, followed by the group name “wheel” and the permission read, write and execute.

To define a directory tree in Puppet is slightly harder than you expect, because puppet lacks a native mkdir -p equivalent. Instead we use an array of directories, each one progressing slightly further down the tree and puppet will create them in turn or you can assign them to a variable and use them in the resource.

Creating directory tree without assigning variable

file { [ “/usr/local/whisper/”, “/usr/local/whisper/2.0”,

“/usr/local/whisper/2.0/bin”, “/usr/local/whisper/2.0/log” ]:

   ensure => “directory”,

}

Slave$ puppet apply File_Example3.pp

Creating directory tree by assigning variable

$dir_tree = [ “/usr/local/whisper/”, “/usr/local/whisper/2.0”,
“/usr/local/whisper/2.0/bin”, “/usr/local/whisper/2.0/log”,
]

file { $dir_tree:
   ensure => “directory”,
   owner => “root”,
   group => “wheel”,
   mode => 750,
}

Slave$ puppet apply File_Example4.pp

Unfortunately Puppet requires its parent directory to create sub directories, means the following script doesn’t work in creating the directory tree.

file { “/usr/local/whisper/2.0/bin”:
   ensure => “directory”,
}

How To Create Symlink Using Puppet?

Symbolic link is a special type of file that contains a reference to another file or directory in the form of absolute or relative path and that resolve the pathname. To create a symbolic link using puppet,

file { “/tmp/hi_link”:
   ensure => “link”,
   target => “/home/puppet-slave/Desktop”,
}

Slave$ puppet apply File_Example5.pp

The other way to create symlink in less and clear syntax is

file { “/tmp/hi_link”:
   ensure => “/home/puppet-slave/Desktop”,
}

Slave$ puppet apply File_Example6.pp

How To Create A File Inside Sub Directory’s Using Puppet? 

So far we are only dealing with creating directories, now lets look how to create a file inside a sub directory, the below script will create a file “hi_puppet” with the content “Hello World Puppet” on “/tmp/mac/test/directory/” path. The job should get split in two ways, first it should create the directory and then to create file.

file { [“/tmp/mac”,”/tmp/mac/test/”,”/tmp/mac/test/directory”]:

   ensure => “directory”,

}

file { “/tmp/mac/test/directory/hi_puppet”:

   require => File[“/tmp/mac/test/directory/”],

   content => “Hello World Puppet”

}

Slave$ puppet apply File_Example7.pp

I believe this article rounds out your in-dept look at the core file and directory processing in puppet, if you need clarification on any of these concept leave your comments. Stay tuned to know more about Puppet in further series.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA Image

*