See all articles
Refactoring of Foreman's Template Rendering Engine: Case Study

Refactoring of Foreman's Template Rendering Engine: Case Study

Here’s a behind-the-scenes look at our collaboration with a large retail company to build a new rendering engine for Foreman templates.

Modern organizations depend on their server infrastructure for most IT functions and day-to-day business operations. Effective server management is one of the key competencies of any IT department. The larger the company, the bigger the potential cost of suboptimal server management, poor automation of repetitive tasks, outdated security, etc.

In today’s article, we describe how we helped one of our clients improve their Foreman setup and implement a new template rendering engine that plays an important role in their IT infrastructure.

Our Client

Our client is the IT subsidiary of a leading retail company in Europe. We have supported their in-house software developers, data engineers, and IT architects in creating digital solutions for their parent company’s customers and employees.

Project Objectives

This time, they reached out to us with a project to develop new features for Foreman’s template rendering:

1. Provide the possibility to use templates from a Git repository (by default, templates were stored in the database)

2. Add support for using credentials from Hashicorp Vault in the templates.

Considering the client’s requirements, we realized the existing rendering engine needed refactoring. Therefore, we decided to build a new engine based on the old one, including the new features.

What is a Rendering Engine?

A rendering engine is pivotal in converting input data or code into a visually perceivable format or executable action in software development and IT infrastructure management. The rendering concept is ubiquitous across various domains, including web development, graphic design, and server management systems like Foreman. 

A rendering engine is a software component responsible for processing input data or code and generating an output that users or other software systems can interpret. In server management systems like Foreman, a rendering engine transforms template code into executable scripts that automate server configuration and deployment tasks.

Core Functionalities

Rendering engines typically encompass a range of functionalities designed to process input data efficiently and produce accurate outputs. Some of the core functionalities of rendering engines include:

Parsing: In server management, parsing involves analyzing configuration templates to identify variables, macros, and logic necessary for generating executable scripts tailored to server setups and operations.

Rendering: Once parsed, the rendering engine interprets the template data, translating it into a format compatible with server configurations and automation processes. In certain instances, the rendering engine compiles the interpreted data into optimized scripts or configurations for faster execution and improved server management tasks.

Executing: The final step involves executing the generated scripts to automate server configurations, deployments, and maintenance tasks, ensuring smooth operations and optimal performance across server environments.

Foreman and Generating Executable Scripts

Foreman is an open-source application that automates the lifecycle of physical and virtual Linux-based servers (called hosts). It’s used to manage servers across organizations, especially large ones, automate repetitive tasks, and deploy applications. If you’re unfamiliar with this tool, check out our article about how it helps approach various challenges in IT companies.

An important part of Foreman is the template rendering engine. It turns the ERB (Embedded Ruby) templates into executable scripts. For example, a template `<%= "#{@host.fqdn} #{@host.ip}" %>` produces ``. Variables and macros enable access to the data within templates. You can read more about that in the Foreman’s documentation: writing ERB templates, variables, and macros. Over the years, more features have been added to the engine, and the code has become much more complicated.

Our Solution: Introducing a New Rendering Paradigm

First, we designed a new template rendering system to ensure it covers all the client’s needs. Once it was ready, we also carried out the implementation. The new system is:

  • Fully compatible with the old one, contains all the previously existing features, and passes all tests.
  • Extendable with additional context details (i.e. variables, macros) through Foreman plugins.
  • Divided into modules – we refactored the old monolith engine into modules.
  • Independent of the template source.

From Foreman 1.20, the new template rendering engine replaced the old one.

We also introduced a new way of testing templates by using snapshots, as well as created Foreman plugins adjusted for the new engine:

  • foreman_git_templates plugin adds support for templates from Git repositories. For a host to use templates from a Git repository, set the host parameter named `template_url`.
  • foreman_vault plugin adds support for credentials from Hashicorp Vault by introducing a new `vault_secret` macro.

Develop Foreman Custom Plugins with iRonin.IT

Foreman is a powerful tool that can be adjusted to different requirements through third-party or custom plugins. Over the last few years, our backend team has offered Foreman development services, has been an active community member, and has created several custom plugins to serve the specific needs of our clients.

If you’re considering Foreman for your project or already have it implemented, but are missing some functionalities, let’s see how iRonin.IT could help you with that!

Read Similar Articles