use regex to ensure multiple directories are absent or exist

171 views
Skip to first unread message

Panaman

unread,
Oct 21, 2012, 12:37:25 PM10/21/12
to puppet...@googlegroups.com
Is it possible to use regex to ensure directories are deleted.....

I want to be able to do something like:

file { '/var/directory[1-9]':
  ensure => absent,
}

Would also be nice to use regex to ensure directories are present...

jcbollinger

unread,
Oct 22, 2012, 10:51:51 AM10/22/12
to puppet...@googlegroups.com


Puppet is not a scripting language.

You can dress up this sort of thing in various ways, but in the end you need to either create a separate resource for each file you want to manage (directly), or use an Exec.  For example, you could do this:

define patternfile($pattern, $replace) {
  $filename = regsubst($pattern, $replace, $name)
  file { $filename:
    ensure => absent
  }
}

patternfile { [1, 2, 3, 4, 5, 6, 7, 8, 9]:
  pattern => '/var/directoryREPLACEME',
  $replace => 'REPLACEME'
}

You could write something slightly more concise in Puppet's Ruby DSL, but it would still amount to declaring a separate resource for each file you want to ensure absent.

In practice, what you ask, in the general form about which you ask, is not often the best approach to the task.  It may help to step back and look at the bigger picture.


John

Reply all
Reply to author
Forward
0 new messages