skip to Main Content

I have a below variables.yml file

variables:
  vmImage: ubuntu
  poolName: 'cloud'
  env: Dev
  module: $(moduleName)
  
  sc: abc-dev
  rg: abc-dev-rg
  location: eus2

I would like to toggle the value of sc,rg and location based on the value of $(moduleName). Is this possible to achieve.

something like this

- ${if eq($(modulename), 'abc') }: 
    sc: abc-dev
    rg: abc-dev-rg
    location: eus2
- ${if eq($(modulename), 'xyz') }: 
    sc: xyz-dev
    rg: xyz-dev-rg
    location: scus

I tried using multiple if conditions but looks like this cannot be done is variables.yml file. So I was looking for any alternative options.

2

Answers


  1. variables.yml

    variables:
      vmImage: ubuntu
      poolName: 'cloud'
      env: Dev
      module: 'xyz'
    

    main.yml

    variables:
    - template: variables.yml
    
    - ${{ if eq('1', '1') }}:
      - name: sc1
        value: abc-dev1
      - name: rg1
        value: abc-dev-rg1
      - name: location1
        value: eus21
    
    - ${{ if eq(variables.module, 'abc') }}:
      - name: sc
        value: abc-dev
      - name: rg
        value: abc-dev-rg
      - name: location
        value: eus2
    - ${{ if eq(variables.module, 'xyz') }}:
      - name: sc
        value: xyz-dev
      - name: rg
        value: xyz-dev-rg
      - name: location
        value: scus
    
    steps:
    - ${{ if eq(variables.sc, 'abc-dev') }}:
      - script: echo "this is $(sc)"
    - ${{ elseif eq(variables.sc, 'xyz-dev') }}: # true
      - script: echo "this is $(sc)" 
    - ${{ else }}:
      - script: echo "this is nothing"
    
    - script: echo "$(sc1)"
    

    change the module value in variables, you should get the corresponding result. hope this can help you.

    enter image description here

    Login or Signup to reply.
  2. Based on my test, we cannot use condition to assign values to variables in variable template.

    As a workaround, you can create a variable template for each moduleName and then select the corresponding template based on the value of moduleName. For example, create variable templates "abc-var.yml" and "xyz-var.yml".

    xyz-var.yml:

    variables:
      vmImage: ubuntu
      poolName: 'cloud'
      env: Dev
      module: 'xyz'
      sc: xyz-dev
      rg: xyz-dev-rg
      location: scus
    

    In the main yaml, define modulename as parameter and select template based on the value of modulename.

    parameters:
    - name: modulename
      type: string
      default: "abc"
      values:
      - abc
      - xyz
    variables:
    - ${{ if eq(parameters.modulename, 'abc') }}:
      - template: 'abc-var.yml'
    - ${{ if eq(parameters.modulename, 'xyz') }}:
      - template: 'xyz-var.yml'
    

    When you run the pipeline, you can select the value of modulename from UI.
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search