【问题标题】:How to use Azure DevOps server (TFS) Predefined Variable in My Ansible Playbook?如何在 My Ansible Playbook 中使用 Azure DevOps 服务器 (TFS) 预定义变量?
【发布时间】:2020-05-03 04:59:58
【问题描述】:

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

这是我的剧本:

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

我正在使用 Azure DevOps Pipeline 运行这个 ansible-playbook:

TFS Pipeline Task

但它不起作用

有人知道如何在管道中使用变量吗?

【问题讨论】:

    标签: powershell shell ansible ansible-role azure-devops-server-2019


    【解决方案1】:

    如果你看这里:https://daniel-krzyczkowski.github.io/Parameters-In-Azure-DevOps-Pipelines 有一种方法可以将管道变量传递给 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 中效果很好。

    【讨论】:

    • 我没有在我的 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:36.2135800Z 确定:[MTL361.metatreesystems.com] => { 2020-01-16T04:32:36.2136628Z "msg": "D:Agent_1_work1s" 2020-01-16T04:32:36.2136981Z }
    【解决方案2】:

    只需将变量作为附加参数添加到 azure-pipelines.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)"'
    

    然后您可以访问 playbook 中的变量:

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

    【讨论】:

    • 终于,它开始工作了...非常感谢@AstraSerg 为实现我的目标提供的大力帮助。
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多