ERB template not picking up variables from hieradata - erb

I have a puppet infrastructure and I've created a new module which refers to variables that exist in the hieradata yaml files, that all works fine in the manifest section. However, when I'm referring to them in an erb template the variables display nothing after the puppetrun. The file is there, just not the variables..
init.pp
class cms_nxlog ($msi_file) {
anchor { 'cms_nxlog::begin': }
->
file { "C:/CMS/${cms_nxlog::msi_file}":
ensure => 'file',
source => "puppet:///modules/cms_nxlog/${cms_nxlog::msi_file}",
owner => 'Administrators',
group => 'Users',
mode => '0770'
}
->
package { 'NXLOG-CE':
ensure => installed ,
source => "C:\\CMS\\${cms_nxlog::msi_file}",
}
->
file { "C:/Program Files (x86)/nxlog/conf/nxlog.conf":
ensure => 'file',
content => template('cms_nxlog/nxlog.conf.erb'),
owner => 'Administrators',
group => 'Users',
mode => '0770',
notify => Service['nxlog'],
}
->
service { 'nxlog' :
ensure => 'running',
require => Package['NXLOG-CE']
}
->
anchor { 'cms_nxlog::end': }
}
Relevant section of the erb template:
<Output out>
Module om_udp
Host <%= scope.lookupvar('cms::log_server') %>
Port <%= scope.lookupvar('cms_nxlog::port') %>
</Output>
Relevant sections of the yaml
cms_nxlog::msi_file: nxlog-ce-2.8.1248.msi
cms_nxlog::port: 514
cms::log_server: 192.168.1.50
The whole thing installs fine, it's just when copying the erb it seems to not fill in the content of the scope.lookupvar so I end up with
<Output out>
Module om_udp
Host
Port
</Output>
As I said previously the variables seem to work ok in the manifest, just not in the template. I've compared this to a similar module which seems to work to no avail.
Thanks

You need to call scope.function_hiera, not scope.lookupvar:
<Output out>
Moudule om_udp
Host <%= scope.function_hiera(['cms::log_server']) %>
Port <%= scope.function_hiera(['cms_nxlog::port']) %>
</Output>
Take note that function_hiera takes an array as it's argument, not a string.
Hope this helps!

in puppet 6 this works:
<%= scope().call_function('lookup', ['cms_nxlog::port']) %>
where 'cms_nxlog::port' is a string variable in hiera

Related

puppet 3.8.x variable can't apply to template

module/bareos_backup_client/manifests/init.pp:
class bareos_backup_client {
##file { "${fqdn}-bareos-client.conf":
mode => 600,
owner => bareos,
group => bareos,
path => "/etc/bareos/director.d/${fqdn}-client.conf",
content => template("bareos_backup_client/bareos-dir-cliententry.erb"),
tag => 'bareos-client',
notify => Service[bareos-dir],
}
}
module/bareos_backup_client/templates/bareos-dir-cliententry.erb:
<% if #clientrunbeforejob -%>
ClientRunBeforeJob = "<%= #clientrunbeforejob %>"
<% end -%>
<% if #clientrunafterjob -%>
ClientRunAfterJob = "<%= #clientrunafterjob %>"
<% end -%>
manifests/nodes/server_1.pp:
include bareos_backup_client
$clientrunbeforejob = "apple"
Why clientrunbeforejob variable can't inject into erb template?
You try and use dynamic scoping. This has not worked in years, and for good reason!
You will likely want to pass this value as a class parameter.
class bareos_backup_client($clientrunbeforejob) {
...
}
Then declare it like
class { 'bareos_backup_client':
clientrunbeforejob => 'apple'
}

To judge whether a variable in the ejs

Here's my application:
index.js
function index(req, res) {
res.render('admin/index');
}
module.exports = index;
index.ejs
<%
if(data) {
%>
<div class="alert alert-danger" role="alert">login fail</div>
<%
}
%>
I got an error saying:
data is not defined
I want to check whether the variable exists, and to display the dialog if it does. What should I do?
Either rewrite the check as follows:
<% if (typeof data !== 'undefined') { %>
... or check the property on locals (local variables object) instead:
<% if (locals.data) { %>
Explanation: when EJS compiles a template into a function, it does not populate its variables' stack based on the options supplied. Instead, it wraps this function with with statement:
with (locals || {}) {
(function() {
// ... here goes the template content
})();
}
Now, the data object (second parameter of render) is passed into the template function as locals variable, it's against this object all the checks are made. The point is, if accessed somevar is never defined in the local template scope (by var statement), and doesn't exist in locals object either, it'll cause a ReferenceError: somevar is not defined error.
(one can disable with-wrapping, setting _with option of template to false, but by default it's just undefined)

puppet iteration with 2 dimensional array to fill template

I want to store ip address, port values in a two dimensional array.
192.168.1.156, 4100
192.168.1.157, 4000
Using the defined array, I want to fill this following template structure.
<members>
<%- if #members -%>
<%- #members.each_pair do |hostname,port| -%>
<member>
<hostName><%= hostname %></hostName>
<port><%= port %></port>
</member>
<%- end -%>
<%- end -%>
</members>
How do I construct the two dimensional array in my setup.pp or init.pp file?
It would make the most sense for you to declare your data in hash form. In Hiera, you can use YAML
members:
192.168.1.156: 4100
192.168.1.157: 4000
or JSON
{ members: { '192.168.1.156': '4100', '192.168.1.157': '4000' } }
If you don't want to use Hiera, you can declare the data right in the manifest.
$members = { '192.168.1.156' => '4100',
'192.168.1.157' => '4000' }

Display MediaLibraryPickerField when editting a part

I'm missing something here and can't for the life of me figure out what.
I've added a MediaLibraryPickerField to a part I created:
ContentDefinitionManager.AlterPartDefinition(typeof (FloorPlanPart).Name, cfg => cfg
.WithField("Photo", f => f
.OfType("MediaLibraryPickerField")
.WithDisplayName("Photo")
.WithSetting("MediaLibraryPickerFieldSettings.Required", "true")));
I can verify that this field has been added correctly to my part. This part belongs to a custom type:
ContentDefinitionManager.AlterTypeDefinition("FloorPlan", cfg => cfg
.WithPart(typeof(FloorPlanPart).Name)
.Creatable()
.Draftable(false));
I have my driver setup to return the following on an edit:
protected override DriverResult Editor(FloorPlanPart part, dynamic shapeHelper)
{
return ContentShape("Parts_FloorPlan_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/FloorPlan",
Model: part,
Prefix: Prefix));
}
my edit view looks like the following:
#using System.Web.Mvc.Html
#model Models.FloorPlanPart
<fieldset>
<legend>Floor Plan Fields</legend>
<div class="editor-label">#Html.LabelFor(x => x.FloorPlanName)</div>
<div class="editor-field">
#Html.EditorFor(x => x.FloorPlanName)
#Html.ValidationMessageFor(x => x.FloorPlanName)
</div>
</fieldset>
Some where along the line the Media picker is supposed to show up but never does when creating a new Floor Plan. All I get is just the fields in my part (FloorPlanName). How do I get the media picker to show? If I add the Media picker as a field on my content type it does show but shouldn't I be able to do it this way as well?
Rookie mistake, I thought I was using the latest version of Orchard 1.7.2 but was using 1.6.2.

drupal 6 -----why theme() can't output

the code i put in the mytheme template.php
function mytheme_theme(){
return array(
'mytheme_example' => 'example',
'argument' => array('myvar' => null),
);
}
the code i put in the node.tpl.php
<?php
$html = "";
$myvar = "hello,world";
$html .= theme('mytheme_example', myvar);
return $html;
?>
the code i put into the example.tpl.php
<div>
here is the <b><?php print myvar; ?></b>being created.
</div>
i have cleared the cache,but on the node article's page, there is no any output about hello world.
ps:which files i can use the hook_theme, template.php, module file. are there any files i can use this hook?
It looks like you have declared your hook_theme correctly in template.php so I do not think this is the issue.
I did spot a syntax issue with your node.tpl.php, should it not be:
<?php
$vars = array('myvar' => 'hello, world');
$html = theme('mytheme_example', $vars);
return $html;
?>
Note the associate array, with the 'myvar' (the variable declared in hook_theme), is being passed in as the key.
Another point, it is standard practice to name the template file the same as the hook name, so I would suggest calling the template mytheme-example.tpl.php.
See drupal.org for more information
I don't know if you have solved that issue yet.
I would try to declare my theme this way:
function mytheme_theme(){
return array(
'mytheme_example' => array(
'arguments' => array('arguments'=>array()),
'template' => 'example',
),
}
That's how I usually do and it works fine on me.

Resources