Create multiple directory via Puppet - puppet

How do I create multiple file via Puppet?
For example I can run:
touch abc{1,2,3,4,5,6,7,8} to create abc1,abc2,abc3....abc8
How do I run the same via Puppet?

At its simplest, you can specify a list of files by passing an array to a file resource:
$numbered_files = ["/abc1", "/abc2", "/abc3"]
file { $numbered_files:
ensure => file,
}
To automatically build an array of filenames with a certain prefix, then the range function from stdlib can do this trivially:
$numbered_files = range("/abc1", "/abc8")
file { $numbered_files:
ensure => file,
}

Related

How to include multiple values under a section in inifile

I am trying to create a puppet manifest using inifile. This would be for a configuration file where I need to have the following format.
[safe]
directory = /home/foo
directory = /home/test
directory = /home/something
I know that there is a way to use directory1, and directory2 but I was wondering if there is a way to do it without changing the directory since it needs that specific attribute. This implementation is meant for puppet manifest.
Also, I was thinking puppetlabs/inifile module but if there is another option to achieve this would be great too.
Thanks for the help in advance
So far, I have an implementation like:
ini_setting { 'procedure cache size':
ensure => present,
path => '/var/lib/somethning/test.config',
section => 'safe',
setting => 'directory',
value => '/home/foo',
indent_char => "\t",
}
This is for each directory. The purpose for this implementation is to address the new git configuration for safe.repository in the recent update. My understanding is that for multiple directories, it adds a new value as directory = <directory> I don't believe that it likes directories separate by commas.
First I thought about file_line, but this is not idempotent for multi-line settings (you get problems when you run again). You can try:
Sample puppet code dir.pp
$safe_directories="directory = /home/foo
directory = /home/test
directory = /home/something"
notice "Testing\n${safe_directories}"
file { "/tmp/result.ini":
ensure => present,
content => template('/tmp/layout.erb'),
}
notice "Check /tmp/result.ini"
Sample template /tmp/layout.erb
[unsafe]
directory=/unsafe
[safe]
<%=#safe_directories%>
otherfield=secure
[header3]
nothing = here
Now run command from commandline
puppet apply dir.pp

How to copy list of files using puppet

In puppet is it possible to copy of list of files
Tried below code, but didn't work
$list_files = ['file1','file2','file2']
file { $list_files:
ensure => present,
path => /tmp,
source => 'puppet:///modules/$module_name/list_file_dir/$list_files',
}
any Suggestions?
As others have said you can use the each switch e.g.
['file1','file2','file2'].each |$flie| {
file{ "/tmp/${file}":
ensure => file,
source => "puppet:///modules/$module_name/list_file_dir/${file}",
}
}
however it is also worth noting that you can pass a directory to both the destination and the source parameter e.g. if you have the directory puppet:///modules/$module_name/list_file_dir on your puppet master with the files ['file1','file2','file2'] then you can copy them all to temp using the following
file { '/tmp':
ensure => directory,
source => 'puppet:///modules/$module_name/list_file_dir',
recurse => remote,
}
using recurse => remote remote ensures puppet only manages files which exist in the remote location (i.e. the puppetmaster)
https://puppet.com/docs/puppet/7/types/file.html#file-attribute-recurse

TeamCity: Adding additional logs to artifacts zip file

In TeamCity, it is possible to specify the 'Artifact Paths' so that all files and folders from a directory can be added to a Zip file:
E:\Logs\**\* => Logs.zip
However, I would like to add additional log files to a sub folder within the zip file. Is it possible to do this without an additional build step to move/copy the files? For example:
E:\Logs\**\* => Logs.zip
D:\ThirdParty\Logs\*.txt => Logs.zip\ThirdParty\
It is possible to specify the path inside a zip when you are creating an artefact
You just need to set a ! after the extension, in order to indicate that the content will be put inside.
/logs/*.log => Logs.zip
/out/*.txt => Logs.zip!/ThirdParty

Setting multiple environment variables using Puppet

Currently I am using
file {"/etc/profile.d/setjvmparams.sh":
ensure => file,
owner => root,
source => "puppet:///modules/java/setjvmparams.sh",
}
But I want to remove dependency of my module on external files. Is there a way out to set env variable?
Note: setjvmparams.sh has 3 lines.
I want to put paths in /etc/profile.d and source it.

How can I refer to the current puppet module's files directory?

This is most likely an anti-pattern, but I'd like to know nonetheless:
I need to extract a tgz which is in puppet and then move the contents somewhere else. Is it possible, in a puppet exec { }, to refer to the file where it is stored on disk?
For example, puppet is available at /usr/local/puppet, and the tgz file I need it in /usr/local/puppet/modules/components/files/file.tgz. In the exec { } can I do something like command => "/bin/cp $modules/components/files/file.tgz /somewhere_else" ? Or do I have to declare a file { source => "..." } block first?
Both approaches are correct if you run puppet with puppet apply.
In master-agent architecture using exec to copy file probably will not work at all.
In my opinion using file resource is more "puppet-like" but is has one significant drawback.
You can use:
file { '/some_path/somewhere_else':
source => '/usr/local/puppet/modules/components/files/file.tgz',
}
This will create file /some_path/somewhere_else with the same content as /usr/local/puppet/modules/components/files/file.tgz (it will make a copy of the original file).
But if /some_path doesn't not exist in the file system, the command will fail.
If you are working with tgz files you can also consider using some of the archive puppet modules e.g gini.
UPDATE:
I can propose two approaches:
Use puppet file server to serve files (or define module path for old puppet versions). Next just use it e.g:
file { '/some_path/somewhere_else':
source => "puppet:///modules/components/file.tgz',
}
Define custom facter fact 1, 2 that points path in your filesystem containing required files. E.g:
file { '/some_path/somewhere_else':
source => "${::my_custom_fact}/some_path/file.tgz',
}
I do not think that any of the core facts might be useful for you.

Resources