在Ansible中,如何从安全FTP(SFTP)服务器下载文件?

huangapple go评论71阅读模式
英文:

In Ansible, how to download from a secure FTP (SFTP) server?

问题

以下是您提供的代码的中文翻译部分:

$ ansible --version
ansible 2.10.8

我有一个Linux Ansible服务器和一个Windows目标主机。我正在尝试设置一个任务来从安全FTP(SFTP)服务器下载文件。我只找到了 win_get_url 这个模块,但它没有提到SFTP支持。不过,我还是尝试了以下方式:

- name: 从SFTP下载
  win_get_url:
    url: "sftp://ftp.mycompany.com/path/someFile.exe"
    dest: "C:\\temp"
    url_username: "myUser"
    url_password: "myPw123"

但我收到了以下错误:

任务 [downloader : 下载 sftp://ftp.kmhapub.com/devops/exa-3rdparty/7z/7z2107-x64.exe] *************************************************
任务路径: /path/exa-playbooks/roles/downloader/tasks/download.yml:2
将 ansible.builtin.win_get_url 重定向到 ansible.windows.win_get_url(类型:modules)
使用模块文件 /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_get_url.ps1
启用了管道。
<10.227.x.x> 建立用户Administrator对10.227.x.x端口5986的WINRM连接
执行(通过管道包装器)
完整的跟踪信息如下:
使用带有1个参数的“Create”时出现异常:“未识别URI前缀。”
在行号:213字符:20
+ ... $web_request = Get-AnsibleWindowsWebRequest -Uri $Uri -Module $Module
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AnsibleWindowsWebRequest],MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException,Get-AnsibleWindowsWebRequest

ScriptStackTrace:
在 Get-AnsibleWindowsWebRequest,<无文件>:第211行
在 Invoke-DownloadFile,<无文件>:第213行
在 <ScriptBlock>,<无文件>:第265行

System.Management.Automation.MethodInvocationException:在使用1个参数调用“Create”时出现异常:“未识别URI前缀。” ---&gt; System.NotSupportedException:未识别URI前缀。
   在 System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   在 CallSite.Target(Closure , CallSite , Type , Object )
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   在 System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   在 System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   在 System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   在 System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   在 System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   在 System.Management.Automation.CommandProcessorBase.Complete()
致命: [10.227.x.x]: 失败! =&gt; {
    "changed": false,
    "msg": "在执行模块时发生未处理的异常:在使用1个参数调用“Create”时出现异常:“未识别URI前缀。”"
}

PLAY RECAP ********************************************************************************************************************************
10.227.x.x               : ok=5    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

这个错误是什么意思,可以修复吗?或者是否有不同的模块可以用于从SFTP服务器下载文件?TIA

英文:
$ ansible --version
ansible 2.10.8

I have a Linux Ansible server, and a Windows target host. I'm trying to setup a task to download from a secure FTP (SFTP) server. The only module I can find is win_get_url, but it says nothing about SFTP support. I tried it anyway

- name: Download from SFTP
  win_get_url:
    url: &quot;sftp://ftp.mycompany.com/path/someFile.exe&quot;
    dest: &quot;C:\\temp&quot;
    url_username: &quot;myUser&quot;
    utl_password: &quot;myPw123&quot;

But I get

TASK [downloader : Download sftp://ftp.kmhapub.com/devops/exa-3rdparty/7z/7z2107-x64.exe] *************************************************
task path: /path/exa-playbooks/roles/downloader/tasks/download.yml:2
redirecting (type: modules) ansible.builtin.win_get_url to ansible.windows.win_get_url
Using module file /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_get_url.ps1
Pipelining is enabled.
&lt;10.227.x.x&gt; ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO 10.227.x.x
EXEC (via pipeline wrapper)
The full traceback is:
Exception calling &quot;Create&quot; with &quot;1&quot; argument(s): &quot;The URI prefix is not recognized.&quot;
At line:213 char:20
+ ... $web_request = Get-AnsibleWindowsWebRequest -Uri $Uri -Module $Module
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AnsibleWindowsWebRequest], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException,Get-AnsibleWindowsWebRequest

ScriptStackTrace:
at Get-AnsibleWindowsWebRequest, &lt;No file&gt;: line 211
at Invoke-DownloadFile, &lt;No file&gt;: line 213
at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 265

System.Management.Automation.MethodInvocationException: Exception calling &quot;Create&quot; with &quot;1&quot; argument(s): &quot;The URI prefix is not recognized.&quot; ---&gt; System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at CallSite.Target(Closure , CallSite , Type , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
fatal: [10.227.x.x]: FAILED! =&gt; {
    &quot;changed&quot;: false,
    &quot;msg&quot;: &quot;Unhandled exception while executing module: Exception calling \&quot;Create\&quot; with \&quot;1\&quot; argument(s): \&quot;The URI prefix is not recognized.\&quot;&quot;
}

PLAY RECAP ********************************************************************************************************************************
10.227.x.x               : ok=5    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

What does the error mean, and can it be fixed? Or is there a different module for downloading from an SFTP server? TIA

答案1

得分: 1

已经告诉您问题是什么,“URI前缀未被识别”

当您查看win_get_url文档时,您会发现SFTP未在支持的协议(HTTP、HTTPS、FTP)中提到,可能是因为该模块不支持它。如果您查看代码,它们还会检查URI是否属于这些协议之一。所以看起来确实不受支持。

此外,如果您希望屏蔽服务器的IP地址,应该在所有代码中执行,而不仅仅在最后 在Ansible中,如何从安全FTP(SFTP)服务器下载文件?

英文:

It already tells you what the Problem is "The URI prefix is not recognized"

When you take a look at the documentation of win_get_url you see, that SFTP is not mentioned in the supported Protocols (HTTP, HTTPS, FTP), probably because it is not supported by that module. if you look into the code, they also check if the URI ist one of those. So it seems like, it is really not supported

Also, if you want to censor the IP-Adresse of you Server, you should do that in all of your code, not just at the end 在Ansible中,如何从安全FTP(SFTP)服务器下载文件?

huangapple
  • 本文由 发表于 2023年3月4日 06:28:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定