如何告诉Ansible win_shell不要尝试解释多行命令

当直接在Powershell中运行ps代码块时,下面的Ansible“问题播放”运行正常。当通过Ansible运行时,它会失败:

ERROR! failed at splitting arguments,either an unbalanced jinja2 block or quotes: $iExit = 0

运行简单的代码块不会出现问题(也包括在下面)

我曾尝试使用{%raw%}和{%endraw%}将块包装在jinja2多行转义中。错误始终是第一行:

ERROR! failed at splitting arguments,either an unbalanced jinja2 block or quotes: {% raw %}

问题播放

tasks:

  - name: Purge all rotated logs
    win_shell: |
      $iExit = 0
      Get-ChildItem D:\application*|? {$_.PSIsContainer -eq $true}|% {
      Get-ChildItem log* -Path "$_\logs\" -Erroraction Stop |? {$_.Name -match "log\d{4}-\d{2}-\d{2}\.\d{2}\.txt"}|% {
      Write-Host Removing $_.FullName which is $_.Length Bytes and last written to on $_.LastWriteTime
      Try {
      Remove-Item $_.FullName -Erroraction Stop
      }
      Catch [Exception] {
      $iExit = 1
      Write-Host "[ERROR removing file: $($_.Exception.Message)]"
      }}}
      Exit $iExit

非问题性游戏

tasks:

    - name: Purge all rotated logs
      win_shell: |
        Get-Childitem log* -Recurse -path D:\logs\ |
        Where{$_.Name -match "log\d{4}-\d{2}-\d{2}\.\d{2}\.txt"} |
        Foreach-Object {
        Write-Host Removing $_.FullName which is $_.Length Bytes and last written to on $_.LastWriteTime
        Remove-Item $_.FullName
        }
YesKing_new 回答:如何告诉Ansible win_shell不要尝试解释多行命令

问题似乎是此行:

...
Get-ChildItem log* -Path "$_\logs\" -ErrorAction Stop ...
...

最后一个引号是

"$_\logs\"

无意中,通过使文件路径带有斜杠结尾,您无意中告诉Ansible使用\"来转义最后一个引号,它看起来像关闭了字符串,从而导致“不平衡的引号”错误。

删除尾部斜杠可以解决该错误。

本文链接:https://www.f2er.com/3150713.html

大家都在问