当直接在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
}