问题描述

在使用Ansible管理Windows目标时,可能会遇到“模块失败”(Module Failure)的致命错误。这种错误通常是由于使用了不兼容的模块或目标主机上缺少必要的组件导致的。本文将介绍如何复现、排查和修复Windows目标上的模块失败错误,并提供一个下载文件的示例。

错误复现

以下是一个Ansible playbook示例,用于复现Windows目标上的模块失败错误:

文件:get_url_error.yml

---
- name: win_get_url 模块演示
  hosts: all
  become: false
  vars:
    myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
    mydest: 'C:\Users\vagrant\Desktop\ansible-2.9.25.tar.gz'
  tasks:
    - name: 下载文件
      ansible.builtin.get_url:
        url: "{{ myurl }}"
        dest: "{{ mydest }}"

错误执行

运行playbook时,可能会出现以下错误:

$ ansible-playbook -i inventory troubleshooting/get_url_error.yml
PLAY [win_get_url 模块演示] **************************************************

TASK [Gathering Facts] ******************************************************
ok: [WindowsServer]

TASK [下载文件] *************************************************************
[WARNING]: No python interpreters found for host WindowsServer (tried ['python3.10', 'python3.9', 'python3.8', 'python3.7', 'python3.6', 'python3.5', '/usr/bin/python3', '/usr/libexec/platform-python', 'python2.7', 'python2.6', '/usr/bin/python', 'python'])
fatal: [WindowsServer]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansible_main():\r\n+                     ~\r\nAn expression was expected after '('.\r\n..."}
PLAY RECAP ******************************************************************
WindowsServer              : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

错误原因

  1. 使用了不兼容的模块:ansible.builtin.get_url模块是为Linux系统设计的,不适用于Windows目标。
  2. 缺少Python解释器:Windows目标上没有安装Python,或者Ansible无法找到合适的Python解释器。
  3. 模块调用错误:模块的调用方式不正确,导致无法正常执行。

解决方案

修复playbook,使用适用于Windows的ansible.windows.win_get_url模块。

文件:get_url_fix.yml

---
- name: win_get_url 模块演示
  hosts: all
  become: false
  vars:
    myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
    mydest: 'C:\Users\vagrant\Desktop\ansible-2.9.25.tar.gz'
  tasks:
    - name: 下载文件
      ansible.windows.win_get_url:
        url: "{{ myurl }}"
        dest: "{{ mydest }}"

修复执行

运行修复后的playbook:

$ ansible-playbook -i inventory troubleshooting/get_url_fix.yml
PLAY [win_get_url 模块演示] **************************************************

TASK [Gathering Facts] ******************************************************
ok: [WindowsServer]

TASK [下载文件] *************************************************************
changed: [WindowsServer]

PLAY RECAP ******************************************************************
WindowsServer              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

总结

  1. 使用正确的模块:对于Windows目标,确保使用ansible.windows.win_get_url模块,而不是ansible.builtin.get_url。
  2. 检查Python环境:确保Windows目标上安装了Python,并且Ansible能够找到合适的Python解释器。
  3. 验证模块调用:检查模块的调用方式是否正确,确保参数和路径格式符合要求。
  4. 测试playbook:在修复后重新运行playbook,验证问题是否解决。

通过以上步骤,您可以快速解决Windows目标上的模块失败错误,并成功运行Ansible playbook。