首页 > 解决方案 > 将 ansible 变量传递给包含花括号的 powershell 代码

问题描述

我想将包含 ansible 变量的 powershell 命令的执行与 win_shell 模块集成到 ansible 任务中,否则我总是收到错误“模板错误时模板字符串:预期令牌'打印语句结束',得到'std'”。但是,我尝试添加引号和双引号,但没有任何帮助。这是使用 powershell 代码的任务:

- name: add windows key
  win_shell: |
    $kms = '{{kms}}'
    $OSversion = (Get-WmiObject -class Win32_OperatingSystem).Caption
    switch -Regex ($OSversion) {
      'Windows Server 2016 Standard'   {$key = {{ key1 }};break}
      'Windows Server 2016 Datacenter' {$key = {{ key2 }};break}
      'Windows Server 2019 Datacenter' {$key = {{ key3 }};break}
    }
    $KMSservice = Get-WMIObject -query "select * from SoftwareLicensingService"
    Write-Debug 'Activating Windows.'
    $null = $KMSservice.InstallProductKey($key)
    $null = $KMSservice.SetKeyManagementServiceMachine($kms)
    $null = $KMSservice.RefreshLicenseStatus()

我对'{{kms}}' var没有任何问题,但只有在powershell花括号之间有ansible vars

ansible 变量 key1/2/3 被设置到一个 var 文件中。

谢谢您的帮助!

标签: powershellvariablesansiblecurly-braces

解决方案


推荐阅读