Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppet chown/chmod against files under a directory in batch

Tags:

puppet

In puppet, you can chown/chmod a single file by doing:

file {
    '/var/log/mylog/test.log':
    ensure  => 'present',
    mode    => '0644',
    owner    => 'me';
}

Two questions on this:

  1. ensure=>'present' is gonna make sure '/var/log/mylog/test.log' exists, if it doesn't it creates it. Is there any way I can make it do actions if file exists, if file doesn't exist, don't bother to create/delete it, just ignore it and carry on.

  2. Let's say I have 3 files under /var/log/mylog/, I want to chown/chmod against them all in a batch instead of having 3 file resource sections in my puppet code. Can I do something like below(of coz, the code below doesn't exist, it's in my dream now ^_^ ):

    files {
        '/var/log/mylog/*.log':
        ensure  => 'present',
        mode    => '0644',
        owner    => 'me';
    }
    
like image 856
Shengjie Avatar asked Feb 20 '23 07:02

Shengjie


1 Answers

  1. If you want to specify to take a given action if file exists, if file doesn't exist etc. you have no choice (to my knownledge) currently than to use the exec resource with creates + onlyif or unless directives. You could use for instance (see reference doc)

     exec { "touch /var/log/mylog/test.log":
        path    => "/usr/bin:/usr/sbin:/bin",
        user    => "${yourmodule::params::user}",
        group   => "${yourmodule::params::group}",
        creates => "/var/log/mylog/test.log", 
        unless  => "test -f /var/log/mylog/test.log"
     }
    
     file { '/var/log/mylog/test.log':
        ensure  => 'present',
        mode    => "${${yourmodule::params::mode}",
        owner   => "${yourmodule::params::user}",
        group   => "${yourmodule::params::group}",
        require => Exec["touch /var/log/mylog/test.log"]    
     }
    
  2. No. Again, you'll have to use an execresource.

like image 121
Sebastien Varrette Avatar answered Mar 15 '23 22:03

Sebastien Varrette