Category: yaml

In the last post in this series, we covered the basics of creating a YAML pipeline in Azure DevOps to run Terraform. Our aim is to manage our infrastructure as code, and to that aim we want our pipelines — which deploy said code — to be as concise, configurable, and adherent to the DRY principle as possible.

We’ll put this in a folder called vars just beneath the directory that holds our pipeline yaml file: In this file we’ll declare all of the variables we had earlier as key-value pairs: With our variables now saved in a template file, we can call them directly in the stages where we need them, instead of hardcoding them at the top of the pipeline: What this means is that we can create multiple variable templates for each environment we deploy to.

We will take the three tasks that comprise our terraform plan and convert them into a template by doing the following: Here is what our folder structure looks like now: And what our template file looks like at the moment: To make this behave like a function, we’ll do the following: Once this is all done, your template should look like this: We now have templated variables and a templated pipeline job. Let’s replace the tasks underneath the job definition we had previously with the new template, and pass the variables we defined at the top of our pipeline to the template.

Related Articles