问题描述
在使用Ansible的win_user模块时,可能会遇到“Unhandled exception”错误,这通常是因为在模块执行过程中发生了意外情况,例如密码不符合策略要求或系统配置问题。本文将通过实际演示介绍如何排查和解决该错误。
错误复现
以下是一个Ansible Playbook示例,用于复现“Unhandled exception”错误:
文件:windows_user_add_error.yml
---
- name: windows user add
hosts: all
vars:
usr_name: 'example'
usr_password: 'password'
tasks:
- name: create local user
ansible.windows.win_user:
name: "{{ usr_name }}"
password: "{{ usr_password }}"
错误执行
运行Playbook时,可能会出现以下错误:
$ ansible-playbook -i virtualmachines/win/inventory troubleshooting/windows_user_add_error.yml
PLAY [windows user add] ******************************************************
TASK [Gathering Facts] *******************************************************
ok: [WindowsServer]
TASK [create local user] ******************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: at <ScriptBlock>, <No file>: line 260
fatal: [WindowsServer]: FAILED! => {"changed": false, "msg": "Unhandled exception while executing module: Exception calling \"SetPassword\" with \"1\" argument(s): \"The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.\r\n\""}
PLAY RECAP *******************************************************************
WindowsServer : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
错误原因
- 密码不符合策略要求:Windows系统对密码有复杂性要求,例如最小长度、复杂性(包含数字、字母、特殊字符)和历史记录要求。
- 系统配置问题:某些系统配置可能导致模块执行失败。
解决方案
修改Playbook,确保密码符合Windows的密码策略要求。例如,使用一个更复杂的密码:
文件:windows_user_add_fix.yml
---
- name: windows user add
hosts: all
vars:
usr_name: 'example'
usr_password: 'NRns@bOFJNyX' # 使用更复杂的密码
tasks:
- name: create local user
ansible.windows.win_user:
name: "{{ usr_name }}"
password: "{{ usr_password }}"
修复执行
运行修复后的Playbook:
$ ansible-playbook -i virtualmachines/win/inventory troubleshooting/windows_user_add_fix.yml
PLAY [windows user add] ******************************************************
TASK [Gathering Facts] *******************************************************
ok: [WindowsServer]
TASK [create local user] ******************************************************
changed: [WindowsServer]
PLAY RECAP *******************************************************************
WindowsServer : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
总结
- 检查密码策略:确保提供的密码符合Windows的密码策略要求。
- 验证系统配置:如果问题仍然存在,检查目标主机的系统配置,确保没有其他限制。
- 启用详细日志:使用
-vvv选项运行Playbook,以获取更多错误信息,帮助进一步排查问题。
通过以上步骤,您可以快速解决执行win_user模块时出现的“Unhandled exception”错误,并确保Playbook顺利执行。