OpsCanary
Back to daily brief
azuredevopsPractitioner

YAML Templates in Azure Pipelines: Reusable and Secure Processes

5 min read Microsoft LearnApr 21, 2026
PractitionerHands-on experience recommended

In the world of DevOps, efficiency and security are paramount. YAML templates in Azure Pipelines address these needs by allowing you to define reusable content, logic, and parameters. This means you can avoid duplication and enforce best practices across your pipelines, leading to cleaner and more maintainable code.

Templates work by inserting reusable content into your pipeline. When you use an 'include' directive, it pulls in content from a specified template, similar to include statements in programming languages. The 'extends' functionality allows you to define a schema that pipelines must adhere to, ensuring consistency. For example, you can define a parameter like 'buildSteps' to control the steps executed in your pipeline, with default values to simplify configuration. This structure not only enhances readability but also minimizes the risk of errors in complex pipelines.

However, be cautious when implementing templates. They can lead to increased complexity and size in your pipeline configurations. Ensure that your templates are well-organized and that you avoid excessive nesting or overly complex logic. Additionally, remember that template files must exist on your filesystem at the start of a pipeline run; you cannot reference them in an artifact. This can be a common pitfall if you're not careful about your directory structure. Overall, YAML templates are a powerful tool for enhancing your Azure DevOps pipelines, but they require thoughtful implementation to avoid pitfalls.

Key takeaways

  • Use 'extends' to enforce a schema for your pipelines, ensuring consistency.
  • Define reusable parameters like 'buildSteps' to streamline your pipeline configuration.
  • Be aware that templates must exist on your filesystem at the start of a pipeline run.
  • Avoid excessive complexity in your templates to prevent pipeline bloat.
  • Leverage 'includes' to reduce redundancy and improve maintainability.

Why it matters

Implementing YAML templates can drastically reduce duplication in your pipelines, leading to faster development cycles and fewer errors. This not only improves team efficiency but also enhances the security posture of your CI/CD processes.

Code examples

YAML
1# File: start-extends-template.yml
2parameters:
3- name: buildSteps # the name of the parameter is buildSteps
4  type: stepList # data type is StepList
5  default: [] # default value of buildSteps
6stages:
7- stage: secure_buildstage
8  pool:
9    vmImage: windows-latest
10  jobs:
11  - job: secure_buildjob
12    steps:
13    - script: echo This happens before code 
14      displayName: 'Base: Pre-build'
15    - script: echo Building
16      displayName: 'Base: Build'
17
18    - ${{ each step in parameters.buildSteps }}:
19      - ${{ each pair in step }}:
20          ${{ if ne(pair.value, 'CmdLine@2') }}:
21            ${{ pair.key }}: ${{ pair.value }}       
22          ${{ if eq(pair.value, 'CmdLine@2') }}: 
23            # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
24            '${{ pair.value }}': error         
25
26    - script: echo This happens after code
27      displayName: 'Base: Signing'
YAML
1# File: azure-pipelines.yml
2trigger:
3- main
4
5extends:
6  template: start-extends-template.yml
7  parameters:
8    buildSteps:  
9      - bash: echo Test #Passes
10        displayName: succeed
11      - bash: echo "Test"
12        displayName: succeed
13      # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
14      - task: CmdLine@2
15        inputs:
16          script: echo "Script Test"
17      # Step is rejected by raising a YAML syntax error: Unexpected value 'CmdLine@2'
18      - script: echo "Script Test"
YAML
1# File: templates/insert-npm-steps.yml
2
3steps:
4- script: npm install
5- script: yarn install
6- script: npm run compile

When NOT to use this

Templates and template expressions can cause explosive growth to the size and complexity of a pipeline. If your project is small or your team is just starting with Azure DevOps, consider simpler configurations before adopting templates.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.