首页 > 解决方案 > 使用ansible从windows获取文件到远程主机没有被正确复制

问题描述

我通过 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 playbook 说它正在复制文件,但我看不到那里复制的任何文件 -

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 - []

任何人都知道可能是什么问题?

标签: pythonansible

解决方案


  • 我能够从我的 Active Directory 机器(Windows Server 2019)获取一个 Kerberos 密钥表文件到 ansible 主机(CentOS 7)。

  • 这是我的库存文件和示例剧本。

  • 在这种情况下,参数“ validate_checksum: yes ”至关重要。如果您不使用它,playbook 将因校验和错误而失败。

    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


推荐阅读