Migrating puppet from 3.x to 4x - puppet

I have existing puppet modules written in hiera. Basically, data module with param.pp pattern. Everything works fine in puppet 3.x. But after setup with puppet 4.x and ran puppet agent, following error return for hash keys.
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, Failed to parse template wso2ei/conf/carbon.xml.erb:
Filepath: /etc/puppetlabs/code/environments/production/modules/wso2ei/templates/conf/carbon.xml.erb
Line: 138
Detail: undefined method `[]' for nil:NilClass
at /etc/puppetlabs/code/environments/production/modules/wso2base/manifests/push_templates.pp:23:16 at /etc/puppetlabs/code/environments/production/modules/wso2base/manifests/configure.pp:59 on node puppet4-aa54a7f2-83a6-43d2-b8d7-c15a42591e88.openstacklocal
The particular section of carbon.xml.erb looks like below
<%- if #ports['offset'] -%>
<Offset><%= #ports['offset'] %></Offset>
<%- else -%>
<Offset>0</Offset>
<%- end -%>
hieradata set as below in yaml file
wso2::ports:
offset: 0
param.pp written to read the data
$ports = hiera('wso2::ports')
init.pp set value as below
$ports = $wso2ei::params::ports,
Template not giving any error for a single line data. For an example in carbon.xml.erb
<% if #hostname %>
<HostName><%= #hostname %></HostName>
<%- else -%>
<!--HostName>www.wso2.org</HostName-->
<% end %>
hieradata in yaml file
wso2::hostname: "%{::fqdn}"
All hash keys getting the same error. When I Google, found that # has to replace with scope like below. But there are lots of places to change.
<%- if scope['wso2ei::params::ports']['offset'] -%>
<Offset><%= scope['wso2ei::params::ports']['offset'] %></Offset>
<%- else -%>
<Offset>0</Offset>
<%- end -%>
Is there a way to preserve existing param.pp data module pattern in puppet 4? Appreciate your help.

Related

Unexpected token '/' in {FILE} while compiling ejs

I am working on this: https://www.youtube.com/watch?v=6FOq4cUdH8k
For some reason, the following line is causing the error in the title.
<% include ./partials/messages %>
Removing the line above solves the problem.
I have confirmed that it is not the messages file as there are no slashes in it.
<% if(typeof errors != 'undefined') { %>
<% errors.forEach(function(error){ %>
<%= error.msg %>
<% }); %>
<% } %>
as you can read in the official documentation
Includes are relative to the template with the include call. (This
requires the 'filename' option.) For example if you have
"./views/users.ejs" and "./views/user/show.ejs" you would use <%-
include('user/show'); %>.
You'll likely want to use the raw output tag (<%-) with your include
to avoid double-escaping the HTML output.
so instead of
<% include ./partials/messages %>
Write
<%- include ("./partials/messages") %>
Why?
Template tag <%- outputs the unescaped value into the template, whereas <% 'scriptlet' tag is used for control-flow, no output. The parenthesis and " " are used for filename location

SyntaxError: Unexpected identifier when using ejs in nodejs

I am following The Net Nijna's tutorial on youtube.
I reached tutorial number 27, working with partials in ejs. Everything works until I add the <% include partials/nav.js %>, once I add this code I recieve:
SyntaxError: Unexpected identifier in (file location) testapp\views\profile.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass async: true as an option.
at new Function ()..... blah blah blah...
If I remove it, my ejs all works fine.
<body>
<% include partials/nav.ejs %>
<h1>Welcome to the profile of <%= person %> !</h1>
<p><strong> Age: <%= data.age %></strong></p>
<p><strong> Job: <%= data.job %></strong></p>
<p><strong> Pet: <%= data.pet %></strong></p>
<h2>Hobbies</h2>
<ul>
<% data.hobbies.forEach(function(item){ %>
<li><%= item %></li>
<%});%>
</ul>
</body>
can you help a student out? Thanks a ton!
Missing hyphen and need to invoke the include function.
<%- include('partials/nav') %>
I was stuck at the same problem and used -include('#filename');
it worked
Use: <%- include('folder/file') %>
agreeing with #Joseph Varilla
Not necessary to include engine extension as this should have been done in app.js or index.js file

Deep hiera lookup in a ruby template returns last result only

I have the following hiera:
profile::example::firewalls:
- serverclass1:
label: http
port: 80
label: https
port: 443
- serverclass2:
label: ssh
port: 22
label: telnet
port: 21
I try to call it in the following way in an erb template:
<% #example = scope().call_function('hiera',['profile::example::firewalls']) -%>
show me the array!
<%= #example %>
Oddly this returns the following:
+show me the array!
+[{"serverclass1"=>nil, "label"=>"https", "port"=>443}, {"serverclass2"=>nil, "label"=>"telnet", "port"=>21}]
Once I have a complete array I'll eventually use this within a for/each loop within ruby, but for the moment this seems to return just the final result and not everything I'd expect.
The problem begins in your YAML data structure, that contains duplicate "label" keys. That is why some of your data is missing in the output.
(See this related Stack Overflow answer for more info.)
Although it is not entirely clear what you are trying to do, it seems to me that you would be better with a YAML data structure like this:
profile::example::firewalls:
serverclass1:
http: 80
https: 443
serverclass2:
ssh: 22
telnet: 21
You could then iterate through that in the ERB template using code like:
<% scope().call_function('lookup',['profile::example::firewalls']).each do |server_class, data| -%>
Server class: <%= server_class %>
<%- data.each do |key, value| -%>
<%= key %> --- <%= value %>
<%- end -%>
<% end -%>
Note also that I removed the deprecated hiera() function and replaced it with lookup().

Chef Template If Attribute Exists

I've got an optional attribute on my nodes. I want my template to only set a specific value if that attribute exists:
<% if node['haproxy']['server']['backup'] %>
server <%= node['haproxy']['server']['backup']['hostname'] %> <%= node['haproxy']['server']['backup']['ipaddress'] %>:<%= node['mysql']['port'] %> weight 1 maxconn 100 check
<% end %>
This looks good to me but when I run it I'm getting the following error:
Chef::Mixin::Template::TemplateError
------------------------------------
no implicit conversion of String into Integer
How can I get this working so Chef recognizes if the attribute is set?
Try
<% if node['haproxy']['server'].attribute?('backup') %>

puppet erb extra newline

I have a puppet template that is adding an extra newline on each iteration of the inner loop. The template is as follows:
;; THIS FILE IS MANAGED BY PUPPET
; <%= #comment %>
[production]
<%-
#data.sort.map do |provider,attributes|
#data[provider].sort.map do |key,value| -%>
<%= provider %>.<%= key %> = "<%= value %>"
<%- end
end -%>
The output is something along the lines of:
;; THIS FILE IS MANAGED BY PUPPET
; Some random config file
[production]
provider1.a="1"
provider1.a="2"
provider1.a="3"
provider2.a="4"
provider2.a="5"
provider2.a="6"
As far as I can tell, that template should be suppressing the additional newlines. Am I missing something?
try add '-' value in iteration.
"<%= value %>" => "<%= value -%>"
Change to:
;; THIS FILE IS MANAGED BY PUPPET
; <%= #comment %>
[production]
<%-
#data.sort.map do |provider,attributes|
#data[provider].sort.map do |key,value| -%>
<%= provider %>.<%= key %> = "<%= value -%>"
<%- end end -%>

Resources