Execute Puppet Code depending upon service status - puppet

How can we write a puppet manifest code that identifies whether a service(httpd) is running or not on puppet clients/agents. And if not, it should start that service and send out an email ?
class apache {
package { mysql-server: ensure => installed }
if hasstatus == "false" {
service { "mysql":
ensure => running,
require => Package["mysql-server"],
}
}
}
node default {
include apache
}
I know this is not a correct code. But I want to check hasstatus first and if the service status is false then I want to start service and send out an email.
Thanks
Sanket Dangi
I have configured tagmail.conf in puppet master and have also enabled puppet reports but not able to receive mails to my gmail account. I can see puppet agent reports on puppet master but not receiving mails. Do I need to configure mail server for this ?
My Tagmail Conf :
all: xxxxxxx#gmail.com

Puppet isn't an imperative shell script where you need to check the value of X before performing action Y that gets you to state Z. Instead, you specify that you want state Z and Puppet checks the current state and handles the transition.
What this means is that you don't need to check the status of a service before deciding whether to start it or not and instead you declare that the mysql service should be running and Puppet ensures this is the case.
Simply have this in your manifest alongside the package line:
service { "mysql":
ensure => running,
enable => true,
require => Package["mysql-server"],
}
The require line ensures the package is installed before evaluating or starting the service.
To send out notifications you can use the tagmail reporting feature in Puppet. First set up a tagmail file (reference docs) like this at /etc/puppet/tagmail.conf on the master:
mysql, apache: wwwadmins#example.com
And in the master's puppet.conf, set:
[master]
reports = tagmail
Ensure clients have report enabled in puppet.conf:
[agent]
report = true
This should then trigger e-mails relating to any resources with the "mysql" or "apache" tags (class names, module names etc).

Related

Puppet command does not start stop service in ubuntu

service{'cron':
ensure => 'running',
enable => 'true',
}
Error:
change from 'running' to 'stopped' failed: systems stop for cron failed.
Drop this
service { 'crond':
ensure => 'running',
enable => 'true',
}
Into a file on a server, let's call the file crontest.pp then as root run puppet apply crontest.pp you should see cron start.
Also, if you're trying to debug this sort of thing a good starting place is to use puppet resource in this case puppet resource service, you should be able to see a list of all your services. Look through that to find the one relating to cron, it gives you the Puppet code for it's current state so you can copy that directly into a class file, just ignore the provider => line as the Puppet resource abstraction layer will take care of that.

Puppet : How to use service with schedule

I would like to schedule a service restart during a certain period (schedule).
I have attempted a number of variations but none have been successful. Hoping someone knows what I am doing wrong :
schedule { 'my_sched':
range => '11:00 - 11:20',
period => daily,
repeat => 1,
}
service { 'my_service':
ensure => 'running',
enable => true,
schedule => 'my_sched',
}
If I replace service with exec, it works fine.
If I replace schedule with a file dependency, it works as well.
But service does not work with schedule
It is explained in the docs here that:
Currently, schedules can only be used to stop a resource from being applied; they cannot cause a resource to be applied when it otherwise wouldn’t be, and they cannot accurately specify a time when a resource should run.
And that's the problem ; the service will already be running in your schedule, so Puppet will see that it is running and find there is nothing to do.
Maybe just use cron? I would get Puppet to install a cron job that restarts the service instead.

Change Foreman UI to ignore Puppet resource 'Notify' messages

We're using Puppet + Foreman to monitor changes in environment by checking custom facts. For example, whenever a custom fact equals 'true' puppet calls the Notify resource with a message sent to the agent log. Puppet includes this message in the agent report and Foreman shows this in UI.
Problem is that whenever a message is thrown, Foreman considers this action as "Applied" and the node status changes to "Active" (blue icon).
We want to keep the node at "No Changes" (Green) plus show the Notify message.
Is that possible in some way? Maybe define a new custom resource type?
Here is the puppet code:
class mymodule::myclass::mysubclass {
if $::fact023 == 'fail' {
notify {'mynotify1':
message => "WARNING: Node ${::fqdn} failed fact023",
loglevel => hiera('warnings_loglevel'),
} } }
See screenshot of Foreman here
Update:
I'll refine the question: Is there a way to use the Notify resource without causing puppet to report that the node has changed? Meaning just print the message to client log (and therefore the message will be visible in report) but without puppet classify the event as an applied configuration?
The reason is that when puppet triggers the Notify resource, Foreman flags the node as being active (changed)
UPDATE #2
I'm thinking about changing Foreman report file so that the UI will ignore Notify events so that the node's status will remain unchanged but still show the message in the report. Can someone point me to the right direction? Thanks!
UPDATE #3
Problem fixed after switching from the "Notify" resource type to custom type "echo" created by some dude in Puppet Forge. Thanks!
It's not completely clear what you are trying to accomplish. One option would be to use the notice function instead of a resource. Functions execute on the puppet master, so you the log will end up in the puppet master's logs instead of the agent report. That also means that it will not count as an applied resource, and the node should appear to be stable.

Notify in verbose or debug mode only

I'm looking for a way to display a notify { ... } message during puppet client run only when using
puppet apply <--verbose | --debug>
puppet agent <--verbose | --debug>
I'd like to display debug messages when interactively/manually running puppet on clients.
Yet, I couldn't find a way to evaluate these switches in my puppet manifests. How could I achieve this for puppet 4.x?
All Puppet resources support the loglevel metaparameter, including notify. Set it to debug to make a resource report on that level.
notify { "this is a debug message": loglevel => "debug" }
Output from such resources is hidden by default.
The verbose option does not imply a specific log level.

puppet and puppetdb debugging ##file - puppetmaster not pushing config

I am using the backuppc module that has the following code for the client:
##file { "${backuppc::params::config_directory}/pc/${::fqdn}.pl":
ensure => $ensure,
content => template("${module_name}/host.pl.erb"),
owner => 'backuppc',
group => $backuppc::params::group_apache,
mode => '0640',
tag => "backuppc_config_${backuppc_hostname}"
}
While the backuppc server uses this code to create the files:
File <<| tag == "backuppc_config_${::fqdn}" |>> {
group => $backuppc::params::group_apache,
notify => Service[$backuppc::params::service],
require => File["${backuppc::params::config_directory}/pc"],
}
Twenty seven other hosts have their files written out,for some darn reason the puppet master is not pushing the config. The class on the client, puppetmaster, ran because the other components worked. I can see the class running with a do a puppet agent --debug --test, but I am looking for recommendations on how to debug this further. Other servers are running fine. It is the puppet master that is not pushing the config to backuppc_config tag.
There are two things that are missing:
1. a config file that is created
2. an entry in a host file for backuppc
As I mentioned multiple other servers are working, but not the darn puppetmaster, which is just kinda important to backup.
Adding notify debugging is showing that the command is running, but when I poll the /v3/resources url, the resource is not showing up. The other resources that are not exported are showing up, but for some reason the puppet on the puppet master is not exporting that resource.
To add more fun to this I am seeing puppetdb insert the resource into the database.
2014-05-16 17:31:53,688 DEBUG [command-proc-69] [bonecp.PreparedStatementHandle] INSERT INTO catalog_resources (catalog_id,resource,type,title,tags,exported,file,line) VALUES (6,'14be980772ccedd05d2c7e8d31563f20159b71a1','File','/etc/backuppc/pc/myserver_redacted.com.pl',(array of type7),true,'/etc/puppet/modules/backuppc/manifests/client.pp',418)
2014-05-16 17:31:53,689 DEBUG [command-proc-69] [bonecp.PreparedStatementHandle] INSERT INTO catalog_resources (catalog_id,resource,type,title,tags,exported,file,line) VALUES (6,'65c88548872aba1ae75da554e75192279da41e8a','File_line','backuppc_host_/myserver_redacted.igivefirst.com',(array of type7),true,'/etc/puppet/modules/backuppc/manifests/client.pp',407)
I actually filed a bug on puppetdb on this item: https://tickets.puppetlabs.com/browse/PDB-672

Resources