【发布时间】:2022-08-03 02:27:59
【问题描述】:
我正在尝试从远程主机下载一个大文件,但是我不知道要下载的服务器中文件的名称和格式,我只有 URL。
- 我需要保留下载的文件名。
- 防止重复下载以节省时间。
- 在两种情况下下载或跳过文件时获取本地文件名。
第一个剧本:问题是,以下代码可以正常工作并将图像下载到
/tmp/imagesdir。但是,它每次都会下载图像。 (大约需要 2 分钟)。如何防止重复下载?--- - hosts: localhost tasks: - name: \"Download the Image\" ansible.builtin.get_url: url: \"https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img\" dest: \"/tmp/images/\" mode: \'0755\' register: image_download_stats - name: \"Print the downloaded image name\" debug: msg: \"{{ image_download_stats.dest|basename }}\"第二个剧本:解决方法代码,我想出了:
--- - hosts: localhost tasks: - name: \"Download the image\" shell: wget --show-progress=off --content-disposition -N https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img --force-directories -P /tmp/images/ register: image_download - name: \"Print the name of the image\" debug: msg: \"{% if image_download.stdout_lines |length > 0 %}{{ image_download.stdout_lines |regex_search(\'(?<=‘)(.*?)(?=’)\')}}{%else%}{{image_download.stderr_lines |regex_search(\'(?<=‘)(.*?)(?=’)\')}}{%endif%}\"如果您将两种类型的剧本运行两次,您会注意到第 2 次通过不下载图像并仍然返回文件名来节省时间。对第二部剧本有什么建议吗?第二本剧本严重依赖于
wget的繁重工作,而不是ansible 原生方式。 ansible 用户/专家认为可以使用它吗?第二个对我来说很好用,但是你们认为这种方法会在某些极端情况下失败吗?或者有没有办法让url模块更智能?笔记:我知道从 URL 中提取文件名的
basename技术。但是,有时我的 URL 不是以标准格式来结束文件名。不能相信获取由/字符分隔的 URL 的最后一部分。编辑:我用
archlinux下载了第二个剧本,但它没有用。意思是,它导致重复下载。所以,没有时间节省。欢迎任何建议。例如:wget --show-progress=off --content-disposition -N \'https://gitlab.archlinux.org/archlinux/arch-boxes/-/jobs/69793/artifacts/raw/output/Arch-Linux-x86_64-basic-20220721.69793.qcow2?inline=false\' --force-directories -P /tmp/images我应该放弃跳过下载以节省时间的想法吗?