Skip to content
Advertisement

Puppet – How to purge a directory

A bit of a Puppet newbie here. I am trying to recursively purge all files and directories under /var/www except keep one file present (/var/www/html/appicon.ico). This is my code:

  file {'/var/www':
    ensure    => directory,
    recurse   => true,
    purge     => true,
    force     => true,
    require   => Package['httpd'],
    subscribe => Package['httpd']
  } ->
  file {'/var/www/html':
    ensure    => directory,
    owner     => 'root',
    group     => 'root',
    mode      => '0755'
  } ->
  file {'/var/www/html/appicon.ico':
    ensure    => file,
    owner     => 'root',
    group     => 'root',
    mode      => '0644',
    content   => ''
  }

The code does appear to purge all files and directories, except for other files under /var/www/html. Any thoughts what I am doing wrong here or how this can be done properly?

Advertisement

Answer

You would need to set purge on the html directory too, that is:

file {'/var/www':
  ensure    => directory,
  recurse   => true,
  purge     => true,
  force     => true,
  require   => Package['httpd'],
  subscribe => Package['httpd'],
} ->
file {'/var/www/html':
  ensure => directory,
  recurse => true,    # note here
  purge   => true,    #
  owner   => 'root', 
  group   => 'root',
  mode   => '0755'
} ->
file {'/var/www/html/appicon.ico':
  ensure  => file,
  owner   => 'root',
  group   => 'root',
  mode    => '0644',
  content => ''
}

Explicitly including a file/directory in a Puppet manifest “protects” it from being purged by purge => true, recurse => true set on its parent directory.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement