Ask Your Question

Puppet pass a fact into a double-quoted line of code in an ERB file

asked 2016-04-19 06:12:49 -0600

Arianrhod gravatar image

I'm sure the answer to this is straightforward, but I can't find out how to do this. I have a line in my erb template file that needs to write exactly as-is into a server's config file (for OpenVPN):

push "dhcp-option DNS"

I need to change this so that the ip address gets looked up from the facter listing on the server, rather than having it hard-coded, as this conf file needs to be used on different servers now. The fact holding this ip address is "ipaddress_tun0". I've tried putting it into my line, thus:

push "dhcp-option DNS <%= @ipaddress_tun0 %>"

.. but the fact doesn't get interpolated properly, presumably because of the double quotes. Unfortunately the double quotes are needed as a literal part of the line in the conf file, so they have to be there (single quotes don't work). Can anyone advise how I can get this line to look up and insert the value of ipaddress_tun0, please?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2016-04-20 05:04:46 -0600

Fyi I just tried this from a VM:

[root@centos-72-x64 ~]# cat /tmp/foo.pp 
$content = inline_template('push "dhcp-option DNS <%= @ipaddress_enp0s8 %>"')


[root@centos-72-x64 ~]# puppet apply /tmp/foo.pp 
Notice: Scope(Class[main]): push "dhcp-option DNS"
Notice: Compiled catalog for centos-72-x64.home in environment production in 0.02 seconds
Notice: Finished catalog run in 0.01 seconds

I was using Puppet 3.8.6.

edit flag offensive delete link more

answered 2016-04-20 04:30:59 -0600

Arianrhod gravatar image
push "dhcp-option DNS <%= scope.lookupvar('ipaddress_tun0') %>"

was needed in the end, as the fact "ipaddress_tun0" was outside of the current scope.

edit flag offensive delete link more

answered 2016-04-19 11:09:48 -0600

awippler gravatar image

push "dhcp-option DNS <%= @ipaddress_tun0 -%>" should be the line you need to use.

basically your configuration prints as:

push "dhcp-option DNS ipaddress_tun0


This is because your erb snippet includes a line break. To suppress this line break, you need to include the - in the closing bracket.

edit flag offensive delete link more


Hmm Interesting, thanks. However, this doesn't work either, but a colleague suggested I need to use scope.lookupvar, and that does work, as apparently my fact "ipaddress_tun0" is out of the current scope.

Arianrhod gravatar imageArianrhod ( 2016-04-20 04:30:06 -0600 )edit

That doesn't make sense, because facts are global.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-20 04:39:58 -0600 )edit

It must be an old agent version to require the `scope.lookupvar`.

awippler gravatar imageawippler ( 2016-04-20 15:08:33 -0600 )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


Asked: 2016-04-19 06:12:49 -0600

Seen: 94 times

Last updated: Apr 20 '16