Skip to main content
Puppet

Puppet Moving Directories

By March 18, 2016September 12th, 2022No Comments

Puppet Moving Directories


The task of having Puppet moving directories is a simple one which we demonstrate for you here using file resources. One of the things that a good configuration management system should be able to do is to help you cope with organizational change. In this blog we look at how we can create a simple manifest to simulate moving directories with Puppet. We will, in fact, copy the contents on one directory to another and then delete the original source directory. A simple use case for this is wanting to unify all of the web server document roots. On some systems thus is /var/www, other it is /var/www/html and others is it is /srv/www/htdocs. We can easily copy all if the contents of one of these directories into a new directory that conforms to your new standard of the document root location. We will see that we can copy the directory and the entire contents including sub-directories.

We will be using a simple manifest to show the moving of directories with Puppet, the reality is that this would be part of a larger module but we are just try to show how the Puppet Server could easily manage the task.

To help allow for the scaling out of the manifest into a bigger application we first set two variables. These then could be passed as class parameters to override the defaults that we would code into the module

$source_directory = '/tmp/apt'
$target_directory = 'tmp/aptnew'

The source directory whose content we want to move, we will be moving it to the /tmp/aptnew directory which will  not exist on the system. We then set up file resources for both directories. First the target directory that we need to create:

file { $target_dir :
 ensure => 'directory',
 recurse => true,
 source => "file:///${source_dir}",
 before => File[$source_dir],
}

The syntax for any resource is:

type { 'title':
}

We use the file type and the title is the interpolated variable. We want to ensue that the file resource exists as a directory and then we recursively  copy the contents of the local source directory. Using the file:// protocol we copy from the local file system. We need to make sure that this happen first so we explicitly declare this with the ending before line which references the next resource that we will configure next.

file { $source_dir :
 ensure => 'absent',
 purge => true,
 recurse => true,
 force => true,
}

In this second  file resource the ensure absent line is going to delete this directory, we do this recursive to delete sub-directories and we need to use force with this. The purge is used to delete the file content of the directories.

The complete code is shown below:

Puppet Moving DirectoriesWe can text the manifest that we have created with:

# puppet apply /tmp/file.pp

The video steps you through and explains the process: