skip puppet functions when there is no change in file content - puppet

I want to skip certain exec, and file resources when there is no change in file content. Its working for file and service combination...
For example,
file { 'configfile.cfg':
ensure => file,
path => '/etc/configfile.cfg',
mode => '0644',
owner => 'root',
group => 'root',
content => template($template_file),
require => Package[$main_package],
notify => Service[$service],
}
service { $service:
ensure => $ensure,
enable => $enable,
hasrestart => true,
hasstatus => true,
require => [ Package[$main_package], File['configfile.cfg'] ],
}
The above code is working as expected. Service restarts only if it detects any change in /etc/configfile.cfg..
But I am following the same approach for file and exec combination, but its not working.... please see the below code
exec { 'purge-config-files':
before => [File["${config_file_service}"], File["${config_file_host}"]],
command => "/bin/rm -f ${baseconfigdir}/*",
notify => Domain_ip_map[$domain_ip_map_titles],
}
file { 'deployconfig.cfg':
ensure => file,
path => '/home/path/deployconfig.cfg',
mode => '0644',
owner => 'root',
group => 'root',
content => "test",
notify => Exec['purge-config-files'],
}
This code is not working as expected. Even if there is no change in
/home/path/deployconfig.cfg, Exec['purge-config-files'] is always
executing... what could be the reason for this?

I found the answer
exec { 'purge-config-files':
before => [File["${config_file_service}"], File["${config_file_host}"]],
command => "/bin/rm -f ${baseconfigdir}/*",
notify => Domain_ip_map[$domain_ip_map_titles],
subscribe=> File['deployconfig.cfg'],
refreshonly => true,
}
I forgot to put subscribe and refreshonly
....

Related

Sensu-Go ::: Set handler for keepalive check with puppet

I want to be able to get notified when a server is down.
Puppet: sensu/sensu-puppet v5.9.0
Based on https://github.com/sensu/sensu-go/issues/1960, I tried this code without success.
Since there is a special static handler called "keepalive", I create a set handler "keepalive" and include my telegram handler (telegram_ops) in it.
BACKEND Code
class { 'sensu':
api_host => 'sensu3.mydomain.com',
password => '****',
agent_password => '****',
agent_entity_config_password => '****',
ssl_ca_source => 'puppet:///modules/common/ssl/ca.crt',
}
include sensu::cli
class { 'sensu::backend':
ssl_cert_source => 'puppet:///modules/common/ssl/my.crt',
ssl_key_source => 'puppet:///modules/common/ssl/my.key',
config_hash => {
'deregistration-handler' => 'deregistration',
'event-log-file' => '/var/log/sensu/events.log'
}
}
sensu_bonsai_asset { 'sensu/check-cpu-usage':
ensure => 'present',
version => 'latest',
}
sensu_check { 'check-cpu':
ensure => 'present',
labels => {'contacts' => 'ops'},
handlers => ['telegram_ops'],
command => 'check-cpu-usage -w 75 -c 85',
interval => 60,
subscriptions => 'linux',
publish => true,
runtime_assets => ['sensu/check-cpu-usage']
}
sensu_bonsai_asset { 'sensu/sensu-go-has-contact-filter':
ensure => 'present',
version => '0.2.0',
}
sensu_filter { 'contact_ops':
ensure => 'present',
action => 'allow',
runtime_assets => ['sensu/sensu-go-has-contact-filter'],
expressions => ['has_contact(event, "ops")'],
}
sensu_filter { 'first_occurrence':
ensure => 'present',
action => 'allow',
expressions => ['event.check.occurrences == 1'],
}
sensu_bonsai_asset { 'Thor77/sensu-telegram-handler':
ensure => 'present'
}
sensu_handler { 'telegram_ops':
ensure => 'present',
type => 'pipe',
command => 'sensu-telegram-handler --api-token **** --chatid -****',
timeout => 10,
runtime_assets => ['Thor77/sensu-telegram-handler'],
filters => [
'is_incident',
'not_silenced',
'contact_ops',
'first_occurrence'
],
}
sensu_handler { 'keepalive':
ensure => 'present',
type => 'set',
handlers => ['telegram_ops'],
}
AGENT Code (Very simple code.)
class { 'sensu::agent':
subscriptions => ['default', 'linux', $hostname, 'nuc']
}
It does not work. If I suddenly shutdown a server, nothing happeds.
What is the proper way to do this?
It is posible any other aproach?
Long time ago there was another solution, class sensu had the parameter client_keepalive but it is not available anymore.
Thanks.

Logstash not replacing "%type" with value

Hello I have this configuration for a logstash running on my computer :
input {
exec {
command => "powershell -executionpolicy unrestricted -f scripts/windows/process.ps1 command logstash"
interval => 30
type => "process_data"
codec => line
tags => [ logstash" ]
}
}
output
{
if "sometype-logs" in [tags] {
elasticsearch {
action => "index"
doc_as_upsert => true
index => "sometype-logs-%{+YYYY.MM.dd}"
hosts => "locahost:9200"
template_overwrite => true
}
} else {
elasticsearch {
action => "index"
doc_as_upsert => true
index => "%{type}"
hosts => "localhost:9200"
template_overwrite => true
}
}
When displaying indexes I have :
Why is index name is "%type" and not "process_data" ?
Probably just something about syntax. To used some field of the data, you must use this syntax
%{[somefield]}
(see example on this documentation page)
So, in your case, try this :
"%{[type]}"
in place of
"%{type}"

After adding a config file to the logstash with a mysql driver, the data is no longer sent to the elastic

In the logstash configuration files with PostgreSQL queries already existed. After adding the config file with mysql, after some time the data ceases to arrive in the elastic, tcpdump on the outgoing port also shows the absence of data, while requests to other servers are made. There are no errors in the logs. When debug is enabled, it shows that the config is re-read and that's it.
On another server, where this config is only one, logstash works fine.
In what there can be an error? Where to look. Tell me please.
input {
jdbc {
jdbc_driver_library => "/etc/logstash/mysql-connector-java-5.1.46-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://xx.xx.xx.xx:3306/database"
jdbc_user => "user"
jdbc_password => "*************"
schedule => "0-59 * * * *"
statement => "SELECT * FROM `database`.table WHERE calldate > :sql_last_value"
tracking_column => "calldate"
tracking_column_type => "timestamp"
use_column_value => true
add_field => { "typetable_id" => "table" }
}
}
output {
if [typetable_id] == "table" {
elasticsearch {
hosts => "xx.xx.xx.xx:9200"
index => "data_index"
user => "elastic"
password => "***********"
}
}
}

Loop over multiple variables in Puppet

In Puppet, I made my own module that adds administrator accounts to our management servers.
class admins::add_admin($username, $userkey) {
$username.each |String $username| {
file { "/home/${username}":
ensure => directory,
mode => '0750',
owner => $username,
}
user { $username:
ensure => present,
shell => '/bin/bash',
}
ssh_authorized_key { $username:
ensure => present,
user => $username,
type => 'ssh-rsa',
key => $userkey,
}
}
}
$username is an array of the desired usernames and $userkeys is an array of the ssh keys.
When the each loop is run, the users are created accordingly, however the keys are the same for every user (which is logical, because I don't have functionality yet to iterate over the userkeys).
What I want is that this Puppet module iterates over two arrays, but I don't know how to do that.
You could do it this way:
class admins::add_admin (
Array[Hash[String, String]] $users_data = [], # declare data type and defaults
) {
$users_data.each |Hash $user| {
$username = $user['username']
$userkey = $user['userkey']
file { "/home/${username}":
ensure => directory,
mode => '0750',
owner => $username,
}
user { $username:
ensure => present,
shell => '/bin/bash',
}
ssh_authorized_key { $username:
ensure => present,
user => $username,
type => 'ssh-rsa',
key => $userkey,
}
}
}
And then you'd pass data in that looks like this:
class { 'admins::add_admin':
users_data => [
{
'username' => 'bill',
'userkey' => 'keydata1',
},
{
'username' => 'ted',
'userkey' => 'keydata2',
},
]
}
I think it is much better here to restructure your input data than try to deal with two arrays.

Getting "Archive[/tmp/${filename}] doesn't seem to be in the catalog" while executing my puppet program

My Puppet class looks like this:
class aoa_agent_installation::splunk_install {
$sourcefile = $osfamily ? {
'Solaris' => 'puppet:///modules/aoa_agent_installation/splunkforwarder-6.5.2-67571ef4b87d-SunOS-sparc.tar.Z',
'RedHat' => 'puppet:///modules/aoa_agent_installation/splunkforwarder-6.5.2-67571ef4b87d-Linux-x86_64.tgz',
'AIX' => 'puppet:///modules/aoa_agent_installation/splunkforwarder-6.5.2-67571ef4b87d-AIX-powerpc.tgz',
}
$dirname = 'splunkforwarder'
$filename = "${dirname}.tar.gz"
$install_path = "/opt/${dirname}"
file { $install_path:
ensure => directory,
owner => 'findev',
group => 'finacle',
mode => '0777',
}
archive { $filename:
path => "/tmp/${filename}",
source => $sourcefile,
extract => true,
extract_path => '/opt',
creates => "${install_path}",
cleanup => true,
user => 'findev',
group => 'finacle',
require => File[$install_path],
}
exec {'start_splunk_service':
path => '/usr/bin:/bin:/usr/sbin:/sbin',
user => 'findev',
command => '/opt/splunkforwarder/bin/splunk start --accept-license && echo "1" > /tmp/splunk_status.txt',
onlyif => [
"test -f /opt/splunkforwarder/bin/splunk",
"test `ps -ef | grep -v grep | grep splunk | wc -l` -eq 0",
],
environment => ["HOME=/app/finacle"],
require => Archive[$filename],
}
file { '/opt/splunkforwarder/etc/system/local/deploymentclient.conf':
ensure => present,
owner => 'findev',
group => 'finacle',
mode => "0777",
source => 'puppet:///modules/aoa_agent_installation/deploymentclient.conf',
require => Archive[$filename],
}
exec {'restart_start_splunk_service':
path => '/usr/bin:/bin:/usr/sbin:/sbin',
user => 'findev',
command => '/opt/splunkforwarder/bin/splunk restart && echo "0" > /tmp/splunk_status.txt',
onlyif => [
"test -f /opt/splunkforwarder/bin/splunk",
"test `cat /tmp/splunk_status.txt | tr -s ' '` -eq 1",
],
environment => ["HOME=/app/finacle"],
require => File['/opt/splunkforwarder/etc/system/local/deploymentclient.conf'],
}
}
I am getting the below error:
Error :
Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Invalid relationship: Exec[start_splunk_service] { require => Archive[/tmp/${filename}] }, because Archive[/tmp/${filename}] doesn't seem to be in the catalog

Resources