Ansible get_url 仅在更改时下载文件

mar*_*ald 1 ansible

我只想从 yaml 文件 download.yaml 下载文件(仅当它们发生更改时)。

我已经找到了这个/sf/answers/1586792441/但似乎 ansible 仍然下载整个文件,即使它没有改变

playbook.yaml 中的任务:

tasks:
  - name: Download File
    get_url:
      url="{{ item.value.url }}"
      dest="{{ item.value.dest }}{{item.value.file }}"
    loop: "{{ lookup('dict', dl) }}"
Run Code Online (Sandbox Code Playgroud)

下载.yaml:

dl:
  4mlinux:
    dest: /var/www/html/live/asset-mirror/releases/download/36.1-39ff9a19/
    file: vmlinuz
    url: https://github.com/netbootxyz/asset-mirror/releases/download/36.1-39ff9a19/vmlinuz
  4mlinux-antivirus:
    dest: /var/www/html/live/asset-mirror/releases/download/37.0-0.103.2-ba9e3ece/
    file: vmlinuz
    url: https://github.com/netbootxyz/asset-mirror/releases/download/37.0-0.103.2-ba9e3ece/vmlinuz
  4mlinux-bakandimg:
    dest: /var/www/html/live/asset-mirror/releases/download/36.0-262576bf/
    file: vmlinuz
    url: https://github.com/netbootxyz/asset-mirror/releases/download/36.0-262576bf/vmlinuz
Run Code Online (Sandbox Code Playgroud)

编辑:
为了让我的问题更清楚。
Github 不提供版本的哈希值,请参阅此处Where to get MD5 hashes from a GitHub release?

我想避免自己计算校验和,因为校验和download.yaml是从另一个 yaml 文件生成的。

get_url如果模块仅检查文件的大小就足够了。

Edit2:
Ansible 仍然下载整个文件。以下是详细输出示例:

ok: [127.0.0.1] => (item={'key': 'regolith-current', 'value': {'dest': '/var/www/html/live/ubuntu-squash/releases/download/1.2-0f285b8f/', 'file': 'filesystem.squashfs', 'url': 'https://github.com/netbootxyz/ubuntu-squash/releases/download/1.2-0f285b8f/filesystem.squashfs'}}) => {
"ansible_loop_var": "item", 
"changed": false, 
"checksum_dest": "f81cc8c2e941746ec359d93ab4ce6b14f75af225", 
"checksum_src": "f81cc8c2e941746ec359d93ab4ce6b14f75af225", 
"dest": "/var/www/html/live/ubuntu-squash/releases/download/1.2-0f285b8f/filesystem.squashfs", 
"elapsed": 94, 
"gid": 0, 
"group": "root", 
"item": {
  "key": "regolith-current", 
  "value": {
    "dest": "/var/www/html/live/ubuntu-squash/releases/download/1.2-0f285b8f/", 
    "file": "filesystem.squashfs", 
    "url": "https://github.com/netbootxyz/ubuntu-squash/releases/download/1.2-0f285b8f/filesystem.squashfs"
  }
}, 
"md5sum": "285b019b7a2d91c5400b6c9991de2a60", 
"mode": "0644", 
"msg": "OK (2097151999 bytes)", 
"owner": "root", 
"size": 2097151999, 
"src": "/root/.ansible/tmp/ansible-tmp-1624984184.2966168-188394881121369/tmphilspaat", 
"state": "file", 
"status_code": 200, 
"uid": 0, 
"url": "https://github.com/netbootxyz/ubuntu-squash/releases/download/1.2-0f285b8f/filesystem.squashfs"}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的 "src": "/root/.ansible/tmp/ansible-tmp-1624984184.2966168-188394881121369/tmphilspaat""elapsed": 94

toy*_*ian 6

在本例中,问题是由get_url模块中的错误引起的。更新到最新版本的ansible修复了它。

有关get_url及其参数的一些信息:
每次都会下载您的文件,因为它dest是一个目录。
文档中:

如果dest是目录,则始终会下载该文件(无论选项如何force),但仅在内容发生更改时才进行替换。

dest如果您不希望 ansible 下载该文件,则必须包含该文件的名称。
更好的选择是另外提供文件的校验和。这将
a) 验证下载的工件,因此您将始终确保获得所需的内容,
b) 仅在文件与提供的校验和不匹配时才下载文件dest(例如,如果它在主机上发生更改)。
查看校验和参数的文档以获取更多信息。

顺便说一句,您的代码无效yaml。这对我有用:

- hosts: all
  connection: local
  become: true
  vars:
    dl:
      4mlinux:
        dest: /tmp/test/36.1-39ff9a19/
        file: vmlinuz
        url: https://github.com/netbootxyz/asset-mirror/releases/download/36.1-39ff9a19/vmlinuz
      4mlinux-antivirus:
        dest: /tmp/test/37.0-0.103.2-ba9e3ece/
        file: vmlinuz
        url: https://github.com/netbootxyz/asset-mirror/releases/download/37.0-0.103.2-ba9e3ece/vmlinuz
      4mlinux-bakandimg:
        dest: /tmp/test/36.0-262576bf/
        file: vmlinuz
        url: https://github.com/netbootxyz/asset-mirror/releases/download/36.0-262576bf/vmlinuz
  tasks:
    - name: Download File
      get_url:
        url: "{{ item.value.url }}"
        dest: "{{ item.value.dest }}{{ item.value.file }}"
      loop: "{{ lookup('dict', dl) }}"
Run Code Online (Sandbox Code Playgroud)