how to make sure the file created before class import? - puppet

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

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.

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: how to share a common resource / variable in modules

I am trying to use puppet to bootstrap a new VPS. I am going to be running multiple sites, and, at the moment I am planning on running them in separate user accounts. I want to have a common authorized key for all of these users. I am using puppet 4.10.
The issue I'm having is that I want to add my ssh key into the authorized_keys for all of these users, but I can't seem to work out how to have a common resource. I've tried adding it in a class and then including that, but it's a duplicate. I tried passing in a variable to the class, but again, duplicate.
Basically I have a module like this
class wibble_somesite {
user { 'someuser':
ensure => 'present',
managehome => true,
purge_ssh_keys => true,
home => '/home/someuser',
shell => '/bin/bash'
}
ssh_authorized_key { 'patrickmacbookair':
ensure => present,
user => 'someuser',
type => 'ssh-rsa',
key => 'some_shared_key'
}
}
which I then include in my manifests/site.pp. However, I want to have multiples of these class wibble_someothersite and I want to centrally manage the some_shared_key inside the ssh_authorized_key stanza.
Any help would be appreciated. I have tried following the docs but I just haven't got anywhere.
I could just duplicate all the ssh_authorized_key calls, but that's obviously horrible.
You cannot have multiple instances of a class. However, you can with defined types.
Your example can be :
define wibble_somesite () {
user { $title:
ensure => 'present',
managehome => true,
purge_ssh_keys => true,
home => "/home/${title}",
shell => '/bin/bash'
}
ssh_authorized_key { "${title}_patrickmacbookair":
ensure => present,
user => $title,
type => 'ssh-rsa',
key => 'some_shared_key'
}
}
And you can use it like this :
wibble_somesite{'patrick':}
wibble_somesite{'bob':}
It will create users 'patrick' and 'bob', and allow the shared key to connect.
Is this what you are are looking for ?

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
}

Puppet: Multiple files based on templates

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.

Resources