Ask Your Question

modify file for matched content

asked 2014-05-03 19:29:50 -0600

Benjo gravatar image

Hello Frds,

I am new with puppet. Just setup puppet master on centos and running agnet on freebsd. i tried with package installation and some basic file modification or creation stuff with puppet and really puppet makes life easier for administrator.

I am looking for a module or someway to modify specific file with specific content, let say that I want to make changes in file which files has key/value pair with delimeter should be tab,comma,equal sign or space.

For example, I want to make changes in fstab file, current i have partition name like /dev/sda1 now i want to set uuid instead of name.

Please suggest me to how to do it and also recommend me any reference manual or something to get it in more practical manner for administrator daily life stuffs.

Regards Benjo

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-05-03 23:16:24 -0600

spuder gravatar image

updated 2014-05-04 01:16:15 -0600

There are several ways to modify the content of a file.

Create a template

 file { '/etc/fstab':
   ensure => present, 
   content => template(foo/templates/fstab.erb),

This is the simple answer, however it could become hard to manage if every server requires its own template.

Use a module

While I haven't tried it, it looks like over 1000 other people have downloaded this module
There are several other listed on the puppet forge which look promising.

Use an augeas provider

Augeas is a great way to manage a single line inside a file. It thrives on editing key value pairs but has many plugins to edit everything from json, to xml. These plugins are called lenses.

Change just the HOSTNAME line inside /etc/sysconfig/network

augeas { 'new_hostname':
  context   =>  '/files/etc/sysconfig/network/',
  changes   =>  "set HOSTNAME foobar",

Or change the sshd config file

augeas { 'disable root':
  context   =>  '/files/etc/sshd/config',
  changes   =>  "set PermitRootLogin no",

Here is a more complex example

 augeas { 'nppcu':
    incl    =>  '/var/spool/torque/server_priv/serverdb',
    context =>  '/files/var/spool/torque/server_priv/serverdb/server_db/attributes',
    lens   =>  "Xml.lns",
    changes =>  [
      "ins nppcu after /files/var/spool/torque/server_priv/serverdb/server_db/attributes/cray_enabled",
      "set /files/var/spool/torque/server_priv/serverdb/server_db/attributes/nppcu/#text 1",

From what you are describing, a fstab module will likely be the best option, followed by augeas.

edit flag offensive delete link more


Thanks... I will implement augeas .

Benjo gravatar imageBenjo ( 2014-05-04 01:09:46 -0600 )edit

Do i need to install any puppet module on master or on clients...? I think on master...

Benjo gravatar imageBenjo ( 2014-05-04 01:10:33 -0600 )edit

@Benjo You Install the module on the master, and then inside your site.pp file you 'include' the module on that node.

spuder gravatar imagespuder ( 2014-05-04 01:17:31 -0600 )edit

thankx i want to change device name from ./dev/sda2 to uuid one. , is it possible by augeaus.?

Benjo gravatar imageBenjo ( 2014-05-04 19:19:19 -0600 )edit

@Benjo, possibly. The more I look at this, the more I think the puppet module will be the better way to go. Augeas is meant for for key value pairs

spuder gravatar imagespuder ( 2014-05-05 00:13:20 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-05-03 19:29:50 -0600

Seen: 604 times

Last updated: May 04 '14