I've an ERB template in YAML file which is parsed successfully and looks like so
---
name: message_from_json_to_raw
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract'
end %>
<%= output %>
I want the output variable to contain | extract
at the end irrespective of the if
block execution. So I tried the following approaches
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n")'
end %>
<%= output.concat(" | extract ") %>
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract '
else
output << ' | extract '
end %>
<%= output %>
config:
definition: <%
output = ' | extract '
if "splunk_index".eql? env_index
output = ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n")'
end %>
<%= output %>
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n")'
end %>
<% output.concat(' | extract ') %>
<%= output %>
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n")'
end
output << ' | extract ' %>
<%= output %>
All the above approaches given a single error which looks as follows
/var/lib/spork/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse': (<unknown>): did not find expected comment or line break while scanning a block scalar at line 5 column 6 (Psych::SyntaxError)
from /var/lib/spork/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse_stream'
from /var/lib/spork/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:325:in `parse'
from /var/lib/spork/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:291:in `safe_load'
Any suggestion on what's causing this and a possible fix? This looks similar to the issue here but the suggestions don't look related to this situation.
Interestingly, all of the things I tried work perfectly fine when I use the Ruby REPL here. For instance the below one
require 'erb'
ibus_index = 'ibus_cloud'
template = ERB.new <<-EOF
<%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n")'
end %>
<% output.concat(' | extract ') %>
<%= output %>
EOF
puts template.result(binding)
CodePudding user response:
It seems like the error is telling you the truth: you're missing a line break after your block scalar.
| extract
needs to be followed by a line break before the end of the file or the next key.
The reason that the heredoc example worked is because a heredoc is always terminated by a linebreak unless you add chomp.
CodePudding user response:
Let's narrow it down to where the error actually is. Let's render your first example:
# config.yml.erb
---
name: message_from_json_to_raw
config:
definition: <%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract'
end %>
<%= output %>
Render erb first. No issues.
>> env_index = "splunk_index"
>> yaml = ERB.new(File.read('config.yml.erb')).result(binding)
>> puts yaml
---
name: message_from_json_to_raw
config:
definition:
| spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract
Now it's a plain yaml. This is what's being parsed by Psych
>> Psych.load(yaml)
/home/alex/.rbenv/versions/3.1.1/lib/ruby/3.1.0/psych.rb:455:in `parse': (<unknown>):
did not find expected comment or line break while scanning a block scalar
at line 5 column 6 (Psych::SyntaxError)
This is exact same error you're getting. Line 5 Column 6:
1---
2name: message_from_json_to_raw
3config:
4 definition:
5 |*spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract
123456
* Gotta have a new line there or have another properly defined block scalar.
Let's untangle the erb a bit to be clear where the output is:
<%
output = ''
if "splunk_index".eql? env_index
output << ' | spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract'
end
# build the output here
%>
---
name: message_from_json_to_raw
config:
definition: |-
<%= output %>
|-
now a block scalar has a new line, a dash is to remove some extra new lines at the end
It parses into this. I don't know if that's the output you're looking for, but, there is no error.
---
name: message_from_json_to_raw
config:
definition: '| spath output=_raw path=msg | eval _raw = split(_raw,"\n") | extract'