Ask Your Question
0

How can I use my own variable in a template ERB file? Thank you so much.

asked 2018-04-07 16:58:06 -0500

stevenabb gravatar image

updated 2018-04-09 13:38:09 -0500

DarylW gravatar image

For some reason, I am having a tough time trying to use/create my own variable in a template. Can someone please write an ultra simple init.pp and template/steven.erb file to add a variable. Here is what I am using, and it is not working:

~ The contents of the init.pp file is this ~

class transport:::config {
$myvar = 'puppet is hard to learn'
}

~ The config file is this ~ config.pp

file { "/tmp/steven.conf":
    ensure => "present",
    content => template("transport/steven.erb")
}

~ And in the transport/templates directory file steven.erb contents ~

# file managed by puppet
    Put in My variable here <%= @myvar %>

Yes... I have been reading documentation and Puppet style guides a lot, trying to learn this and using " puppet parser validate ", so thank you! Can someone just respond back with a very very simple init.pp , config.pp and steven.erb file that will take in a custom variable, and put that variable into the file /tmp/steven.conf . Thank you very much.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-04-09 16:08:37 -0500

stevenabb gravatar image

Thank you so much! Yea Puppet is a tough thing to learn for the very first time. I think??? I found the answer after a lot of trial and error. I will post the answer when I find the time... super swamped. Hey thank you for your response.

edit flag offensive delete link more
0

answered 2018-04-09 14:16:20 -0500

DarylW gravatar image

There are some helpful patterns that are out there, I would recommend starting with puppet's module guide. https://puppet.com/docs/puppet/5.5/bg... .. I would also recommend you get started with the pdk and have that generate your initial module skeleton. ( https://puppet.com/docs/pdk/1.x/pdk.html )

It looks like you may have your file structure set up incorrectly. From your above, It makes me think that you have the following

transport/init.pp

class transport:::config {
  $myvar = 'puppet is hard to learn'
}

transport/config.pp

file { "/tmp/steven.conf":
    ensure => "present",
    content => template("transport/steven.erb")
}

transport/templates/stephen.erb

# file managed by puppet
    Put in My variable here <%= @myvar %>

What you SHOULD have would be something like the following transport/init.pp

class transport (   #note, this is named 'transport', not 'transport::config' since this isn't in the file config.pp
  $myvar = 'puppet is hard to learn'  # This is brought out to a class parameter, instead of a variable inside of the class
) {
  # ...
}

transport/config.pp

class transport::config (  # Note, that by convention, a file name needs to match a modulename::filename pattern, except for init.pp which the class is just the name of the module.

    $myvar = $transport::myvar # This brings the 'myvar' param from init.pp into scope in the transport::config class
) {
    file { "/tmp/steven.conf":
        ensure => "present",
        content => template("transport/steven.erb")  # This template should have 'myvar' in scope, since we brought it in as a parameter.
    }
}

transport/templates/stephen.erb

# file managed by puppet
    Put in My variable here <%= @myvar %>

I haven't tried putting this in and evaluating it, but it should help you get part of the way. I would highly recommend getting started with the pdk, and doing a pdk new module command

There are also some good resources from puppetconf presentations, like this one ( https://www.slideshare.net/DavidDanzi... - slide 30, the package, file, service pattern )

edit flag offensive delete link more

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: 2018-04-07 15:56:31 -0500

Seen: 155 times

Last updated: Apr 09