我正在通过Python使用Ansible从远程Windows主机获取日志。我不确定自己在做什么错,因为Ansible无法获取日志文件。
这是我的Python调用:
status = ansible_runner.run_ansible_playbook(
"fetch-windows-cbl-logs.yml",
extra_vars={
"log_full_path": log_full_path,
"log_file": log_file
}
)
这是我的Ansible剧本:
stop-fetch-windows-cbl-logs.yml
- name: Getting directory of files
win_shell: Get-ChildItem | where {!$_.PsIsContainer} | foreach { $_.Name }
args:
chdir: "{{ log_file }}"
register: files_to_copy
- name: Debug Msg
debug: msg="Copying logs at - {{ log_full_path }}"
- name: Fetch logs
fetch: src={{ log_file }} dest={{ log_full_path }} fail_on_missing=yes validate_checksum=yes flat=yes
with_items: "{{ files_to_copy.stdout_lines }}"
从另一本剧本中调用-
- name: Windows - Stop TestServer
hosts: windows
vars:
log_full_path:
log_file:
tasks:
- include: tasks/stop-fetch-windows-cbl-logs.yml
我遇到以下错误:
Running playbook: libraries/provision/ansible/playbooks/fetch-windows-cbl-logs.yml
PLAY [Windows - Stop TestServer] ***********************************************
TASK [setup] *******************************************************************
ok: [win1]
TASK [debug] *******************************************************************
ok: [win1] => {
"msg": "Copying from C:\\Users\\Administrator\\AppData\\Local\\Packages\b0bd6c4c-e257-44c1-aeeb-7188f65af9d0_75cr2b68sm664\\AC\\Temp\\log_1556888740.22821\\cbl_debug_1556888740255.cbllog to /tmp/cbl-logs/"
}
TASK [Fetch logs] **************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fatal: [win1]: FAILED! => {"changed": false, "error_record": {"CategoryInfo": {"Activity": "Test-Path", "Category": 0, "Reason": "ArgumentException", "TargetName": "", "TargetType": ""}, "ErrorDetails": null, "Exception": {"Data": {}, "HResult": -2147024809, "HelpLink": null, "InnerException": null, "Message": "Illegal characters in path.", "ParamName": null, "Source": "mscorlib", "StackTrace": " at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)\r\n at System.IO.Path.GetFileName(String path)\r\n at System.Management.Automation.Utils.IsReservedDeviceName(String destinationPath)\r\n at System.Management.Automation.Utils.NativeItemExists(String path, Boolean& isDirectory, Exception& exception)\r\n at System.Management.Automation.Utils.NativeDirectoryExists(String path)\r\n at System.Management.Automation.SessionStateInternal.IsItemContainer(CmdletProvider providerInstance, String path, CmdletProviderContext context)", "TargetSite": {"Attributes": 147, "CallingConvention": 1, "ContainsGenericParameters": false, "CustomAttributes": "", "DeclaringType": "System.IO.Path", "IsAbstract": false, "IsAssembly": true, "IsConstructor": false, "IsFamily": false, "IsFamilyAndAssembly": false, "IsFamilyOrAssembly": false, "IsFinal": false, "IsGenericMethod": false, "IsGenericMethodDefinition": false, "IsHideBySig": true, "IsPrivate": false, "IsPublic": false, "IsSecurityCritical": false, "IsSecuritySafeCritical": false, "IsSecurityTransparent": true, "IsSpecialName": false, "IsStatic": true, "IsVirtual": false, "MemberType": 8, "MetadataToken": 100669730, "MethodHandle": "System.RuntimeMethodHandle", "MethodImplementationFlags": 0, "Module": "CommonLanguageRuntimeLibrary", "Name": "CheckInvalidPathChars", "ReflectedType": "System.IO.Path", "ReturnParameter": "Void ", "ReturnType": "void", "ReturnTypeCustomAttributes": "Void "}}, "FullyQualifiedErrorId": "System.ArgumentException,Microsoft.PowerShell.Commands.TestPathCommand", "InvocationInfo": {"BoundParameters": {}, "CommandOrigin": 1, "DisplayScriptPosition": null, "ExpectingInput": false, "HistoryId": 1, "InvocationName": "Test-Path", "Line": "ElseIf (Test-Path -PathType Container $src)\n", "MyCommand": {"CommandType": 8, "DefaultParameterSet": "Path", "Definition": "\r\nTest-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]\r\n\r\nTest-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]\r\n", "HelpFile": "Microsoft.PowerShell.Commands.Management.dll-Help.xml", "ImplementingType": "Microsoft.PowerShell.Commands.TestPathCommand", "Module": "Microsoft.PowerShell.Management", "ModuleName": "Microsoft.PowerShell.Management", "Name": "Test-Path", "Noun": "Path", "Options": 1, "OutputType": "System.Boolean", "PSSnapIn": null, "ParameterSets": "[-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>] -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]", "Parameters": "System.Collections.Generic.Dictionary`2[System.String,System.Management.Automation.ParameterMetadata]", "RemotingCapability": 1, "Source": "Microsoft.PowerShell.Management", "Verb": "Test", "Version": "3.1.0.0", "Visibility": 0}, "OffsetInLine": 9, "PSCommandPath": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1556892651.2-106929474671208\\slurp.ps1", "PSScriptRoot": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1556892651.2-106929474671208", "PipelineLength": 0, "PipelinePosition": 0, "PositionMessage": "At C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1556892651.2-106929474671208\\slurp.ps1:285 char:9\r\n+ ElseIf (Test-Path -PathType Container $src)\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", "ScriptLineNumber": 285, "ScriptName": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1556892651.2-106929474671208\\slurp.ps1", "UnboundArguments": []}, "PSMessageDetails": null, "PipelineIterationInfo": [], "ScriptStackTrace": "at <ScriptBlock>, C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1556892651.2-106929474671208\\slurp.ps1: line 285\r\nat <ScriptBlock>, <No file>: line 4", "TargetObject": null}, "failed": true, "msg": "Illegal characters in path."}
to retry, use: --limit @/Users/hemant/couchbase/couchbase/mobile-testkit/libraries/provision/ansible/playbooks/fetch-windows-cbl-logs.retry
解决上述错误后,Ansible剧本说它正在复制文件,但是我看不到任何文件复制在其中-
Running playbook: libraries/provision/ansible/playbooks/fetch-windows-cbl-logs.yml
PLAY [Windows - Stop TestServer] ***********************************************
TASK [setup] *******************************************************************
ok: [win1]
TASK [Getting directory of files] **********************************************
changed: [win1]
TASK [Debug Msg] ***************************************************************
ok: [win1] => {
"msg": "Copying logs at - /tmp/cbl-logs/"
}
TASK [Fetch logs] **************************************************************
ok: [win1] => (item=cbl_debug_1556888740255.cbllog)
ok: [win1] => (item=cbl_error_1556888740258.cbllog)
ok: [win1] => (item=cbl_info_1556888740256.cbllog)
ok: [win1] => (item=cbl_verbose_1556888740256.cbllog)
ok: [win1] => (item=cbl_warning_1556888740257.cbllog)
PLAY RECAP *********************************************************************
win1 : ok=4 changed=1 unreachable=0 failed=0
Checking /tmp/cbl-logs/ for copied log files - []
任何人都知道可能是什么问题?
答案 0 :(得分:0)
我知道这很困难,但是您实际上需要仔细阅读错误消息(Ansible输出中第二行的底部-非常长的一行)。原因在某处。请记住,它通常是“元数据,异常原因,堆栈跟踪”。在这种情况下,相关详细信息(一旦将其拉出)为:
"Exception": {..., "Message": "Illegal characters in path.", ...}
这就是您的错误。路径中包含非法字符。我们可以通过查找堆栈跟踪来走得更远,该消息稍后会出现在错误消息中:
"StackTrace": " at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)\r\n at System.IO.Path.GetFileName(String path)\r\n at System.Management.Automation.Utils.IsReservedDeviceName(String destinationPath)\r\n at System.Management.Automation.Utils.NativeItemExists(String path, Boolean& isDirectory, Exception& exception)\r\n at System.Management.Automation.Utils.NativeDirectoryExists(String path)\r\n at System.Management.Automation.SessionStateInternal.IsItemContainer(CmdletProvider providerInstance, String path, CmdletProviderContext context)"
我通常通过打开python控制台,用三引号将其粘贴并打印以摆脱\r\n
来处理此问题(如有必要,可以多次执行此操作):
>>> x = """" at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)\r\n at System.IO.Path.GetFileName(String path)\r\n at System.Management.Automation.Utils.IsReservedDeviceName(String destinationPath)\r\n at System.Management.Automation.Utils.NativeItemExists(String path, Boolean& isDirectory, Exception& exception)\r\n at System.Management.Automation.Utils.NativeDirectoryExists(String path)\r\n at System.Management.Automation.SessionStateInternal.IsItemContainer(CmdletProvider providerInstance, String path, CmdletProviderContext context)"""
>>> print(x)
" at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.IO.Path.GetFileName(String path)
at System.Management.Automation.Utils.IsReservedDeviceName(String destinationPath)"
at System.Management.Automation.Utils.NativeItemExists(String path, Boolean& isDirectory, Exception& exception)
at System.Management.Automation.Utils.NativeDirectoryExists(String path)
at System.Management.Automation.SessionStateInternal.IsItemContainer(CmdletProvider providerInstance, String path, CmdletProviderContext context)
因此,总的来说,您的错误是“路径中的非法字符”(大概是log_file
或log_full_path
的路径。请调查您要传递到脚本的路径,看看是否可以确保它是仅限ascii,不包含任何特殊字符。
答案 1 :(得分:0)
我能够从我的Active Directory计算机(Windows Server 2019)到ansible主控主机(CentOS 7)中获取Kerberos密钥表文件。
这是我的清单文件和示例剧本。
在这种情况下,参数“ validate_checksum:是 ”至关重要。如果您不使用它,则剧本会因校验和错误而失败。
02:52 PM ✔ mhaque on iternal-r8 Δ [ansible-workspace] Ω cat inventory/invetory
[linuxservers]
crypton.belovedhorizon.com
helium.belovedhorizon.com
[winservers]
avocado.belovedhorizon.com
[winservers:vars]
ansible_user=Administrator
ansible_password="XXXXXXXXXXX"
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
02:52 PM ✔ mhaque on iternal-r8 Δ [ansible-workspace] Ω cat chopper.yml
---
- hosts: all
#become: yes
gather_facts: yes
tasks:
- name: Fetch the file from the avocado to ansible master
run_once: yes
fetch:
validate_checksum: yes
src: C:\TMP\krb\others\nfs-crypton.keytab
dest: /tmp/nfs-crypton.keytab
flat: yes
when: ansible_hostname == 'avocado
02:29 PM ✔ mhaque on iternal-r8 Δ [ansible-workspace] Ω ansible-playbook -i inventory/invetory chopper.yml -vv