How to set system hostname to match Foreman hostname

I am using Foreman 2.1.1 and am creating CentOS installations from scratch (VM and bare metal).
When I create a new host from the GUI the system name does not match the Foreman host name.
For example:

I can not setup DNS records ahead of time and would like to set the hostname in kickstart.
I need an automated method which will make sure that the Foreman and system hostname is the same.
I have seen the solution of creating a script and including it in the kickstart file - I don’t see an easy way of automating that. I am looking for a way to get Foreman to pass the hostname to the kickstart file which would then set the hostname in the “network” command.

This differs on platform, however including fix_hosts snippet, that’s shipped by default, should help. You could also take an inspiration from this PR that was sadly never merged. However that’s IMHO the best solution (I’m biased :slight_smile:

1 Like

Thanks for the prompt response.
Do I cut and paste the appropriate pieces or can I call the snippet (and if so how do you do that)?

To use the existing snippet, add <%= snippet 'fix_hosts' %> to your kickstart %post section.

If you want to go with the second option, take just the set_hostname snippet, create the new template with such name and content from the PR and mark it as a snippet. Then do the same as mentioned in the first option, only chamge fix_hosts to the name of newly created snippet.

I tried adding the following to the kickstart file as well as in the provisioning template and still am not setting the hostname as expected. What do oyu think I’m doing wrong?

Provisioning:
<%= snippet ‘fix_hosts’ %>

kickstart file:
%post
echo “<%= @host.shortname %>” >> /root/host-name
echo “<%= @host %>” >> /root/host-name
/usr/bin/hostnamectl set-hostname <%= @host %>
<%= snippet ‘fix_hosts’ %>
echo “after snippet” >> /root/host-name
%end

Where can I see all parameters that are available in the kickstart environment - I think I am just missing some basic information.

[root@localhost ~]# cat /root/host-name
<%= @host.shortname %>
<%= @host %>

That is weird, since you see the <%= tags in the /root/host-name file, it seems the template didn’t get rendered at all. These tags should be replaced by the resulting value. E.g. <%= 1 + 1 %> should result in 2 in the rendered template. If you go to template form and click preview, do you see the tags or values, e.g. the hostname? If you see tags, make sure to type symbols yourself, instead of copying from the browser.

The documentation of all variables and functions was added in Foreman 2.3 and you would find the link on about page, but you have older version.

Below screen shot is from the provisioning template yet the file created on the server shows:

cat host-name

<%= @host.shortname %>
<%= @host %>

Oh wait, it seems you updated the wrong template. Don’t modify the Kickstart default PXEGrub2 CentOS 8.2 but Kickstart default (or how you called that). The template of Provisioning template kind. Find the line %post --log=/mnt/sysimage/root/install.post.log and put the snippet somewhere after it.

The template you editted is for the booting over the network, you need to set the hostname during the OS installation. It does not explain why you see <%= @host.shortname %> in the file though.