I'm trying to dynamic change create a nginx xonfig file with chef using an erb. What is the correct syntax for if else it an .erb file.
The below code gives me an error
(erubis):17: syntax error, unexpected tINTEGER, expecting keyword_end
<% node['dd']['pipeline']['env'] -%>
resolver <%if node['dd']['pipeline']['env'] == "production" then 10.100.0.5 else 10.0.0.5 end -%> valid=30s;
Because 10.100.0.5 isn't a valid Ruby literal. You want this:
<%= if node['dd']['pipeline']['env'] == "production" then "10.100.0.5" else "10.0.0.5" end %>
Related
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 %>
I need to evaluate an ERB template, and then ensure it's a valid NGINX configuration file, but vanilla ERB doesn't allow the -%> directive. How am I able to add that extension into my rakefile?
I've been able to replicate the problem in irb as so:
~ $ irb
irb(main):001:0> require 'erb'
=> true
irb(main):002:0> var = "yeah"
=> "yeah"
irb(main):003:0> ERB.new(" <% if var == 'yeh' -%>
irb(main):004:1" something
irb(main):005:1" <% else -%>
irb(main):006:1" something else
irb(main):007:1" <% end -%>
irb(main):008:1" ").result binding #"
SyntaxError: (erb):1: syntax error, unexpected ';'
...concat " "; if var == 'yeh' -; _erbout.concat "\nsomething\...
... ^
(erb):3: syntax error, unexpected keyword_else, expecting $end
; else -; _erbout.concat "\nsomething else\n"
^
from /usr/lib/ruby/1.9.1/erb.rb:838:in `eval'
from /usr/lib/ruby/1.9.1/erb.rb:838:in `result'
from (irb):3
from /usr/bin/irb:12:in `<main>'
In order to use the -%> syntax in ERB you need to set the trim mode option to '-'. This is the third option to the constructor, you will need to pass nil as the second (unless you want to change the safe_level from the default):
ERB.new(" <% if var == 'yeh' %>
something
<% else %>
something else
<% end %>
", nil, '-').result binding
Without this option the - is included in the generated Ruby script and gives you the syntax error when you try to run it.
Note that there is another eRuby processor, Erubis which might have slightly different options (it can still use this syntax). This one is used by Rails. Check out the docs for more info.
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
I have the below code in a chef template but get error when uploading to chef server. How do I resolve?
<%
contents_hash = File.read('/tmp/cluster_hash')
neoservers_hash = JSON.parse(contents_hash)
-%>
<% "#{neoservers_hash}".each_pair do |id, ipaddress| %>
<%= "server.#{id}=#{ipaddress}:2888:3888" %>
<% end %>
When I try to upload the cookbook, I get the following error:
$ knife cookbook upload neo4j -E development
Uploading neo4j [0.1.0]
FATAL: Erb template templates/default/coord.cfg.erb has a syntax error:
FATAL: -:7: syntax error, unexpected tIDENTIFIER, expecting '}'
FATAL: _buf << ( "server.#{id}=#{ipaddress}:2888:3888" ).to_s; _buf << '
FATAL: ^
FATAL: -:8: unterminated string meets end of file
You have a weird syntax in this line:
<% "#{neoservers_hash".each_pair do |id, ipaddress| %>
You seem to try to use string evaluation with the neoserver_hash variable, which will not really work, as neoserver_hash is a Hash and not a String. Also you are missing the closing brace. Instead, you probably want to get rid of the string evaluation completely and use something like this:
<% neoservers_hash.each_pair do |id, ipaddress| %>
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