Use custom facts in erb templates

1,195 views
Skip to first unread message

Vikas Kumar

unread,
Oct 11, 2015, 1:37:26 PM10/11/15
to Puppet Users
Hello Everyone,

I have a YAML file which I am using to generate facts for one of my puppet agents.

# facter -p box32.interfaces_ipv4.eth0.bootproto
static
#



This works fine, but I am not sure how to make this work in erb templates. I've tried few options in vain.

Forgive my ignorance on this. Thanks.

Regards,
Vikas

Russell Cecala

unread,
Oct 11, 2015, 8:19:15 PM10/11/15
to puppet-users

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/1a0698c9-b269-4381-aeae-ee4cd7c524e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vikas Kumar

unread,
Oct 11, 2015, 8:54:35 PM10/11/15
to Puppet Users
Hi Red,

I did tried that, but it is not working for me.

<%= hostname[interfaces_ipv4][eth0][bootproto] %>

or even
<%= os[release][major] %>
Regards,
Vikas

Russell Miller

unread,
Oct 11, 2015, 9:23:24 PM10/11/15
to Puppet Users
Don't forget the "@".  Ruby scoping rules still apply.

<%= @hostname["interfaces_ipv4"]["eth0"]["bootproto"] %> should work, though I haven't tested it, obviously.

Also don't forget that puppet scoping rules ALSO apply, so the variable has to be accessible from the class from which the templating is done.  You could do (I think) lookup_scopevar or some such to get around that.

--Russell

Russell Cecala

unread,
Oct 11, 2015, 9:43:46 PM10/11/15
to puppet-users
What's up with []'s? That doesn't look right to me. Just put your fact.


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.

Russell Cecala

unread,
Oct 11, 2015, 9:54:21 PM10/11/15
to puppet-users
See Russell Miller's email.  :)

Vikas Kumar

unread,
Oct 11, 2015, 10:06:11 PM10/11/15
to Puppet Users
Hi Russel,

I tried to use reference the variable as below, but got an error.
<%= @hostname["interfaces_ipv4"]["eth0"]["bootproto"] %>


Error
Oct 12 13:04:06 box32 puppet-agent[37813]: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Function Call, Failed to parse template network/ifcfg-ethX.erb:
Oct 12 13:04:06 box32 puppet-agent[37813]: Filepath: /etc/puppetlabs/code/environments/production/modules/network/templates/ifcfg-ethX.erb
Oct 12 13:04:06 box32 puppet-agent[37813]: Line: 4
Oct 12 13:04:06 box32 puppet-agent[37813]: Detail: undefined method `[]' for nil:NilClass

Regards,
Vikas

Russell Miller

unread,
Oct 11, 2015, 10:12:16 PM10/11/15
to Puppet Users

On Sunday, October 11, 2015 at 7:06:11 PM UTC-7, Vikas Kumar wrote:
Hi Russel,

I tried to use reference the variable as below, but got an error.
 
Vikas, that means either it's not a hash or a requested hash element doesn't exist.  I don't have a puppet instance in front of me so I'm not 100% sure of this, but iirc, hostname is a default fact that is a standard scalar variable.  I'd check to see exactly what it's returning.  If it's a fact you built yourself, then you might be dealing with a namespace conflict.

The important thing to note here:  All ruby variables imported from puppet are preceded by "@" (I know of no exceptions though there may be) and the puppet scoping is also important.  I'm very confident that once you know what your variable looks like, you'll be able to access it through a ruby template.

--Russell
 

Vikas Kumar

unread,
Oct 11, 2015, 10:29:43 PM10/11/15
to Puppet Users
Russel, 

I can see the hash on puppet master but not on agent. Can this be the reason ? Importantly, when I try to create a file for custom facts on agent, it gets deleted in the subsequent puppet run. 

SERVER
# facter --puppet box32.interfaces_ipv4.eth0.bootproto
static
#

AGENT
# facter --puppet box32.interfaces_ipv4.eth0.bootproto

#


-Vikas

Russell Miller

unread,
Oct 11, 2015, 10:41:01 PM10/11/15
to Puppet Users


On Sunday, October 11, 2015 at 7:29:43 PM UTC-7, Vikas Kumar wrote:
Russel, 

I can see the hash on puppet master but not on agent. Can this be the reason ? Importantly, when I try to create a file for custom facts on agent, it gets deleted in the subsequent puppet run. 

Yes, the facts are deployed from modules onto the agent - trying to create the fact on the agent is a Bad Idea and completely flies in the face of the purpose of puppet.  And yes, that absolutely could do it, as the facts are first deployed then run.  Make yourself a module and deploy it through there and it should stick around.

That also leads to a few interesting file naming conflicts when it comes to different modules, but that's a topic for another day.

--Russell
 
Reply all
Reply to author
Forward
0 new messages