在 Ansible 中,使用 ansible.builtin.unarchive 模块从互联网下载内容时,可能会遇到“下载失败”错误。这种错误通常是由于以下原因之一引起的:

  1. 拼写错误的 URL:提供的 URL 拼写错误或指向不存在的资源。
  2. 资源已移动或删除:目标网站上的内容已被移动或删除,导致 HTTP 404 错误。
  3. 网络问题:网络连接不稳定或无法访问目标 URL。

错误示例

以下是一个会导致“下载失败”错误的 Ansible Playbook 示例:

---
- name: unarchive module demo
  hosts: all
  become: false
  vars:
    myurl: "https://github.com/lucab85/ansible-pilot/archive/refs/master.zip"
  tasks:
    - name: extract archive
      ansible.builtin.unarchive:
        src: "{{ myurl }}"
        dest: "/home/devops/"
        remote_src: true
        validate_certs: true

错误信息

运行上述 Playbook 时,可能会遇到以下错误:

$ ansible-playbook -i demo/inventory troubleshooting/failuredownloading_error.yml
PLAY [unarchive module demo] **************************************************

TASK [Gathering Facts] **************************************************
ok: [demo.example.com]

TASK [extract archive] **************************************************
fatal: [demo.example.com]: FAILED! => {"changed": false, "msg": "Failed to download https://github.com/lucab85/ansible-pilot/archive/refs/master.zip, HTTP error 404"}

PLAY RECAP *******************************************************************
demo.example.com           : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

问题原因

  1. 拼写错误的 URL:提供的 URL 拼写错误或指向不存在的资源。
  2. 资源已移动或删除:目标网站上的内容已被移动或删除,导致 HTTP 404 错误。

解决方法

  1. 检查 URL 的合法性:在浏览器中访问 URL,确保其指向的资源存在。
  2. 修正 URL:如果 URL 拼写错误或资源已移动,修正 URL 以指向正确的资源。
  3. 重试下载:在修正 URL 后,重新运行 Playbook。

修复后的 Playbook 如下所示:

---
- name: unarchive module demo
  hosts: all
  become: false
  vars:
    myurl: "https://github.com/lucab85/ansible-pilot/archive/refs/heads/master.zip"
  tasks:
    - name: extract archive
      ansible.builtin.unarchive:
        src: "{{ myurl }}"
        dest: "/home/devops/"
        remote_src: true
        validate_certs: true

修复步骤

  1. 检查 URL 的合法性:在浏览器中访问 URL,确保其指向的资源存在。
  2. 修正 URL:如果 URL 拼写错误或资源已移动,修正 URL 以指向正确的资源。
  3. 重新运行 Playbook:验证修复是否有效。

验证修复

修复问题后,重新运行 Playbook:

$ ansible-playbook -i demo/inventory troubleshooting/failuredownloading_fix.yml
PLAY [unarchive module demo] **************************************************

TASK [Gathering Facts] **************************************************
ok: [demo.example.com]

TASK [extract archive] **************************************************
changed: [demo.example.com]

PLAY RECAP *******************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

总结

Ansible 中的“下载失败”错误通常是由于以下原因导致的:

  1. 拼写错误的 URL:提供的 URL 拼写错误或指向不存在的资源。
  2. 资源已移动或删除:目标网站上的内容已被移动或删除,导致 HTTP 404 错误。

通过以下步骤可以有效解决这种错误:

  1. 检查 URL 的合法性:在浏览器中访问 URL,确保其指向的资源存在。
  2. 修正 URL:如果 URL 拼写错误或资源已移动,修正 URL 以指向正确的资源。
  3. 重新运行 Playbook:验证修复是否有效。

如果问题仍未解决,可以参考 Ansible 的 官方文档或社区支持获取更多帮助。