Powershell 如何在我的Ansible剧本中使用Azure DevOps服务器(TFS)预定义变量?

Powershell 如何在我的Ansible剧本中使用Azure DevOps服务器(TFS)预定义变量?,powershell,shell,ansible,ansible-role,azure-devops-server-2019,Powershell,Shell,Ansible,Ansible Role,Azure Devops Server 2019,我想在我的剧本中使用Azure DevOps预定义变量“$(Build.SourcesDirectory)”: --- - hosts: KBR_MTL361 tasks: - name: copy file win_copy: src: D:\Web.config dest: $(Build.SourcesDirectory) 这是我的剧本: --- - hosts: KBR_MTL361 tasks: - name: copy file

我想在我的剧本中使用Azure DevOps预定义变量“$(Build.SourcesDirectory)”:

---
- hosts: KBR_MTL361
  tasks:
   - name: copy file
     win_copy:
      src: D:\Web.config
      dest: $(Build.SourcesDirectory)
这是我的剧本:

---
- hosts: KBR_MTL361
  tasks:
   - name: copy file
     win_copy:
      src: D:\Web.config
      dest: $(Build.SourcesDirectory)
我正在使用Azure DevOps管道运行此ansible playbook:

但它不起作用

是否有人知道如何使用管道中的变量?

如果您看这里:有一种方法可以将管道变量传递给powershell脚本,例如:

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

$apimServiceName = $ApiManagementServiceName
$resourceGroupName = $ApiManagementServiceResourceGroup

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

你说你仍在使用powershell,所以请尝试一下,或者尝试做一些类似的事情,在你的情况下,对我来说,上述方法在标准powershell中非常有效。

只需在azure管道中添加变量作为附加参数。yml如下所示:

    - 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)"'
然后,您可以访问剧本中的变量:

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

我没有在我的ansible YAML文件中使用PowerShell。我也尝试过shell脚本:下面是相同脚本的输出:2020-01-16T04:42:40.9212316Z已成功连接。2020-01-16T04:42:40.9212941Z sh tfsvariable.sh D:\Agent_1_work\1\s我运行了如下脚本:sh tfsvariable.sh$(Build.SourcesDirectory)查看消息:但当我在yml文件中使用此路径时,其行为如下:TASK[debug]*********************************************************************************************************2020-01-16T04:32:36.1905719Z 2020-01-16T04:32:32:36.213580Z ok:[MTL361.metatreesystems.com]=>{2020-01-16T04:32:36.2136628Z“msg”:“D:Agent_1-u-u-2138146; 1Z”终于开始工作了。。。非常感谢@AstraSerg为实现我的目标提供的巨大帮助。