将ansible变量传递给包含花括号的powershell代码

时间:2020-07-03 14:04:14

标签: powershell variables ansible curly-braces

我想将包含ansible变量的p​​owershell命令的执行与win_shell模块集成到ansible任务中,或者我总是在模板字符串时收到错误“模板错误:预期标记'print statement的结尾',得到了'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}}'变量没有任何问题,但对于Powershell花括号之间的ansible变量却没有问题

可更改的变量key1 / 2/3设置为var文件。

感谢您的帮助!

0 个答案:

没有答案