I would like to know if there is any way of checking if a string exists inside another string (ie contains function). I have been taken a look to http://forge.puppetlabs.com/puppetlabs/stdlib but I haven't found this specific function. Maybe this is possible through a regexp, but I am not really sure how to do it. Can anybody help me this one?
Puppet treats strings as sequences of bytes. It does not recognize encodings or translate between them, and non-printing characters are preserved. However, all strings must be valid UTF-8. Future versions of Puppet might impose restrictions on string encoding, and using only UTF-8 protects you in this event.
To check if a String str1 contains another string str2 in Java, use String.contains () method. Call contains () method on the string str1 and pass the other string str2 as argument. If str1 contains the string str2, then contains () method returns true.
Also, PuppetDB removes invalid UTF-8 characters when storing catalogs. Access substrings of a string by specifying a numerical index inside square brackets. The index consists of one integer, optionally followed by a comma and a second integer, for example $string [3] or $string [3,10].
It does not recognize encodings or translate between them, and non-printing characters are preserved. However, all strings must be valid UTF-8. Future versions of Puppet might impose restrictions on string encoding, and using only UTF-8 protects you in this event.
There is an "in" operator in Puppet:
# Right operand is a string:
'eat' in 'eaten' # resolves to true
'Eat' in 'eaten' # resolves to true
# Right operand is an array:
'eat' in ['eat', 'ate', 'eating'] # resolves to true
'Eat' in ['eat', 'ate', 'eating'] # resolves to true
# Right operand is a hash:
'eat' in { 'eat' => 'present tense', 'ate' => 'past tense'} # resolves to true
'eat' in { 'present' => 'eat', 'past' => 'ate' } # resolves to false
# Left operand is a regular expression (with the case-insensitive option "?i")
/(?i:EAT)/ in ['eat', 'ate', 'eating'] # resolves to true
# Left operand is a data type (matching integers between 100-199)
Integer[100, 199] in [1, 2, 125] # resolves to true
Integer[100, 199] in [1, 2, 25] # resolves to false
This is quite easy to do, check out the docs here: http://docs.puppetlabs.com/puppet/2.7/reference/lang_conditional.html
A simple example:
if $hostname =~ /^www(\d+)\./ {
notice("Welcome to web server number $1")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With