Inline if statement for puppet parameters - puppet

I have a following puppet module
class base (
$someBoolean=false,
)
{
exec { 'Do something':
command => '/usr/bin/someStuff',
timeout => (someBoolean) ? 100000000 : 300
}
}
The timeout => () ? : is enssentially what I want to do, but what is the correct syntax to do it? Is it possible at all?

Puppet's version of the ternary operator is the more general "selector". The syntax for your case looks like this:
exec { 'Do something':
command => '/usr/bin/someStuff',
timeout => $someBoolean ? { true => 100000000, default => 300 }
}
The control expression ($someBoolean in the above) can in fact be any expression that produces a value, and any number of corresponding cases can be provided.

Related

Why eslint throw error when I want check array length?

why eslint throw error ( no-unused-expression ). I simply want to check if array is empty or not, and execute dispatch.
useEffect(() => {
let totalPrice = 0;
// eslint-disable-next-line no-unused-expressions
cart.length
? cart.forEach((item: AsideItemInterface) => {
totalPrice += item.quantity * item.price;
dispatch(setTotal(totalPrice));
})
: dispatch(setTotal(0));
}, [cart]);
If, I remove eslint-disable-next-line, get error.
The problem is you're writing an expression that has a value result, but you're not using that result. More specifically, you're using the ? : operators like an if statement. The solution is to write the if statement that you're actually using (i.e if (cart.length) { ... } else { ... }).

Why isn't /usr/local/bin/ checked in check_flexlm.pl ? Source included

Eventhough I have /usr/local/bin/lmstat the below script always fails with Cannot find "lmstat".
Can anyone see why that is the case?
use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_F $opt_t $verbose $PROGNAME);
use FindBin;
use lib "$FindBin::Bin";
use lib '/usr/lib64/nagios/plugins';
use utils qw(%ERRORS &print_revision &support &usage);
$PROGNAME="check_flexlm";
sub print_help ();
sub print_usage ();
$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';
Getopt::Long::Configure('bundling');
GetOptions
("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$verbose, "verbose" => \$verbose,
"F=s" => \$opt_F, "filename=s" => \$opt_F,
"t=i" => \$opt_t, "timeout=i" => \$opt_t);
if ($opt_V) {
print_revision($PROGNAME,'2.2.1');
exit $ERRORS{'OK'};
}
unless (defined $opt_t) {
$opt_t = $utils::TIMEOUT ; # default timeout
}
if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
unless (defined $opt_F) {
$opt_F = $ENV{'LM_LICENSE_FILE'};
unless (defined $opt_F) {
print "Missing license.dat file\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
}
}
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print "Timeout: No Answer from Client\n";
exit $ERRORS{'UNKNOWN'};
};
alarm($opt_t);
my $lmstat = $utils::PATH_TO_LMSTAT ;
unless (-x $lmstat ) {
print "Cannot find \"lmstat\"\n";
exit $ERRORS{'UNKNOWN'};
}
Never assume you know what something is. Try printing the path to verify it is what you think it is:
unless (-x $utils::PATH_TO_LMSTAT ) {
print qq/Cannot find "lmstat" at <$utils::PATH_TO_LMSTAT>\n/;
exit $ERRORS{'UNKNOWN'};
}
If $utils::PATH_TO_LMSTAT is a relative path (such as lmstat by itself) the -x is looking in the current directory. If it's a full path, maybe you have the string wrong.
Note that your options handling can be a bit less unwieldy since you can specify multiple names for options in the same key:
GetOptions(
"V|version" => \$opt_V,
"h|help" => \$opt_h,
"v|verbose" => \$verbose,
"F|filename=s" => \$opt_F,
"t|timeout=i" => \$opt_t,
);
The "Secure Programming Techniques" chapter of Mastering Perl discusses many of the headaches of programs that call external programs.

How to call another filter from within a ruby filter in logstash.

I'm building out logstash and would like to build functionality to anonymize fields as specified in the message.
Given the message below, the field fta is a list of fields to anonymize. I would like to just use %{fta} and pass it through to the anonymize filter, but that doesn't seem to work.
{ "containsPII":"True", "fta":["f1","f2"], "f1":"test", "f2":"5551212" }
My config is as follows
input {
stdin { codec => json }
}
filter {
if [containsPII] {
anonymize {
algorithm => "SHA1"
key => "123456789"
fields => %{fta}
}
}
}
output {
stdout {
codec => rubydebug
}
}
The output is
{
"containsPII" => "True",
"fta" => [
[0] "f1",
[1] "f2"
],
"f1" => "test",
"f2" => "5551212",
"#version" => "1",
"#timestamp" => "2016-07-13T22:07:04.036Z",
"host" => "..."
}
Does anyone have any thoughts? I have tried several permutations at this point with no luck.
Thanks,
-D
EDIT:
After posting in the Elastic forums, I found out that this is not possible using base logstash functionality. I will try using the ruby filter instead. So, to ammend my question, How do I call another filter from within the ruby filter? I tried the following with no luck and honestly can't even figure out where to look. I'm very new to ruby.
filter {
if [containsPII] {
ruby {
code => "event['fta'].each { |item| event[item] = LogStash::Filters::Anonymize.execute(event[item],'12345','SHA1') }"
add_tag => ["Rubyrun"]
}
}
}
You can execute the filters from ruby script. Steps will be:
Create the required filter instance in the init block of inline ruby script.
For every event call the filter method of the filter instance.
Following is the example for above problem statement. It will replace my_ip field in event with its SHA1.
Same can be achieved using ruby script file.
Following is the sample config file.
input { stdin { codec => json_lines } }
filter {
ruby {
init => "
require 'logstash/filters/anonymize'
# Create instance of filter with applicable parameters
#anonymize = LogStash::Filters::Anonymize.new({'algorithm' => 'SHA1',
'key' => '123456789',
'fields' => ['my_ip']})
# Make sure to call register
#anonymize.register
"
code => "
# Invoke the filter
#anonymize.filter(event)
"
}
}
output { stdout { codec => rubydebug {metadata => true} } }
Well, I wasn't able to figure out how to call another filter from within a ruby filter, but I did get to the functional goal.
filter {
if [fta] {
ruby {
init => "require 'openssl'"
code => "event['fta'].each { |item| event[item] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, '123456789', event[item] ) }"
}
}
}
If the field FTA exists, it will SHA2 encode each of the fields listed in that array.

conditional within define in puppet

Running Puppet 3.8
I have two defines:
define desktop::vinstall () {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
}
}
and
define desktop::vinstallwseeds () {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
file { "/var/cache/debconf/pkg-${title}.seeds":
source => "puppet:///modules/desktop/pkg-${title}.seeds",
ensure => present,
}
}
Would like to turn these into one define statement with an optional boolean argument, something like:
define desktop::vinstallopt ( $queryresponse = 'false', ) {
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
if $queryresponse == 'true' {
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
}
file { "/var/cache/debconf/pkg-${title}.seeds":
source => "puppet:///modules/desktop/pkg-${title}.seeds",
ensure => present,
}
}
and then instantiate it with statements like this one in init.pp:
#desktop::vinstallopt { 'gdebi': queryresponse => 'false', }
But doing so gives an error:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Puppet::Parser::AST::Resource failed with argument error ArgumentError: Invalid resource type desktop::vinstallopt at /etc/puppet/modules/desktop/manifests/init.pp:40 on node machine.prvt.net
where line 40 has the syntax above. I'm a newbie with puppet, so my apologies if this turns out the be a simple syntax question. I've tried to find a way to make this work from the PuppetLabs documentation and from other puppet users, so far without luck.
You are trying to embed an if block inside a resource declaration. Alas, this is not possible. The block must be global or in a regular block (e.g., class body, define body, lambda body).
In this case, you want to "amend" the package resource, so to speak. I like to use the following construct for this purpose:
package { $title:
ensure => installed,
allow_virtual => true,
configfiles => keep,
}
if $queryresponse {
Package[$title] {
require => File["/var/cache/debconf/pkg-${title}.seeds"],
responsefile => "/var/cache/debconf/pkg-${title}.seeds",
}
}
Please note that this override syntax is only allowed in this scope because the require and responsefile attributes don't have any value assigned originally.

Defining and calling function in puppet 2.6 and 2.7

I have a module core and a class core::logrotate defined in core/manifests/logrotate.pp.
class core::logrotate {
#...some stuff here
#
define confd ($ensure = "present" , $log_name = "dummy" ) {
if ( $ensure == present )
{
file {
"/etc/logrotate.d/$log_name":
ensure => present,
source => filelookup("core/${log_name}.logrotate"),
}
} else {
file {
"/etc/logrotate.d/$log_name":
ensure => absent,
}
}
}
}
calling this function inside of templates.pp as
core::logrotate::confd { "mkill": log_name => mkill }
This fails with the error
Error 400 on SERVER: Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type core::logrotate::confd
If the puppet master version is 2.6.x then this fails, to make it work there used to be a import "*" in the init.pp of the module. Now removed this as moving to puppet 2.7.20.
The code pasted here works in 2.7 but fails in 2.6. Any idea why? how can I make it work for both 2.6 and 2.7?
You should take the define outside of the class, see the style guide: http://docs.puppetlabs.com/guides/style_guide.html#classes
Also, I think that you might be using modules wrong, it would be much more logical to have a 'logrotate' module on its own.
So; in modulepath/logrotate/manifests/confd.pp you'd put this:
define logrotate::confd ($ensure = "present" , $log_name = "dummy" ) {
if ( $ensure == present )
{
file {
"/etc/logrotate.d/$log_name":
ensure => present,
source => filelookup("core/${log_name}.logrotate"),
}
} else {
file {
"/etc/logrotate.d/$log_name":
ensure => absent,
}
}
}
That should make it work properly.
Greetings,
Ger

Resources