Skip to main content

Title: Automatic generation of Ansible and Terraform code

The Infrastructure Code Generator (ICG ) is the PIACERE component that allows generating executable infrastructural code (IaC) from models written in DevSecOps Modelling Language ( DOML). ICG can be seen as a DOML compiler that generates executable IaC code.

The ICG takes as an input the DOML model produced by the IDE (Integrated Development Environment)  and oversees the generation of the Infrastructure as Code (IaC) files necessary for the deployment, configuration and orchestration of the represented user application and the needed infrastructure. The ICG now  is available  as a running application (CLI) and in the future release will be available also  via REST API ( in a docker container).

The main components of ICG are: ICG Controller, ICG DOML Parser and  ICG Code Generator Plug-in as shown in the following figure.

ICG components


The code-generation strategy adopted in the ICG component is called in the literature Template-Based Code Generation (TBCG).This technique allows producing code from partially complete code snippets (“IaC templates” in the above figure).

The Controller is a Python module dedicated to the management of the processes of the ICG, it is invocated from the command line (or REST API in the future)  and receives in input the model generated by the IDE. The Controller then should call the second component of the ICG, the Parser, which takes as an input the DOML model and writes the intermediate representation that the ICG is going to use in the next steps. The last component to be called by the ICG Controller is the Code Generator, which is also a module implemented in python. It reads the intermediate representation and, using  the IaC templates available for the required resource generates the output code. The Code Generator is implemented by multiple plug-ins, one for each IaC language supported by ICG, at the moment Ansible and Terraform.

The main functionalities for the Infrastructural Code Generator are the following:

  1. Read the input DOML model to extract all the needed information.
  2. Generate executable code for selected IaC languages
  3. Provide enough extensibility to support the DOML extension mechanism
  4. Provide enough extensibility to generate code for new IaC languages
  5. Generate IaC code that supports different cloud platforms

All the functionalities are implemented using an incremental approach over multiple releases. In this first release of the ICG prototype the functionalities 2 and 5 are implemented while the others are in progress.

The first version of the prototype is more focused on code generation functionalities than on the parsing of the DOML input. We used a bottom-up approach,  and the current prototype contains the backend part of  the compiler which generates IaC code from the developed templates, using as input a preliminary, handcrafted, version of the Intermediate Representation that in the next version will be created by the Parser. The final version of ICG component will get DOML models in input and generate IaC files as output in Terraform and Ansible languages. Templates have been created for the major DOML concepts, such as VM, Network, Database, and others.

An example of ICG usage is the generation of the IaC files for the deployment of the Wordpress application on Azure Cloud. Supposing the user defines in DOML this architecture:

  • One Virtual Machine for the installation of MySQL database server
  • One Virtual Machine as Wordpress docker container
  • A Virtual Network connecting the Virtual Machines
  • One public IP to reach Wordpress application from the Internet


The ICG at the end will produce:

  • Terraform files for provision of VMs, Network and public IP address
  • Ansible files for the installation and configuration of MySQL database, Docker and Wordpress container application


The execution of these files at the end will instantiate the environment shown in the following figure:


Future versions of the ICG will complete the planned functionalities and will improve several aspects of the component, in particular next version will focus on parser development while the final one on implementing ICG extensibility, providing guidelines for writing new templates, so that expert users will be able to develop their own templates or to modify existing ones, both for supporting new DOML concepts and for providing support for new IaC languages.


Share this post

Comments (0)