Automate a Custom OS Deployment on IBM Cloud Bare Metal Servers Using Terraform and Ansible

3 min read

Deploying a custom image in a cloud environment can present certain challenges

Building automation around infrastructure deployment is key to ensuring rapid delivery of services in the cloud. Deploying standard image templates is a straightforward process on the IBM Cloud, and this can be automated for efficiency and to avoid manual errors. But what about deploying a custom image?  

Deploying a custom image in a cloud environment can present various challenges—ensuring proper formatting of images, encountering cloud server image limitations and compatibility, and the time required to upload and configure images, just to mention a few.  

Automation technology can drastically speed up the deployment of custom images, ensure best practices, and remove any chance of manual errors.    

Example code for an automation solution 

In this blog post, I will provide example code for a solution to automate deployment of a custom OS on a No OS IBM Cloud Bare Metal Server using Terraform and Ansible technology. Terraform will be used to deploy the compute resources—in this case a virtual server that will serve as our Preboot Execution Environment (PXE) server and the No OS bare metal server. Then, Ansible will be used to set up the PXE server.

Specifically, the code in this example will do the following:

  • Create a new VLAN for PXE deployment environment
  • Create a new Subnet on the PXE Server VLAN for use with DHCP
  • Deploy an hourly virtual server as the PXE Server (Ubuntu 16 2x4 Instance)
  • Run Ansible Playbooks to:
    • Update the PXE Server Operating System
    • Install and configure Dnsmasq and PXE
    • Download an Ubuntu 18 Server ISO
    • Copy ISO netboot tools in to the tftpboot directory
    • Copy Ubuntu ISO files to PXE Directory
    • Update the dnsmasq server configuration
    • Configure DHCP settings
  • Create an IBM Cloud support ticket to have the virtual servers private IP set as the DHCP helper address on the appropriate VLAN
  • Deploy a No OS server on to the PXE server VLAN


In order to use this example code, you will need an upgraded IBM Cloud account. Don’t have an IBM Cloud account yet?  Sign up here.

  • Terraform and the IBM Terraform provider plugin installed. Guide.
  • Ansible installed. Guide.
  • An IBM Cloud PaaS and IaaS API key. Guide.

If you already have Terraform v0.12 installed, you will need to use a different machine or downgrade. The IBM Cloud Provider plugin only supports pre-v0.12 Terraform versions.

Step-by-step process to use the example code

1. Download the example code

$ git clone
$ cd ibm_pxe_terraform

2. Update the install.yml file with your local SSH key

If you don't have an SSH key generated, you will need to create one. The SSH key will be used to authenticate with our PXE instance for Ansible communication. The example below will work on macOS and Linux. If you need different options, please see this guide on how to generate an SSH Key.

$ ssh-keygen -o -t rsa -b 4096

Replace your_public_ssh_key_here with your Public SSH Key in the install.yml file.

$ nano/vi install.yml

3. Configuring the provider

Copy the credentials template file and update it with your IBM Cloud details. See this page for more information about provider variables.

$ cp credentials.tfvars.tpl credentials.tfvars
$ nano/vi credentials.tfvars

4. Initialize Terraform

Run the terraform init command to initialize the directory containing your Terraform files.

$ terraform init

5. Create the initial Terraform plan

The plan command is used to create an execution plan based on your Terraform files.

$ terraform plan -var-file='./credentials.tfvars' -out project.tfplan
Terraform plan

6. Deploy your infrastructure

With our plan created, we can now get to the task at hand of deploying infrastructure assets.

$ terraform apply project.tfplan

7. Run Ansible playbook

Run the Ansible playbook to configure your PXE server. 

$ ansible-playbook -i Hosts/inventory.env Playbooks/deploy-environment.yml

You may need to reboot the bare metal server via the Customer portal or API in order for it to pick up the new DHCP Helper IP address.

8. Log in to the NO OS Server IPMI and complete the install

PXE boot

Questions and issues

For any issues or questions, please visit the GitHub repositories issues page.

Be the first to hear about news, product updates, and innovation from IBM Cloud