You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible dig插件报错:参数'text'类型错误(预期str,得到AnsibleUnicode)

Ansible dig lookup插件类型错误排查与解决

环境信息

  • 运行环境:Alpine Docker容器
  • Ansible版本:v2.14.5
  • 已安装组件:python3(v3.11.6)、py3-dnspython、net-tools、bind-tools、bind-libs

测试Playbook

---
- hosts: localhost                                                                                                                           
  tasks:                                                                                                                                           
  - name: Simple A record (IPV4 address) lookup                                                                                     
    debug: msg="{{ lookup('dig', 'www.google.com.')}}" 

执行命令与错误信息

执行命令:

ansible-playbook -i localhost test.yml

错误输出:

TASK [Simple A record (IPV4 address) lookup] ****************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'dig'. 
Error was a <class 'ansible.errors.AnsibleError'>, original message: dns lookup illegal CLASS: Argument 'text' has incorrect type (expected str, got AnsibleUnicode). 
dns lookup illegal CLASS: Argument 'text' has incorrect type (expected str, got AnsibleUnicode)"}

已尝试操作

使用pip安装dnspython,问题未解决。

解决方法

方法1:类型转换

在lookup调用中将域名参数转换为字符串,规避AnsibleUnicode类型冲突:

---
- hosts: localhost                                                                                                                           
  tasks:                                                                                                                                           
  - name: Simple A record (IPV4 address) lookup                                                                                     
    debug: msg="{{ lookup('dig', str('www.google.com.'))}}" 

方法2:调整dnspython版本

Alpine仓库中的py3-dnspython版本可能与Ansible 2.14.5存在兼容性问题,可卸载系统包后用pip安装指定兼容版本:

# 卸载系统的py3-dnspython
apk del py3-dnspython
# 安装兼容版本(如dnspython 2.3.0)
pip install dnspython==2.3.0

内容的提问来源于stack exchange,提问作者rcff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 20:55:53