What does 'if $variable {}' mean in Puppet? - puppet

If I have the following if statement in puppet:
if $variable {
do something
}
What does this mean? Is it if $variable is true? If it exists? If it is defined?

Read the documentation.
“If” statements
“If” statements take a boolean condition and an arbitrary block of Puppet code, and will only execute the block if the condition is true. They can optionally include elsif and else clauses.
https://docs.puppet.com/puppet/latest/reference/lang_conditional.html#if-statements

Related

What does '! -e "/etc/httpd"' in Perl in 'if' Condition

Please explain what following code snippet could do in Perl script,
if (! -e '/etc/httpd') {
print "Something";
}
I can't find exact scenario or condition that makes this condition 'true'. This question also not explain much regarding this.
Exactly, what -e switch does in this condition?
The -e filename is one of filetests ("-X"), which checks for existence of a "file" (an entry, not only a plain file).
So the condition tests whether /etc/httpd does not exist, since the unary operator ! does logical negation of what is on its right-hand side using Perl's rules for truth and falsehood.

puppet in not in code

I have this code...
if $hostname in $var_slave {
file {
"/var/spool/cron/mysql":
ensure => present,
owner => $mysqlUser,
group => $mysqlGroup,
mode => "0600",
source => 'puppet:///modules/eikonappdbcron/mysql-slave',
}
}
I want a way of checking if a value is not in a file.
I tried "if $hostname not in $var_master" but this doesn't work. After doing some research I believe I need to use an "!". I can't get the syntax correct.
Puppet does not have a single, combined "not in" operator. Instead, it has the in operator, and it has a general-purpose boolean negation operator (!). You can use these together to write a compound expression that evaluates the condition you want.
If you already realized that, then perhaps you ran into a problem with operator precedence. The negation operator has the higher precedence than does in. Indeed, ! has the highest precedence of any Puppet operator, so if its operand is intended to be a binary expression then you must enclose the operand in parentheses.
Thus, the negation of the boolean expression
$hostname in $var_master
is
! ($hostname in $var_master)

A variable of which a part is variable

I have variables that look something like this:
$INFOMonday
$INFOTuesday
In my Bash script, I would like to use the value of todays variable, so to say. I extracted the day with:
dayofweek=$(date --date=${dateinfile#?_} "+%A")
What I need help with, is doing the following:
echo $INFO$dayofweek
Is there any way of adding a variable as part of a variables name? For example, if it's monday, the echo would return the value of $INFOMonday.
The old-style way of doing this is with the indirection operator ${!variable}:
dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}
Note that in ${!var}, var must be a variable-name, not a string expression or other type of substitution. It's a little clunky.
The newer way of doing it would be with an associative array.

How to compare alphanumeric values in Perl?

Here is my code-
print $cpu_arch."\n";
if ($cpu_arch eq'AMD64') {
print "Remote machine is 64bit\n";
}
Depending upon the CPU architecture, $cpu_arch returns x86 or AMD64.
The problem is that no matter what $cpu_arch returns, the if loop condition is not getting satisfied. I've tried chomp'ing the $cpu_arch but that doesn't seem to help either.
The operator for string is eq as you have. Your issue isn't in your operator it is in the data. More than likely you are buffering improperly ( change the $| variable ) or your $cpu_arch has trailing white-space. Or maybe you are looking for regex match and not literal match ( then you want /AMD64/ )
Your code is correct. Either $cpu_arch doesn't contain AMD64, or you are mistaken about the if condition being false (perhaps because of buffering).
The following might help you examine $cpu_arch.
use Data::Dumper;
{
local $Data::Dumper::Useqq = 1;
print(Dumper($cpu_arch));
}

Default values for the arguments to a Unix shell script?

Normally when a parameter is passed to a shell script, the value goes into ${1} for the first parameter, ${2} for the second, etc.
How can I set the default values for these parameters, so that if no parameter is passed to the script, we can use a default value for ${1}?
You can't, but you can assign to a local variable like this: ${parameter:-word} or use the same construct in the place you need $1. this menas use word if _paramater is null or unset
Note, this works in bash, check your shell for the syntax of default values
You could consider:
set -- "${1:-'default for 1'}" "${2:-'default 2'}" "${3:-'default 3'}"
The rest of the script can use $1, $2, $3 without further checking.
Note: this does not work well if you can have an indeterminate list of files at the end of your arguments; it works well when you can have only zero to three arguments.
#!/bin/sh
MY_PARAM=${1:-default}
echo $MY_PARAM
Perhaps I don't understand your question well, yet I would feel inclined to solve the problem in a less sophisticated manner:
! [[ ${1} ]] && declare $1="DEFAULT"
Hope that helps.

Resources