How to copy list of files using puppet - 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

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

Puppet: how to use ignore globbing on file resources

Consider the following puppet config snippet:
file { "/opt/app/${name}/traces" :
ensure => directory,
owner => 'appuser',
group => 'appuser',
ignore => [ '*.tra' ],
backup => false,
recurse => false
}
The folder will contain "${name}..tra" files and I don't want puppet to take care of these files.
When calling strace or lsof against the puppet client, I can see it still tries to do something with these files (probably checksuming, according to the time spent on each file).
Thanks a lot,
Adam.

Create multiple directory via 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,
}

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.

Puppet unzip file always into read only folder

I am using following script part to unzip my zip file. But the problem is it unzips the file always into a readonly folder. How can this be fixed.
exec { "install appliction server to pc":
command => 'unzip wso2as-5.2.1.zip',
cwd => '/home/malintha/adikari3/',
path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
logoutput => true,
timeout => 3600,
require => File['/home/malintha/adikari3/wso2as-5.2.1.zip'],
}
There are three parameters to the exec type, of which you should use at least one, to control when and when not to run
onlyif
unless
creates
The unzipping of an archive typically lends itself to a creates solution
exec { "unzip-file":
cwd => "/path/for/extraction",
creates => "/path/for/extraction/software-x.y",
...
}
assuming that the zip extracts into a directory tree rooted at software-x.y.
For more details, see the reference documentation.

Resources