Chef cookbook template REGEX to match - ruby

I have a template file
#fileName: test.properties.erb
<% if #env == 'STAGING' && node['hostname'] =~ /^staging/ %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>
My Recipe is as follows
template ("test.properties") do
source 'test.properties.erb'
variables(
env: "STAGING"
)
end
I wanted to perform && operation along with REGEX check
when i use the below in the erb file it works with &&
<% if #env = 'STAGING' && node['hostname'].include?('staging-01') %>
...
I wanted to apply a regex and check if the hostname starts with string staging
say I have staging-01, staging-02 and apply on both

The answer seems to be simple Ruby string matching expression
So i made the change to expression node['hostname'].match(/staging*/) and this evaluated the cookbook successfully.
#fileName: test.properties.erb
<% if #env == 'STAGING' && node['hostname'].match(/staging*/) %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>

Related

How to use a ERB file in Ruby to write some text in a text file

I have a play.erb file:
<p>Welcome!<br><p>
<% if $wgplaying==true %>
Template: <%= $template %>
<% end %>
<br><br>
<% if $wginword==true && $wgplaying==true && $a!='' %>
Character <%= $a %> is in word.
<% end %>
<% if $wginword==false && $wgplaying==true && $a!='' %>
Character <%= $a %> is not in word.
<% end %>
<br><br>
I want to write
"Character <%= $a %> is in word."
or
"Character <%= $a %> is not in word."
in a text file, but I do not know what command should I use. Or, should I do this in a Ruby file?
ERB is in Ruby's Standard Library, so try something like this:
require 'erb'
TEMPLATE_FILE = 'template.erb'
$wgplaying = true
$template = "This is my template"
$wginword = true
$wgplaying = true
$a = "something"
render = ERB.new(File.read(TEMPLATE_FILE),trim_mode: ">")
puts render.result
The new method has many options.
These are the trim_mode options:
% enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
> omit newline for lines ending in %>
- omit blank lines ending in -%>

ERB file not replacing ENV VAR

I have a simple erb file I'm trying to run and it doesn't seem to replace when I have the environment variables set:
<% if ENV['DEFAULT_ROUTE'] %>
default_route : "<%= ENV['DEFAULT_ROUTE'] %>",
<% else %>
default_route : "/dashboard/file/default.json",
<% end %>
I've checked the environment variable and everything should be working based on what I've read, however I always get the else printed. Any idea what I'm doing wrong?
if you can't tell I'm not a big Ruby dev

How to apply a mapping to a value in a Puppet/Ruby ERB template?

I'm writing an ERB template (for a Puppet module) that gets passed an Hash like this:
{"stuff" => {"foo"=>"aaa", "bar"=>"ccc"},
"other" => {"foo"=>"bbb", "bar"=>"ddd"}}
and I'm iterating over it in my templates producing rows of text:
<% #my_data.each_pair do |k, v| -%>
<%= k %> <%= v["foo"] %>:<%= v["bar"] %>
<% end -%>
Now I'd like to apply some mapping to the "foo" data with a second hash I'll pass to the template. In pseudocode:
mappings = {"aaa" => "something", "bbb" => "somethingelse"}
<% #my_data.each_pair do |k, v| -%>
<%= k %> <%= TRANSLATE_SOMEHOW(v["foo"], mappings) %>:<%= v["bar"] %>
<% end -%>
...in order to get "something" whenever the value was "aaa", and so on. I expect to get the original value if there is no corresponding key in the "mappings".
Doing that kind of thing in Puppet's language is probably possible (by extending it with some Ruby code) I think it is probably more appropriate in the ERB template, but I don't know how to do that and not knowing Ruby isn't helping me - tried google without much success.
I'm looking for code to achieve that in an ERB function or some pointers to relevant documentation for my RTFM pleasure.
EDIT:
for future readers, here's DigitalRoss' answer translated to my ERB example above:
<% #my_data.each_pair do |k, v| -%>
<%= k %> <%= mappings[v["foo"]] || v["foo"] %>:<%= v["bar"] %>
<% end -%>
With the erb stuff removed for clarity, this is what you want to do. (The p() function just prints its argument. You can try this in irb.)
#my_data.each do |k, v|
f, b = v['foo'], v['bar']
p(mappings[f] || f)
p(mappings[b] || b)
end

Conditions not working when rendering partial via ajax

In my index.js.erb
$(".div").html("<%= escape_javascript(render('something/some_form')) %>");
in some_form.htm.erb i have condition which is not working
<% if #variable1 == #variable2 %>
show something1
<% else %>
show something2
<% end %>
For some reason when my js.erb renders this partial i always have show something2. So <% if #variable1 == #variable2 %> condition not working
variable1 and variable2 defined in my controller
How can i make this equal condition work?
Controller variables are not visible within partials. Please, read the official documentation on partials
You need to pass those variables along to the partial via the locals:
//e.g locals: {variable1: #variable1, variable2: #variable2}
$(".div").html("<%= escape_javascript(render('something/some_form', locals: {variable1: #variable1, variable2: #variable2})) %>");
some_form
<% if variable1 == variable2 %>
show something1
<% else %>
show something2
<% end %>

Puppet 3 loop execution only if variable is defined within a template

I am trying to create a loop only if index is defined. But it looks like
erb can't handle a loop within a if clause.
<% if(#index) %>
index <% index_files.each do |i| %> <%= i %> <% end %>;
<% end %>
Expected Result was:
index index.html index.php
or
""
Syntax error i got:
My flat approach failed as expected:
<% if(#index_files) %> try_files <% end %> <% index_files.each do |i| %> <%= i %> <% end %>
I defined index_files as undef => broke the each loop
I defined an empty array => since an empty array is defined it didn't work.
Maybe I can check the length of index_files?
Or do I need a complete different way to solve the problem?
I'm doing the same and it works for me, also for nginx ;).
For example:<% if #proxy_ignore_headers %> proxy_ignore_headers<% proxy_ignore_headers.each do |i| -%> <%= i %><% end -%>;
That works like a charm, the only difference with you is using () for the if condition, but I bet puppet supports (). It's weird, maybe you had pressed a bad combination generating a character that can't be seen but it's messing with your code, try writing all from scratch just in case.
You can see the full template here
Good luck
At first glance you just need to change
index_files.each
to
#index_files.each

Resources