Problem:
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:
3.4
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 <%= @host.name %>
/usr/bin/hostnamectl --pretty set-hostname <%= @host.name %>
/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.