Search code examples
powershellshellansibleansible-roleazure-devops-server-2019

How to use Azure DevOps server (TFS) Predefined Variable in My Ansible Playbook?


I want to use Azure DevOps Predefine Variable "$(Build.SourcesDirectory)" in My playbook:

Here is my playbook:

---
- hosts: KBR_MTL361
  tasks:
   - name: copy file
     win_copy:
      src: D:\Web.config
      dest: $(Build.SourcesDirectory)

I am running this ansible-playbook using Azure DevOps Pipeline:

TFS Pipeline Task

But it is not working

Is there anyone who has any idea how to use the variable in the pipeline?


Solution

  • Just add your variables as additional args in the azure-pipelines.yml like this:

        - task: Ansible@0
          inputs:
            ansibleInterface: 'agentMachine'
            playbookPathOnAgentMachine: 'ansible/tfs_playbooks/install_agent.yml'
            inventoriesAgentMachine: 'file'
            inventoryFileOnAgentMachine: 'hosts.yml'
            args: '--extra-vars "build_source_dir=$(Build.SourcesDirectory) AGENT_URL=$(AGENT_URL)"'
    

    Then you can access the variables in your playbook:

    ---
    - hosts: localhost
      tasks:
      - name: show debug
        debug:
          msg: "Dir {{ build_source_dir }} agent url {{AGENT_URL}}"