Ask Your Question
0

How to use external facts in ERB templates?

asked 2017-01-25 05:31:47 -0500

MacUsers gravatar image

Dear all, I'm having this issue and running out of ideas. I have this two external facts (defined in /etc/facter/facts.d/extngitfacts.txt):

 root@pmaster-001:~# facter|grep ks_git
 ks_git_puppet-hiera => master                         
 ks_git_puppet-master => develop

I want to use these two facts in a template. Here is my pseudo code:

<%-
    git_repos = ['puppet-hiera', 'puppet-master']
    git_repos.each do |g_repo|
        git_branch = "ks_git_#{g_repo}"
        git_br_name = Facter.value("ks_git_#{g_repo}")
-%>
GIT_BRANCH      :: <%= git_branch %>
GIT_BRANCH_NAME :: <%= git_br_name %>
<%-
    end
-%>

and I don't get anything returned by git_br_name at all. If I use the same code in irb console, it works just fine:

irb(main):012:0> require 'facter'
=> true
irb(main):013:0> git_repos = ['puppet-hiera', 'puppet-master']
=> ["puppet-hiera", "puppet-master"]
irb(main):014:0> git_repos.each do |g_repo|
irb(main):015:1* p Facter.value("ks_git_#{g_repo}")
irb(main):016:1> end
"master"
"develop"
=> ["puppet-hiera", "puppet-master"]

What am I doing wrong in the template? if I use some other built-in facts instead, e.g. Facter.value(:hostname) - it works just okay. Does mean the external facts cannot be used in template? Thanks in advance.

Best, Santanu

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-02-09 07:02:39 -0500

puser gravatar image

updated 2017-02-09 07:28:28 -0500

The alternative to this is using an epp template instead of an erb team. With epp templates you can pass in parameters, i.e. your custom facts for example:

<%- | Boolean $isTrue,
      String $someString
| -%>

If this is <%= $isTrue -%>, then have <%= $someString %>
Also you could have some <%= $::osfamily %>
edit flag offensive delete link more

Comments

Another roundabout way would be to assign your external fact to a variable in scope, and then use that variable in your template. Making note of the epp templates, I love that you can make parameterized templates!

DarylW gravatar imageDarylW ( 2017-02-09 22:18:38 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-01-25 05:30:09 -0500

Seen: 77 times

Last updated: Feb 09