puppet chown/chmod against files under a directory in batch

11,263
  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.

Share:
11,263
Shengjie
Author by

Shengjie

Enthusiastic software engineer, a big fan of open source community development, big data, cloud technologies etc...

Updated on June 13, 2022

Comments

  • Shengjie
    Shengjie almost 2 years

    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';
      }