Puppet setup cloudinit not starting service on CentOS

I managed to successfully provision CentOS 7.5 hosts from VMware templates on Foreman 1.19 using UserData and CloudInit plugin. However the stock puppet_setup_cloudinit provisioning template doesn’t seem to want to enable the puppet agent service after the cloudinit is ran. Previewing the output of the snippet shows the following.

if [ -f /usr/bin/dnf ]; then
  dnf -y install puppet-agent
else
  yum -t -y install puppet-agent
fi
/opt/puppetlabs/bin/puppet agent --config /etc/puppetlabs/puppet/puppet.conf --onetime --server foreman.dotslash.io --no-daemonize

Which suggests that it is skipping the block in the snippet altogether which I am not understanding. The os_family should already ‘Redhat’ and the os_major version is 7 which is greater than 6.

<% if os_family == 'Redhat' -%>
<% if os_major > 6 -%>
puppet_unit=puppet
/usr/bin/systemctl list-unit-files | grep -q puppetagent && puppet_unit=puppetagent
/usr/bin/systemctl enable ${puppet_unit}
<% else -%>
/sbin/chkconfig --level 345 puppet on
<% end -%>
<% end -%>

● puppet.service - Puppet agent
   Loaded: loaded (/usr/lib/systemd/system/puppet.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

What am I missing here?

Go to the host in foreman and view the generated template. Does it look as expected there? If you run the commands in the generated template do they produce the desired results?

It definitely does not generate the line for enabling and starting the service.

#cloud-config
hostname: smtp-gateway.example.com
fqdn: smtp-gateway.example.com
manage_etc_hosts: true
users: {}

runcmd:
- |
  
  rpm -Uvh https://yum.puppetlabs.com/puppet5/puppet5-release-el-7.noarch.rpm
  
  
  
  
  if [ -f /usr/bin/dnf ]; then
    dnf -y install puppet-agent
  else
    yum -t -y install puppet-agent
  fi
  /opt/puppetlabs/bin/puppet agent --config /etc/puppetlabs/puppet/puppet.conf --onetime --server foreman.example.com --no-daemonize
  

phone_home:
  url: http://foreman.example.com/unattended/built
  post: []
  tries: 10

Did some additional investigation, and it seems like using offending block is this. Removing it allowed the subsequent conditionals for os_family == ‘Redhat’ to be preserved. However, the very last line where it calls puppet to start the agent service still does not appear until the <% end -%> tag is moved.

Offending block

<% elsif os_family == 'Suse' -%>
<% if host_param_true?('enable-puppetlabs-pc1-repo') || host_param_true?('enable-puppetlabs-puppet5-repo') -%>
rpmkeys --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs
rpmkeys --import http://yum.puppetlabs.com/RPM-GPG-KEY-puppet
<% end -%>
<% if @host.provision_method == 'image' -%>
/usr/bin/zypper -n install <%= linux_package %>
<% end -%>

Generated output with the elseif block removed

if [ -f /usr/bin/dnf ]; then
  dnf -y install puppet-agent
else
  yum -t -y install puppet-agent
fi


cat > /etc/puppetlabs/puppet/puppet.conf << EOF


[main]
vardir = /opt/puppetlabs/puppet/cache
logdir = /var/log/puppetlabs/puppet
rundir = /var/run/puppetlabs
ssldir = /etc/puppetlabs/puppet/ssl

[agent]
pluginsync      = true
report          = true
ignoreschedules = true
ca_server       = foreman.example.com
certname        = smtp-gateway.example.com
environment     = production
server          = foreman.example.com

EOF

puppet_unit=puppet
/usr/bin/systemctl list-unit-files | grep -q puppetagent && puppet_unit=puppetagent
/usr/bin/systemctl enable ${puppet_unit}
/opt/puppetlabs/bin/puppet agent --config /etc/puppetlabs/puppet/puppet.conf --onetime --server foreman.example.com --no-daemonize

Editing the ending conditional seem to properly generate the line to start the service.

From

<%= bin_path %>/puppet agent --config <%= etc_path %>/puppet.conf --onetime <%= @host.puppetmaster.blank? ? '' : "--server #{@host.puppetmaster}" %> --no-daemonize
<% if os_family == 'Suse' || (os_name == 'Debian' && os_major > 8) || (os_name == 'Ubuntu' && os_major >= 15) -%>
<%= bin_path %>/puppet resource service puppet enable=true
<% if @host.provision_method == 'image' -%>
<%= bin_path %>/puppet resource service puppet ensure=running
<% end -%>
<% end -%>

To

<%= bin_path %>/puppet agent --config <%= etc_path %>/puppet.conf --onetime <%= @host.puppetmaster.blank? ? '' : "--server #{@host.puppetmaster}" %> --no-daemonize
<% if os_family == 'Suse' || (os_name == 'Debian' && os_major > 8) || (os_name == 'Ubuntu' && os_major >= 15) -%>
<%= bin_path %>/puppet resource service puppet enable=true
<% end -%>
<% if @host.provision_method == 'image' -%>
<%= bin_path %>/puppet resource service puppet ensure=running
<% end -%>