How to use matrix variable within pool demands?

parameters:
  - name: agentNames
    type: object
    default: []
jobs:
- job: RunOnEveryAgent
  strategy:
    matrix:
       ${{ each agentName in parameters.agentNames }}:
        ${{ agentName }}:
         agentName: ${{ agentName }}
  pool:
    name: Hosted VS2017
    demands:
    - msbuild
    - visualstudio
    - Agent.Name -equals $(agentName)

This would be a better solution in case you want to add more agents in future


Variables are not supported in some of these jobs, due to the order in which they get expanded.

What you can do however, is to use template include syntax (https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops) for your job strategy and pass the agentname in as a parameter.

So your build job in its own YAML file might look like:

parameters:
  agentName1: ''
  agentName2: ''
  agentName3: ''

jobs:
- job: RunOnEveryAgent
  strategy:
    maxParallel: 3
    matrix:
      agent_1:
        agentName: ${{ parameters.agentName1 }}
      agent_2:
        agentName: ${{ parameters.agentName2 }}
      agent_3:
        agentName: ${{ parameters.agentName3 }}
  pool:
    name: Hosted VS2017
    demands:
    - msbuild
    - visualstudio
    - Agent.Name -equals ${{ parameters.agentName3 }}

  steps:

Your main azure-pipelines.yml then changes to look like this:

resources:
- repo: self

trigger: none

jobs:
  - template: buildjob.yml
    parameters:
      agentName1: 'Hosted Agent'
      agentName2: 'Hosted VS2017 2'
      agentName3: 'Hosted VS2017 3'