2

I have installed Foreman in a test VM and I want to create some custom kickstart files. I understand that Foreman renders the templates during the host creation. But in case of an error, type, variable that is missing etc, the installation fails.

So, how can I test my templates without actually trying to install a new VM?

Obviously, it's counterproductive to start a new build every time I want to make changes in my templates or snippets.

Peter
  • 802
  • 2
  • 10
  • 23

2 Answers2

4

Above the template editor is a small Preview button - select that, and from the dropdown menu on the right select the host that you wish to render the template for.

A managed host with the same operating system will be needed, so it must be created before previewing the template (set up for provisioning through New Host, not created through Puppet or fact uploads.)

The rendered version of the template will be shown like so:

Edit provisioning template with preview

Select the Input tab again and you can return to editing the template. This all happens without needing to save + re-visit the edit page. No changes are persisted when you preview the rendered template, only when you save.

Also see Foreman 1.9 release highlight - template previews for a short screencast showing the feature.

Dominic Cleal
  • 3,120
  • 17
  • 16
  • In my case it's not working. – Peter Oct 24 '16 at 08:38
  • It responds with ""Warning! No host could be found for rendering the template" It fails even for the default templates. I will try the erb. In any case, I appreciate your effort to answer this with screenshots! – Peter Oct 24 '16 at 08:56
  • Are any hosts listed where foreman.example.com is above? You will need at least one host to exist that's "managed" (i.e. set up for provisioning) - managed hosts have the Operating system tab visible when you edit them, else click the Manage host button to change it. – Dominic Cleal Oct 24 '16 at 10:33
  • That was the problem. I wanted to test the kickstart before the provisioning, but I couldn't. Now it's OK. You should right it as an answer :) – Peter Oct 26 '16 at 21:54
  • I've added it to the answer, glad it works. – Dominic Cleal Oct 27 '16 at 07:37
1

To check the syntax is correct

erb -P -x -T '-' template.erb | ruby -c

To see what is written from a template is more complicated but you can look to complete this ruby script

require 'erb'
require 'ostruct'

class ErbalT < OpenStruct
  def render(template)
    ERB.new(template).result(binding)
  end
end

et = ErbalT.new({ :kernel => '4.8.4', 'os' => 'Linux' })
puts et.render(File.read(PATH_TO_TEMPLATE)) 

Depending to what template you want to test you need to send as parameters all the variables that are expected.

There is one answer more specific to erb templates as well - see https://stackoverflow.com/questions/8954706/render-an-erb-template-with-values-from-a-hash

silviud
  • 2,677
  • 2
  • 16
  • 19