Ask Your Question
1

ERB variables not getting through?

asked 2017-08-14 15:24:12 -0500

hanover23 gravatar image

updated 2017-08-14 15:27:00 -0500

Hey there -

I've written a practice chrony module to learn about ERB variables. I have three variables, one for each clock server I want to appear in /etc/chrony.conf. When I run my puppet agent, the default values I get blank space instead of my default variables. This could be basic, but would appreciate any help. I'm assigning these classes with Foreman. Thanks for any insights.

#This is what it looks like after a puppet run - no default values appear  
server  iburst 
server  iburst 
server  iburst

I have a client.pp file in my module that defines the following:

class chrony::client{
        package {'chrony':
                ensure => installed,
        }
        service { 'chronyd':
                ensure => running,
                enable => true,
                require => Package["chrony"],
        }
        file {'/etc/chrony.conf':
                notify => Service["chronyd"],
                ensure => file,
                require => Package["chrony"],
                content => template("chrony/chrony.conf.erb"),
        }
}

The relevant part of the chrony.conf.erb file is:

server <%= @first_timesource %> iburst
server <%= @second_timesource %> iburst
server <%= @third_timesource %> iburst

And in my init.pp file I have this:

class chrony (
        $first_timesource = "0.rhel.pool.ntp.org",
        $second_timesource = "1.rhel.pool.ntp.org",
        $third_timesource = "2.rhel.pool.ntp.org",
        ){
        include chrony::client
        include chrony::disable_ntpd
}
edit retag flag offensive close merge delete

Comments

Yeah, it's a basic mistake, and we all, at least I, made that mistake. You have to re-read the section about scope limits and inheritance. You'll see that for instance scope.lookupvar('chrony::first_timesource') results in the expected statement. You may use EPP (embedded Puppet) templates instead.

Kai Burghardt gravatar imageKai Burghardt ( 2017-08-14 19:47:16 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-08-15 16:09:39 -0500

Emerson Prado gravatar image

The ERB template receives the variables from the calling class. You defined the variables in class chrony, but called the template from class chrony::client. A decent solution would be to pass these variables from chrony to chrony::client via parameters. Alternatives would be to create the file in chronyor parameterize directly in chrony::client, but would be not so elegant.

edit flag offensive delete link more
0

answered 2017-08-16 00:17:42 -0500

ramindk gravatar image

You can also just reference the vars in another scope directly.

chrony.conf.erb

server <%= scope['chrony::first_timesource'] %> iburst
server <%= scope['chrony::fsecond_timesource'] %> iburst
server <%= scope['chrony::fthird_timesource'] %> iburst
edit flag offensive delete link more

Comments

You can, but this would reduce readability - the template is declared in one class, but its variables are defined in a different one. Not an absolutely prohibited pattern, but I personally prefer to keep scope as straight as possible.

Emerson Prado gravatar imageEmerson Prado ( 2017-08-16 13:56:38 -0500 )edit

To me it's less readable to be shuffling variables around when I can define them in one place and refer to them directly. I also prefer that data comes in via one class when possible within a module so I'm not searching for data lookups when troubleshooting. ymmv.

ramindk gravatar imageramindk ( 2017-08-16 15:28:13 -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-08-14 15:24:12 -0500

Seen: 177 times

Last updated: Aug 16