How to change the contents of a file by using Puppet? - puppet

I have a html file and I want to use Puppet to replace that file with an empty file with the same filename. I am wondering if it is possible for Puppet to delete the whole contents of a file?
E.g. Is this a correct way to delete a line in Puppet?
file_line { 'delete a line':
ensure => absent,
path => /tmp/test,
line => '\ '
match => '^(?:.*)'
}

If you know there is a file /path/to/file.html and you want to ensure that that file exists and is empty, it's easy:
file { '/path/to/file.html':
ensure => file,
content => '',
}
If you want to ensure that a line in that file is removed using file_line, you have two ways of doing this when ensure => absent is set.
One is to set match => ... and match_for_absence => true,
as in the following example:
file_line { 'bashrc_proxy':
ensure => absent,
path => '/etc/bashrc',
match => '^export\ HTTP_PROXY\=',
match_for_absence => true,
}
In this code example match will look for a line beginning with export
followed by HTTP_PROXY and delete it. If multiple lines match, an
error will be raised unless the multiple => true parameter is set.
Note that the line => ... parameter would be accepted but ignored in
the above example.
The second way of using ensure => absent is to specify a line => ...,
and no match:
file_line { 'bashrc_proxy':
ensure => absent,
path => '/etc/bashrc',
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
}
Note that when ensuring lines are absent this way, the default behavior
this time is to always remove all lines matching, and this behavior
can't be disabled.
See also in the code here and here and in the unit tests.
I have also raised a pull request to add these examples of file_line into the official docs here, as this was not adequately documented before.
Thanks for the question.

Related

Puppet tries to rename sites-avaialbe folder

I am running Puppet on staging server, for some reasons puppet starts trying removing sites-available folder and I have no idea why. Any hint will be helpful.
Error: Could not set 'file' on ensure: Is a directory # rb_file_s_rename - (/etc/nginx/sites-available20180808-12536-11p54v, /etc/nginx/sites-available) at 12:/etc/puppet/modules/nginx/manifests/vhost.pp
Error: Could not set 'file' on ensure: Is a directory # rb_file_s_rename - (/etc/nginx/sites-available20180808-12536-11p54v, /etc/nginx/sites-available) at 12:/etc/puppet/modules/nginx/manifests/vhost.pp
Code:
define nginx::vhost($docroot, $port = 80, $template = 'nginx/vhost_php.erb', $allow = [], $deny = [], $aliases = [])
{
include nginx
file { "/etc/nginx/sites-available/${name}":
owner => 'root',
group => 'root',
mode => 644,
content => template($template),
require => Package['nginx'],
notify => Service['nginx'],
}
file { "/etc/nginx/sites-enabled/${name}":
ensure => 'link',
target => "/etc/nginx/sites-available/${name}",
require => File["/etc/nginx/sites-available/${name}"],
notify => Service['nginx'],
}
}
As #MattSchuchard remarked in comments, the error messages show that Puppet thinks you've asked it to convert a directory into a file. Furthermore, it appears to be associating that action with the first File resource in your manifest, which declares
file { "/etc/nginx/sites-available/${name}":
# ...
}
You will note that that resource appears to be trying to manage a file inside the directory, rather than the directory itself, but that discrepancy would be resolved if the automagic $name variable happened to take an empty string as its value. That's what I presume is happening.
You don't show the relevant declaration(s) of the nginx::vhost resources, but I think you'll find that the problem is there. The (slightly) broader context of those error messages would probably confirm this diagnosis: it normally contains a path-like specification of the resource in which the error occurred, and that would include the relevant resource title.

Puppet : file resource only if file exists

What I want to do is quite simple :
1.
Copy /source/file to /target/file. I achieve this using the following:
file { 'my_file_copy':
ensure => file,
source => 'file:/source/file',
path => "/target/file",
}
2.
However, if file /source/file does not exist, I do NOT want it to perform this task.
I am really struggling with this logic. I attempted the solution below but it throws exceptions during puppet run.
puppet: if one file exists then copy another file over
Is there a better way of achieving this task ?
Ideally, I would like to only use "file" and avoid using "exec". But at this point I would settle for a solution !
Because Puppet is a declarative language where only the end-state is declared, imperative logic such as what you've described - if A, do X - is often hard to express.
Personally, I would try to simply avoid this requirement of having file B copied if and only if file A exists. Often there's a better way.
If the requirement needs to stay, however, then use of Exec here sounds like a pretty good option to me.
exec { 'my_file_copy':
command => 'cp /source/file /target/file',
onlyif => 'test -e /source/file',
creates => '/target/file',
path => '/bin',
}
You could use this logic:
$file = "/source/file"
exec { "chk_${file}_exist":
command => "true",
path => ["/usr/bin","/usr/sbin", "/bin"],
onlyif => "test -f ${file}"
}
file {"/target/file":
ensure => file,
source => 'file:/source/file',
owner => 'root',
group => 'root',
mode => '0750',
require => Exec["chk_${file}_exist"],
}

How to work around vcsrepo "duplicate declaration" evaluation error?

I am installing from github using puppet-vcsrepo. The code looks something like this:
class demo_class(
$my_repo = undef,
$my_tag = undef,
){
vcsrepo { "$my_repo",
path => "/home/user/$my_repo",
source => 'git#github.com:7yl4r/$my_repo.git',
ensure => latest,
provider => git,
}
# then declare resources specific to my_tag
}
This works just fine when called only once, but I am iterating over a list and installing dependencies so this resource sometimes gets declared twice. I think this is roughly equivalent to the code below.
class {"demo_class":
my_repo => test_repo,
my_tag => test_tag1,
}
class {"demo_class":
my_repo => test_repo,
my_tag => test_tag2,
}
Doing this yields a server-side "Duplicate declaration" error because vcsrepo is trying to map the the same path twice. However, this is exactly the behavior I want: for both resources declared by demo_class to require the same repo in the same location. This is so that I can declare one or more resources using demo_class and be sure the repo given by my_repo (which may be common to multiple my_tags) is there.
How can I modify this class so that I can call it twice without hitting an error?
I see the problem.
I reproduced the issue using this code:
define my_vcs_repo ($myRepo, $myTag) {
vcsrepo { "$myRepo-$myTag":
path => "/home/user/$myRepo",
source => "git#github.com:7yl4r/$myRepo.git",
revision => $myTag,
ensure => latest,
provider => git,
}
}
$data = [
{
myRepo => testRepo,
myTag => testTag1,
},
{
myRepo => testRepo,
myTag => testTag2,
},
]
$data.each |$i, $ref| {
$myRepo = $ref['myRepo']
$myTag = $ref['myTag']
my_vcs_repo { "$myRepo-$i":
myRepo => $myRepo,
myTag => $myTag,
}
}
That then results in:
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Resource Statement, Cannot alias Vcsrepo[testRepo-testTag2] to ["/home/user/testRepo"] at /
Users/alexharvey/git/modules/foo/spec/fixtures/modules/foo/manifests/init.pp:3; resource ["Vcsrepo", "/home/user/testRepo"] already declared at /Users/alexharvey/git/modules/foo/spec/fixtures/modules/foo/
manifests/init.pp:3 at /Users/alexharvey/git/modules/foo/spec/fixtures/modules/foo/manifests/init.pp:3:5 at /Users/alexharvey/git/modules/foo/spec/fixtures/modules/foo/manifests/init.pp:26 on node alexs-macbook-pro.local
The problem is that you are asking Puppet to clone the same Git module to a directory but with two different tags checked out. That does not make sense.
The fix is that you need to specify a unique path in the vcsrepo path attribute, e.g.:
vcsrepo { "$myRepo-$myTag":
path => "/home/user/$myRepo-$myTag",
source => "git#github.com:7yl4r/$myRepo.git",
revision => 'production',
ensure => latest,
provider => git,
}
By the way, I notice you are using camelCase for your variables. Don't do that. Aside from the fact that it is not idiomatic for Puppet, there are things that will break in some versions of Puppet/Rspec puppet that I have seen.
Use snake_case for your variable names and class parameter names.
Update
The question has been edited, and it is now a question about how to declare the same vcsrepo in more than one class.
In general, try to refactor so that you do not need to do this in the first place. In other words, just move it out of this class and put it somewhere that is only expected to be declared once.
If you cannot do this for some reason, then you can also use virtual resources, which will allow you to declare it in multiple classes that will be declared on the same node.
To do that, you just have to rewrite what you have there as:
#vcsrepo { $my_repo:
path => "/home/user/$my_repo",
source => "git#github.com:7yl4r/$my_repo.git",
ensure => latest,
provider => git,
}
realize Vcsrepo[$my_repo]
Keep in mind that you will not be able to declare the class demo_class twice on the same node either. You would need to turn it into a defined type, as I did above.
It is mentioned in the comments below that you can also use ensure_resource and ensure_resources; see docs in stdlib.

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
}

Duplicate declaration error with puppet

I try to create a folder then trying to copy some files into it like below.
init.pp
$tempfolder = "D:/TempFolder/"
file { [ $tempfolder ]:
ensure => "directory",
}
file { [ $tempfolder ]:
ensure => present,
recurse => true,
source => "E:/TestFiules",
}
When I try to run this, it is giving below error
Error: Duplicate declaration: File [ D:/TempFolder/ ] is already declared.
What is the wrong in the usage?
For reference: http://docs.puppetlabs.com/guides/techniques.html#how-can-i-manage-whole-directories-of-files-without-explicitly-listing-the-files
Thus you could do
file { "$tempfolder":
ensure => directory,
recurse => true,
source => "E:/TestFiules",
}
The ensure => directory also ensures that it will be present, so you don't have to declare it again.
A node can only have one resources with the same name declared, in this case the $tempfolder.
Either the $tempfolder is created empty (your first declaration) or created and populated with your E:/TestFiule content (the second declaration).
Note that you can drop the array syntax it's often used to create multiple directory at once or ensure an order like creating a tree
I came across this message when, but the error was:
Error: Duplicate declaration: File[] is already declared in file init.pp:40; cannot redeclare at init.pp:46 on node (redacted)
The file it was looking up was not defined, because my Hiera configuration was incorrect. As a result it was declaring two files named "".
Verify that Hiera is passing values correctly.

Resources