Sanket Dangi
Sanket Dangi

Reputation: 1195

Execute Puppet Code depending upon service status

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: [email protected]

Upvotes: 1

Views: 4504

Answers (1)

Dominic Cleal
Dominic Cleal

Reputation: 3205

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: [email protected]

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).

Upvotes: 7

Related Questions