Puppet: Multiple files based on templates - puppet

I have several files which I need to be created by Puppet, all based on templates. As an example:
/etc/my-project/a.ini
/etc/my-project/b.ini
/etc/my-project/c.ini
What I would like to do is to ensure all these files using one statement in Puppet. Something like this:
define myFile {
file { "/ect/init.d/$name.ini":
ensure => file,
content => template("myProject/myFiles/$name.erb"),
}
}
myFile { ['a', 'b', 'c']: }
However, this does not seem to work (inspired by How to iterate over an array in Puppet). What am I missing there? How can I access the file name and use it, if not as $name?

Your array declaration is fine, but you're actually trying to create multiple templates, all with a different filename $name.erb. You should change it to a fixed template name, like template.erb.
Another thing to make sure is that your template file is located correctly.
If your manifest is in a module, the template should be located at module_name/templates/template.erb and called as template("module_name/file_under_template_directory")
If it's a standalone manifest, you have to put the full path instead, template("fully_qualified_path_to_template_file").
Finally, if you're still encountering errors, you should update your question with the error message so we can narrow down the cause.

Did you try using ${name} instead of $name?
If it doesn't work, check that your template files (a.erb; b.erb; c.erb) are in the <module_name>/templates/myProject/myFiles directory.
If all of these don't work, post your error message.

Try this...it will serve your purpose in elegant way.
define myFile($file_name) {
file { "/ect/init.d/${file_name}.ini":
ensure => file,
content => template("myProject/myFiles/${file_name}.erb"),
}
}
$values = {
item_1 => {file_name => "a"},
item_2 => {file_name => "b"},
item_3 => {file_name => "c"}
}
create_resources(myFile,$values)

['a', 'b', 'c'].each |String $name| {
file { "/ect/init.d/$name.ini":
content => template("myProject/myFiles/$name.erb"),
}
}
See puppet docs on iteration.

Related

Puppet data array

I got a module that creating some directories depending of server:
class linux_sftp::sftp_mount ($sftp_mount_ip, $sftp_mount_username, $sftp_mount_password, $sftp_mount_point) {
file { "/mnt/${sftp_mount_point}":
ensure => directory,
subscribe => Exec['sftp_remount'],
}
in data.yml
sftp_mount_point: "stcontent1"
I want to add to data more folders like: stcontent2, stcontent3. Is it a way to add this and loop thru data?
sftp_mount_point:
- "stcontent1"
- "stcontent2" ...
Yes you can use lambda method (can also be invoked as functions if desired) iteration to accomplish this task. The most common for your use case is each. It can be easily invoked on type Array[String] like you have in your question.
$sftp_mount_point.each |String $mount| {
file { "/mnt/${mount}":
ensure => directory,
}
}
Note that the file type does not have a subscribable property, so subscribe is not a valid attribute and I therefore removed it above.

Logstash Filter not working when something has a period in the name

So I need to write a filter that changes all the periods in field names to underscores. I am using mutate, and I can do some things and not other things. For reference here is my current output in Kibana.
See those fields that say "packet.event-id" and so forth? I need to rename all of those. Here is my filter that I wrote and I do not know why it doesn't work
filter {
json {
source => "message"
}
mutate {
add_field => { "pooooo" => "AW CMON" }
rename => { "offset" = "my_offset" }
rename => { "packet.event-id" => "my_packet_event_id" }
}
}
The problem is that I CAN add a field, and the renaming of "offset" WORKS. But when I try and do the packet one nothing changes. I feel like this should be simple and I am very confused as to why only the one with a period in it doesn't work.
I have refreshed the index in Kibana, and still nothing changes. Anyone have a solution?
When they show up in dotted notation in Kibana, it's because there is structure to the document you originally loaded in json format.
To access the document structure using logstash, you need to use [packet][event-id] in your rename filter instead of packet.event-id.
For example:
filter {
mutate {
rename => {
"[packet][event-id]" => "my_packet_event_id"
}
}
}
You can do the JSON parsing directly in Filebeat by adding a few lines of config to your filebeat.yml.
filebeat.prospectors:
- paths:
- /var/log/snort/snort.alert
json.keys_under_root: true
json.add_error_key: true
json.message_key: log
You shouldn't need to rename the fields. If you do need to access a field in Logstash you can reference the field as [packet][length] for example. See Logstash field references for documentation on the syntax.
And by the way, there is a de_dot for replacing dots in field names, but that shouldn't be applied in this case.

how to make sure the file created before class import?

I am fighting with ensuring things are taking place in the order I specified in puppet :( Here is what I plan to do
class A {
notice("1")
file{ 'my file':
force => true,
replace => 'no',
ensure => 'present',
content => "content",
owner => 'me',
group => 'me',
mode => '0444'
}
notice("2")
}
class B {
require A
contain target_class_dependent_on_file
}
then I call the classes as
classA{} -> classB{}
and class B always fails because file is not created, 1 and 2 are output just fine before class B. But if I skip classB, I can see file created just fine. I am banging my head against wall now. Can someone please give me some help? many thanks
nvm. This is not a legitimate case. The 'my file' I tried to set up is really to set up a facter. It will never work

Ordering with creating and reading a file in Puppet

I know it is something with the catalog that is the issue, I just can't figure out how to work around it.
I have the following code and I get the following error:
class test1 {
file { '/tmp/test.txt':
ensure => present,
content => 'name=joe',
}
}
class test2 {
$test = file('/tmp/test.txt')
notify { $test: }
}
class test3 {
class { 'test1': } ->
class { 'test2': }
}
puppet apply -e "include test3"
Error: Could not find any files from test.txt at ../modules/test2/manifests/init.pp
So essentially, I am trying to read a file before it exists, and the ordering doesn't appear to be working. Any ideas how I can work around this?
Based on the description of the function you are trying to utilize it will never operate in the fashion you are trying.
file:
Loads a file from a module and returns its contents as a string.
Affectively what this means is that the file would exist in
test1/files/test.txt
And would be loaded using:
file('test1/test.txt') i.e. file(<MODULENAME>/<FILENAME>)

Puppet; Call another .pp

So I am using the https://forge.puppetlabs.com/pdxcat/nrpe module to try to figure out automation of NRPE across hosts.
One of the available usages is
nrpe::command {
'check_users':
ensure => present,
command => 'check_users -w 5 -c 10';
}
Is there anyway to make a "group" of these commands and have them called on specific nodes?
For example:
you have 5 different nrpe:command each defining a different check, and then call those specific checks?
I am basically trying to figure out if I could group certain checks/commands together instead of setting up a ton of text in the main sites.pp file. This would also allow for customized templates/configurations across numerous nodes.
Thanks!
EDIT:
This is the command and what it's supposed to do when called on with the 'check_users' portion. If I could have a class with a set of "nrpe:command" and just call on that class THROUGH the module, it should work. Sorry, though. Still new at puppet. Thanks again.
define nrpe::command (
$command,
$ensure = present,
$include_dir = $nrpe::params::nrpe_include_dir,
$libdir = $nrpe::params::libdir,
$package_name = $nrpe::params::nrpe_packages,
$service_name = $nrpe::params::nrpe_service,
$file_group = $nrpe::params::nrpe_files_group,
) {
file { "${include_dir}/${title}.cfg":
ensure => $ensure,
content => template('nrpe/command.cfg.erb'),
owner => root,
group => $file_group,
mode => '0644',
require => Package[$package_name],
notify => Service[$service_name],
}
}
What version are you talking about? In puppet latest versions, inheritance is deprecated, then you shouldn't use it.
The easiest way would be to use "baselines".
Assuming you are using a manifests directory (manifest = $confdir/manifests inside your puppet.conf), simply create a $confdir/manifests/minimal.pp (or $confdir/manifests/nrpe_config.pp or whatever class name you want to use) with the content below:
class minimal {
nrpe::command { 'check_users':
ensure => present,
command => 'check_users -w 5 -c 10',
}
}
Then just call this class inside your node definitions (let's say in $confdir/manifests/my_node.pp) :
node 'my_node.foo.bar' {
include minimal
}

Resources