Dharti Sutariya
Dharti Sutariya

Reputation: 489

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?

Upvotes: 4

Views: 2492

Answers (2)

AstraSerg
AstraSerg

Reputation: 522

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}}"

Upvotes: 3

Davide Piras
Davide Piras

Reputation: 44605

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.

Upvotes: 1

Related Questions