HOWTO ERB Template generate an array of files

79 views
Skip to first unread message

Warron French

unread,
Sep 16, 2016, 2:54:52 PM9/16/16
to Puppet Users
Its probably easiest to explain what I want to accomplish.

I want to create the following 3 files:  /etc/motd, /etc/issue, /etc/issue.net from a single (1) puppet file resource using the content => template('template.erb') syntax.

I don't want to create 3 separate file resources and use the exact same syntax three (3) times.

I have a class (I am a novice puppet code) and here is what I have in it so far...


class manage_banners::text {
       { file '/etc/motd':
               ensure  => 'file',
               owner   => '0',
               group   => '0',
               content => template('some_banner_text.erb'),
       }
}

class manage_banners::gdm {
     <snippet ... nothing in here yet>
}

class manage_banners::kde {
     <snippet ... nothing in here yet>
}

In the manage_banners::text class I don't want to place 3 file resources (one for each of the /etc/ files).

I am thinking that there is a solution for this, but I don't know how to keyword search for something like this.

Please help, and thank you,
Warron

Peter Kristolaitis

unread,
Sep 16, 2016, 3:26:57 PM9/16/16
to puppet...@googlegroups.com

You can create arrays of identical resources:

file { [ '/etc/motd',
         '/etc/issue',
         '/etc/issue.net',
       ]:
  ensure   => file,
  owner    => 0,
  group    => 0,
  content  => template('manage_banners/some_banner_text.erb'),
}

Under the hood, when the catalog is compiled, it still gets split out into 3 separate resources that are managed independently, but in your manifests you can define them as a single block.

The other option, if these 3 files are always to be the same, is to pick one to put the actual content into and then use symlinks or hardlinks for the other two files.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/c78a1ca7-7839-433f-8483-8976ecad9532%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Warron French

unread,
Sep 18, 2016, 4:42:36 AM9/18/16
to puppet...@googlegroups.com

Hi, thanks Peter.

I am writing this module to write it based on the design I have had because... being a little lazy I guess you might say/think.  I know that I could do a link, but I believe for something like this an actual file might be required, because if the main file is deleted, then the banner is lost entirely because the links will be broken.


Thanks.

Peter Kristolaitis

unread,
Sep 18, 2016, 12:34:01 PM9/18/16
to puppet...@googlegroups.com

That's only true if you use symlinks.  Hard links wouldn't have that issue, though Puppet currently can't handle those with the file{} resource.

However, the above point is almost irrelevant, because if you have people going around and randomly deleting files in /etc, I would argue that you have a WAY bigger problem than being worried about whether or not a MOTD is displayed, and one that can't be solved with configuration management software.

- Peter

Warron French

unread,
Sep 18, 2016, 9:01:40 PM9/18/16
to puppet...@googlegroups.com

Very true Peter, thank you sir.

jcbollinger

unread,
Sep 19, 2016, 9:12:52 AM9/19/16
to Puppet Users


On Sunday, September 18, 2016 at 3:42:36 AM UTC-5, Warron French wrote:

Hi, thanks Peter.

I am writing this module to write it based on the design I have had because... being a little lazy I guess you might say/think.  I know that I could do a link, but I believe for something like this an actual file might be required, because if the main file is deleted, then the banner is lost entirely because the links will be broken.



As Peter said, If you have people randomly deleting system files then you have a bigger problem.  Furthermore, making three regular files in that case is only a partial mitigation of such a risk, because losing any one of the files still impacts consumers of that file.

But the main point I wanted to raise here is that this is one of the things Puppet is for.  Whether you use regular files or symlinks, if one or more of them is deleted or modified then you can expect Puppet to restore it on the next catalog run.  Moreover, if you have reporting enabled then the update will be reported to you.  This may or may not be sufficient for your purposes.


John

warron.french

unread,
Sep 19, 2016, 1:06:44 PM9/19/16
to puppet...@googlegroups.com
Thanks for the feedback John, much appreciated.

--------------------------
Warron French


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/98a119b6-067c-4fe6-a513-a5b83704f772%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages