Need help with some ruby erb code in a snippet

If else statement doesn’t match my variable host.hostgroup. Always goes to the else statement.
Expected outcome:
Match hostgroup and use the correct block of bash commands.

Foreman and Proxy versions:
Foreman and Proxy plugin versions:

Distribution and version:
Redhat 8.7

Other relevant data:

kind: snippet
name: mfe_set_hostname
model: ProvisioningTemplate
snippet: true
description: |
Set static hostname and pretty hostname.

<% if @host.hostgroup =~ /Render/ -%>
/usr/bin/hostnamectl set-hostname <%= @host.shortname %>
/usr/bin/hostnamectl --pretty set-hostname <%= @host.shortname %>
/usr/bin/cp /etc/hostname /mnt/sysimage/etc/hostname
/usr/bin/cp /etc/machine-info /mnt/sysimage/etc/machine-info
<% else -%>
/usr/bin/hostnamectl set-hostname <%= %>
/usr/bin/hostnamectl --pretty set-hostname <%= %>
/usr/bin/cp /etc/hostname /mnt/sysimage/etc/hostname
/usr/bin/cp /etc/machine-info /mnt/sysimage/etc/machine-info
<% end -%>

So this snippet works great for setting a hostname at the end of provisioning. I forgot that one team likes a certain group of render nodes to be only the shortname, because that’s how they have management set up. I used <%= @host.hostgroup %> to verify in preview that it’s outputting “Rocky Render”. Yet it never matches the first block and goes to the 2nd block always. I put an echo statement to verify and it’s only the second block is being selected after the else statement. I also got rid of the if else statements and used the @host.shortname and indeed it only uses the shortname without our domain name. I also switched to machines in different hostgroups and their hostgroups returned Workstations from the variable.

The weird thing is it briefly started working and I copied the exact code to puppet and pushed it out and overwrote the file and it went back to not matching. Unless I’m missing something completely obvious, it feels like a weird bug.

Can anyone help please. I’m no ruby erb guru but I do plenty of templates in puppet and this is about as straight forward as it gets. I’ve never had a problem with =~ /matchstring/ before ever in puppet.

I think, what’s happening here is that @host.hostgroup does not actually return the hostgroup’s name, but rather a hostgroup object. If you print something, most objects have some implicit to_s method called afaik which is why you did see the name when you just output it through the template. So you are comparing a hostgroup object to a RegEx string, which of course does not match.
I remember stumbling over a similar problem recently that nearly drove me crazy as well. Try calling either .name or .title (depending what matches your use-case better) on the hostgroup object (i.e. and compare that to the /Render/ RegEx.

Hope this helps :slight_smile:

P.S.: I recently learned that there is a comprehensive template doc that comes with your Foreman installation. You can find it at, which always matches your current version and should contain everything any of your plugins provide.

1 Like

Thank you! That was it. It WAS driving me crazy like it did you. What should have taken 2 minutes I spent 2 hours on :joy: :sob:

I’ll book mark that link and study it. I’m taking Ruby courses online at Stormwind’s site and have a couple of books I bought. Slowly improving. I’ve neglected python and Ruby for years and felt it was time to level up as a Linux Systems Admin.

Again thank you very much. You are a life saver.

1 Like