4

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?

Dharti Sutariya
  • 419
  • 1
  • 5
  • 14

2 Answers2

2

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}}"
AstraSerg
  • 502
  • 10
  • 16
1

if you look here: https://daniel-krzyczkowski.github.io/Parameters-In-Azure-DevOps-Pipelines there is a certain way to pass Pipeline variables to a powershell script, for instance:

[CmdletBinding()]
param (
    $ApiManagementServiceName,
    $ApiManagementServiceResourceGroup
)

$apimServiceName = $ApiManagementServiceName
$resourceGroupName = $ApiManagementServiceResourceGroup

Write-Host "Api Management Service Name: $($apimServiceName)"
Write-Host "Api Management Resource Group Name: $($resourceGroupName)"

you are using still powershell you say, so give this a try or try to do something similar that works in your case, for me the above approach works pretty well in standard powershell.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 1
    I am not using PowerShell in my ansible YAML file. I have also tried with the shell script as well: Here is the output for the same: 2020-01-16T04:42:40.9212316Z Successfully connected. 2020-01-16T04:42:40.9212941Z sh tfsvariable.sh D:\Agent_1\_work\1\s I have run the script like: sh tfsvariable.sh $(Build.SourcesDirectory) – Dharti Sutariya Jan 16 '20 at 12:25
  • 1
    see the msg: But when I am using this path in my yml file its act like this: TASK [debug] ******************************************************************* 2020-01-16T04:32:36.1905719Z 2020-01-16T04:32:36.2135800Z ok: [MTL361.metatreesystems.com] => { 2020-01-16T04:32:36.2136628Z "msg": "D:Agent_1_work1s" 2020-01-16T04:32:36.2136981Z } – Dharti Sutariya Jan 16 '20 at 12:32