Ansible 2.9 自定义LDAP动态Inventory脚本解析失败排查
自定义LDAP Inventory脚本Ansible解析报错排查
提问内容
基础报错
Failed to parse /get_branches.py with script plugin: Inventory script (/get_branches.py) had an execution error:
我开发了从LDAP AD拉取主机信息的脚本,运行时触发上述报错,已确认以下配置无误:
- 脚本已设置可执行权限
- 脚本路径已在
ansible.cfg中配置:inventory = /path/to/script/get_branches.py
运行环境
当前Ansible版本信息:
ansible 2.9.6 config file = /Users/smohamme/ansible_liquibase/ansible.cfg configured module search path = ['/Users/smohamme/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.7/site-packages/ansible executable location = /usr/local/bin/ansible python version = 3.7.7 (default, Mar 10 2020, 15:43:03) [Clang 11.0.0 (clang-1100.0.33.17)]
执行ansible-config dump --only-changed的输出:
DEFAULT_HOST_LIST(/ansible_liquibase/ansible.cfg) = ['ansible_liquibase/get_branches.py'] DEFAULT_VAULT_PASSWORD_FILE(/ansible_liquibase/ansible.cfg) = /.vault_pass
报错对应Ansible script插件的触发逻辑:脚本执行异常时抛出AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err))
补充排查结果
直接用python3运行脚本时,可正常从LDAP返回格式完全正确的JSON结构库存数据。
执行ansible-inventory -vvvv -i ./get_branches.py --list的详细输出:
ansible-inventory 2.9.6 config file = /Users/smohamme/ansible_liquibase/ansible.cfg configured module search path = ['/Users/smohamme/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.7/site-packages/ansible executable location = /usr/local/bin/ansible-inventory python version = 3.7.7 (default, Mar 10 2020, 15:43:03) [Clang 11.0.0 (clang-1100.0.33.17)] Using /Users/smohamme/ansible_liquibase/ansible.cfg as config file setting up inventory plugins host_list declined parsing /Users/smohamme/ansible_liquibase/get_branches.py as it did not pass its verify_file() method auto declined parsing /Users/smohamme/ansible_liquibase/get_branches.py as it did not pass its verify_file() method yaml declined parsing /Users/smohamme/ansible_liquibase/get_branches.py as it did not pass its verify_file() method toml declined parsing /Users/smohamme/ansible_liquibase/get_branches.py as it did not pass its verify_file() method [WARNING]: * Failed to parse /Users/smohamme/ansible_liquibase/get_branches.py with script plugin: failed to parse executable inventory script results from /Users/smohamme/ansible_liquibase/get_branches.py: Syntax Error while loading YAML. *mapping values are not allowed here* The error appears to be in '<string>': line 3, column 12, but may be elsewhere in the file depending on the exact syntax problem. File "/usr/local/lib/python3.7/site-packages/ansible/inventory/manager.py", line 280, in parse_source plugin.parse(self._inventory, self._loader, source, cache=cache) File "/usr/local/lib/python3.7/site-packages/ansible/plugins/inventory/script.py", line 161, in parse raise AnsibleParserError(to_native(e)) [WARNING]: * Failed to parse /Users/smohamme/ansible_liquibase/get_branches.py with ini plugin: /Users/smohamme/ansible_liquibase/get_branches.py:3: Expected key=value host variable assignment, got: os File "/usr/local/lib/python3.7/site-packages/ansible/inventory/manager.py", line 280, in parse_source plugin.parse(self._inventory, self._loader, source, cache=cache) File "/usr/local/lib/python3.7/site-packages/ansible/plugins/inventory/ini.py", line 138, in parse raise AnsibleParserError(e) [WARNING]: Unable to parse /Users/smohamme/ansible_liquibase/get_branches.py as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available { "_meta": { "hostvars": {} }, "all": { "children": [ "ungrouped" ] } }
手动执行./get_branches.py --list得到的预期输出:
{ "_meta": { "hostvars": { "abc.example.com": { "name": "abc.example.com", "cn": "abc", "osname": "Windows Server 2016 Standard" }, "abc.example.com": { "name": "abc.example.com", "cn": "abc", "osname": "Windows Server 2016 Standard" } } }, "non-production": { "hosts": [ "abc.example.com", "xyz.example.com" ], "vars": {}, "children": [] } }
修复方案
结合日志特征和脚本输出,定位两个核心问题:
- 脚本标准输出混入非JSON内容
日志显示解析过程在第3行触发YAML语法错误,同时ini插件在第3行捕获到以os开头的无效内容,说明Ansible调用脚本时拿到的标准输出并非纯JSON结构,开头夹杂了日志、环境信息类的普通文本。手动执行脚本时如果仅复制终端的后半段JSON输出,很容易忽略前面的日志已经和JSON一同输出到了stdout。
调整方式:修改脚本输出逻辑,所有日志、调试信息全部输出到stderr,标准输出仅保留最终的JSON库存结构。Ansible调用脚本时会将stdout作为库存解析的唯一来源,stderr的内容会被单独作为错误输出处理,不会混入解析流程。 - JSON结构存在语法缺陷
手动执行脚本得到的输出中,_meta.hostvars下存在两个完全重复的键abc.example.com,不符合JSON规范,会导致解析器抛出异常。
调整方式:在脚本中增加LDAP返回数据的去重逻辑,避免生成重复的主机变量键,保证最终输出的JSON符合标准规范,无重复键值对。
额外验证步骤:
- 检查脚本第一行的shebang声明,确保使用正确的Python解释器路径,如
#!/usr/bin/env python3,避免Ansible调用脚本时因解释器版本错误触发输出异常 - 执行
./get_branches.py --list | head -1,确认输出的第一行直接以{开头,无其他多余字符
内容的提问来源于stack exchange,提问作者sameer khan
相关产品推荐
相关产品推荐

