tester81
tester81

Reputation: 595

Azure Devops for terraform deployment - variables block yml file - problem

I am preparing terraform deployment using Azure devops pipelines. I am storing all my variables in yml file, all worked fine, but now I am trying to use IF statement in my yml variables file. I am using structure like below presented, and it does not work.

variables:
  # Environment/Git Branch condition
  environment: $(Build.SourceBranchName)

  # Environment check - DEV or PROD
  ${{ if eq(variables['environment'], 'DEV') }}:
    # Application Network parameters - DEV
    address_space_01: '10.0.0.0/16'
    address_space_02: '10.1.0.0/16'
    address_space_03: '10.2.0.0/16'

  ${{ elseif eq(variables['environment'], 'prod') }}:
    # Application Network parameters - PROD
    address_space_01: '10.4.0.0/16'
    address_space_02: '10.5.0.0/16'
    address_space_03: '10.6.0.0/16'

I does not work, when azure devops pipeline trying to perform terraform deployment on Azure cloud, it throws an error each.value is "", so it means that all values of properties address_space_0x: are not passed properly to the terraform modules.

Upvotes: 0

Views: 240

Answers (2)

tester81
tester81

Reputation: 595

Below correct structure of YML variables file:

variables:
  # Environment/Git Branch condition
  environment: $(Build.SourceBranchName)

  # Environment check - DEV or PROD
  ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
    # Application Network parameters - DEV
    address_space_01: '10.0.0.0/16'
    address_space_02: '10.1.0.0/16'
    address_space_03: '10.2.0.0/16'

  ${{ if eq(variables['Build.SourceBranchName'], 'prod') }}:
    # Application Network parameters - PROD
    address_space_01: '10.4.0.0/16'
    address_space_02: '10.5.0.0/16'
    address_space_03: '10.6.0.0/16'

Upvotes: 0

Sunny Nazar
Sunny Nazar

Reputation: 246

I would not recommend to give values to Terraform variables in the pipeline like this way.

You should give values to terraform variables either at the resource level or at module level if you use terraform modules.

But please don’t give values in pipeline like this, it’s not a best approach.

If you have to create 10 different virtual networks, then how will you handle that ? If you keep following this approach .

could you please try below approach:

- ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}:
   address_space_01: '10.0.0.0/16'
   address_space_02: '10.1.0.0/16'
   address_space_03: '10.2.0.0/16'

- ${{ if eq(variables['Build.SourceBranchName'], 'prod') }}:
    address_space_01: '10.4.0.0/16'
    address_space_02: '10.5.0.0/16'
    address_space_03: '10.6.0.0/16'

Upvotes: 2

Related Questions