RLabs: Provisioning Linux with Foreman on VMware from template

Ok, so now you have connected Foreman with VMware and want to start creating Linux VMs.

Creating a CentOS VM template

You´ll need to create a template VM on VMware first.

Download the CentOS minimal install image and upload it to a VMware datastore.

Install CentOS

Create a VM called CentOS7 with 1Gb of RAM, 1vCPU and a minimum of 10Gb of disk.
Ensure that the SCSI controller is set to VMware Paravirtual.
Configure partitioning as you want (I used only swap and / for RLabs).
Disable KDump.
Set timezone to Etc/UTC (my personal choice) and add pool.ntp.org as NTP server.
Let the network use DHCP, just ensure it will connect at boot.
Set a root password. I used rootroot.
Do the minimal install.
You don´t need to create another user. In production you should.

Configure the VM

Update the system and install some basic tools:

yum update -y
yum install -y net-tools bind-utils open-vm-tools bash-completion bash-completion-extras vim-enhanced telnet nc wget

Disable firewalld, selinux (do not do this in production) and other stuff:

systemctl disable firewalld
systemctl disable postfix
sed -i s/SELINUX=.*/SELINUX=disabled/ /etc/selinux/config
echo "UseDNS no" >> /etc/ssh/sshd_config
echo "wget -q -O /dev/null --no-check-certificate https://foreman/unattended/built &" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local
echo "Red Hat Enterprise Linux Server release 7.5 (Maipo)" > /etc/redhat-release
rm -f  /etc/ssh/ssh_host_* sync ; halt

Put the foreman-proxy rsa key

Copy the foreman-proxy key content on:
To /root/.ssh/authorized_keys on the VM.

Doing so allow foreman to run jobs on the hosts, including running ansible tasks.


Poweroff the VM and convert it to template.

Import the template as an Image in the compute resource

Go to Infrastructure->Compute resource->DC1_VMware and click on “Create image”.

Enter required information as follow:

Screenshot 2018-09-29 19.00.39.png

NOTE: Don´t forget to check the userdate box.

Create a compute profile

Go to Infrastructure->Compute profile->Create compute profile.
Name it DC1VMW_Linux and select the DC1_VMware compute resource.
Configure it as follow:


Screenshot 2018-09-29 19.04.52.png

Screenshot 2018-09-29 18.38.54.png

Configure the CentOS OS on Foreman

User_data script

We´ll need a user_data template to create VMs based on VMware templates.
What happens is that Foreman will convert it to a customization spec on VMware that will be applied on the first boot of the VM.

Go to Hosts->Provisioning Templates->Create template.
Name it “VMware_linux_userdata”.
Type: User data template.
Association: select all CentOS
On the template text field, paste this:

    domain: <%= @host.domain %>
    hostName: <%= @host.shortname %>
  dnsServerList: [ <%= @host.primary_interface.subnet.dns_primary %>, <%= @host.primary_interface.subnet.dns_secondary %> ]
  dnsSuffixList: [<%= @host.domain %>]
<% @host.interfaces.each do |interface| %>
  - adapter:
      ip: <%= interface.ip %>
      subnetMask: <%= interface.subnet.mask %>
      <% if interface.primary -%>gateway: [<%= interface.subnet.gateway %>]<% end %>
<% end %>


Customize the CentOS operating system

Go to Hosts->Operating systems and click on the OS that have hosts.
Go to the templates tab and set user data to VMware_linux_userdata, then submit.


Create a “Generic Linux” hostgroup

You don´t need it, but it is very recommended so you can set many default values instead of setting them everytime you create a VM.

Go to Configure->Hostgroups->Create hostgroup.
Name it “Generic Linux”.

On the first screen, use the following values:

Screenshot 2018-09-29 18.57.21.png

On the network tab:

Screenshot 2018-09-29 18.57.57.png

On the OS tab:

Screenshot 2018-09-29 19.07.03.png


Create a test VM

Ok, so let´s create a VM for testing !

Go to Hosts->Create host

Populate as follow:

Screenshot 2018-09-29 19.08.50.png

On the Operating System tab:

Screenshot 2018-09-29 19.10.06.png