Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function in puppet for checking if a string contains another string

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?

like image 675
hveiga Avatar asked Oct 09 '13 21:10

hveiga


People also ask

How does puppet handle string encodings?

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.

How to check if a string contains another string in Java?

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.

How do I access substrings in puppetdb catalogs?

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

Does puppet support non-printing characters?

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.


2 Answers

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
like image 132
Sharas Avatar answered Oct 19 '22 10:10

Sharas


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")
}
like image 24
Matt Cooper Avatar answered Oct 19 '22 09:10

Matt Cooper