【发布时间】:2019-03-01 00:09:43
【问题描述】:
我的python脚本动态查询生成JSON文件如下:
{
"all": {
"hosts": [
"192.158.1.1"
],
"vars": {
"ansible_become_method": "sudo",
"ansible_become": "yes"
}
}
}
但是当我执行下面的命令时不知何故
ansible -i script.py -m ping
它会发出以下警告信息
- [警告] 无法将 /etc/ansible/script.py 解析为清单源
- [警告] 未解析任何清单,只有隐式本地主机可用。
- [警告] 提供的主机列表为空,只有 localhost 可用。请注意,隐式 localhost 不匹配 'all'
我不知道自己遗漏了什么或错了什么,因为我是 Ansible 动态库存的新手。
当我运行ansible-inventory -i script.py --list 时,我得到了以下响应
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {}
}
我的 Ansible 版本是 2.7.7
库存插件是脚本
enable_plugins = script
[已更新]根据 Steve 的建议运行 -v 会给我更多的帮助
[WARNING]: * Failed to parse /etc/ansible/newhost.py with script plugin: failed to parse executable inventory script results from /etc/ansible/script.py: Syntax
Error while loading YAML. mapping values are not allowed in this context The error appears to have been in '<string>': line 3, column 8, but may be elsewhere in the
file depending on the exact syntax problem.
File "/usr/lib/python2.7/site-packages/ansible/plugins/inventory/script.py", line 125, in parse
raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err))
【问题讨论】:
-
运行
ansible-inventory -i script.py --list -vvv,它会告诉你 Ansible 正在使用哪个插件,并提供有关它做出的决定的附加信息。它应该使用script插件。 -
感谢@SteveE.for 的提示。我用更多的输出更新了这个问题。我认为我可以使用 Ansible 能够解析的返回 JSON 格式
-
enable_plugins = script不能是您的 Ansible 使用的。输出清楚地显示host_list、yaml和其他插件正在尝试中。但是它没有列出意外的script插件。 The defaults are here -
@SteveE.updated 输出。似乎与我怀疑的缓存有关。
标签: ansible ansible-2.x ansible-inventory