Possible to move/rename files?

842 views
Skip to first unread message

Jasmine Lognnes

unread,
Mar 17, 2014, 12:31:47 PM3/17/14
to puppet...@googlegroups.com
Hello =)

I would like to either replace "enabled=True" to "enabled=False" in a bunch of files or rename those files to end with "_false".

Right now I replace content is files with e.g.

 replace { 'dummy5':
    file => '/etc/xdg/user-dirs.conf',
    pattern => "enabled=True",
    replacement => "enable=False"
  }

but I don't like it a whole lot, as I have to fill in the dummy variable my self and make sure all are unique.

Is it possible to rename files with Puppet or is there a better way to replace content files?

Kind regards
Jasmine


zerozer...@gmail.com

unread,
Mar 18, 2014, 4:59:44 AM3/18/14
to puppet...@googlegroups.com
On Monday, March 17, 2014 5:31:47 PM UTC+1, Jasmine Lognnes wrote:
 
Right now I replace content is files with e.g.

 replace { 'dummy5':

Er... I didn't even know a "replace" resource type existed. Where does it come from?
 
Is it possible to rename files with Puppet or is there a better way to replace content files?

There's no easy way to rename a file AFAIK.
You can use something like this but it's far from ideal (and OS dependent of course):

  exec { '/bin/mv /etc/xdg/user-dirs.conf /etc/xdg/user-dirs.conf_false':

    creates => '/etc/xdg/user-dirs.conf_false',

  }

  ->

  file { '/etc/xdg/user-dirs.conf':

    ensure => absent,

  }


Regarding modifying content, you can have a look at the augeas resource type. You need to have Augeas installed on the machine, but if the files to be modified have some standard format then you might be lucky because you can do your edits without much effort.

Some useful links:

Marco

Jasmine Lognnes

unread,
Mar 19, 2014, 6:06:00 AM3/19/14
to puppet...@googlegroups.com
 replace { 'dummy5':

Er... I didn't even know a "replace" resource type existed. Where does it come from?

I got it from


which now is dead, but can be read at


I created manifests/classes/replace.pp and put in

# Replace a perl regexp with a string:
define replace($file, $pattern, $replacement) {
    $pattern_no_slashes = slash_escape($pattern)
    $replacement_no_slashes = slash_escape($replacement)

    exec { "/usr/bin/perl -pi -e 's/$pattern_no_slashes/$replacement_no_slashes/' '$file'":
        onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0 if /$pattern_no_slashes/ && ! /$replacement_no_slashes/ ; END { exit \$ret; }' '$file'",
    }
}


There's no easy way to rename a file AFAIK.
You can use something like this but it's far from ideal (and OS dependent of course):

  exec { '/bin/mv /etc/xdg/user-dirs.conf /etc/xdg/user-dirs.conf_false':

    creates => '/etc/xdg/user-dirs.conf_false',

  }

  ->

  file { '/etc/xdg/user-dirs.conf':

    ensure => absent,

  }


Very interesting use of "->". Thanks =)
 
Regarding modifying content, you can have a look at the augeas resource type. You need to have Augeas installed on the machine, but if the files to be modified have some standard format then you might be lucky because you can do your edits without much effort.

Some useful links:


This is great! Much better solution!! Thanks =)



 
Reply all
Reply to author
Forward
0 new messages