Ansible 无法将清单脚本作为清单进行分析

Ansible 无法将清单脚本作为清单进行分析,ansible,ansible-2.x,ansible-inventory,Ansible,Ansible 2.x,Ansible Inventory,我的python脚本动态查询并生成JSON文件,如下所示: { "all": { "hosts": [ "192.158.1.1" ], "vars": { "ansible_become_method": "sudo", "ansible_become": "yes" } } } 但是当我执行下面的命令时 ansible -i script.py -m ping 它发出

我的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
[更新]跑步-根据史蒂夫的建议,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))
[警告]:*无法使用脚本插件解析/etc/ansible/newhost.py:无法解析来自/etc/ansible/script.py:语法的可执行清单脚本结果
加载YAML时出错。在此上下文中不允许映射值错误似乎出现在“”中:第3行第8列,但可能在
文件,具体取决于语法问题。
文件“/usr/lib/python2.7/site packages/ansible/plugins/inventory/script.py”,第125行,在parse中
raise AnsibleError(“未能分析来自{0}:{1}\n{2}的可执行清单脚本结果”。格式(路径,到_native(e),错误))

您的脚本需要是可执行的:
$chmod a+x script.py

请参阅有关使用脚本的文档


您的脚本也必须运行。

您需要像这样运行命令

ansible inventory-i'文件/script.py'的路径--list


有引号很重要。对于我来说,在OsX上这就是问题所在。

您是否在python清单脚本的第一行中添加了这样的标题

#!/path/to/python

设置chmod无法使其工作。是的,我的脚本还有--list作为参数,当我使用--list运行它时,它返回一个JSON格式。提供完整的路径可能?运行
ansible inventory-I script.py--list-vvv
,它将告诉您ansible正在使用哪个插件,并提供有关它所做决定的其他信息。它应该使用
脚本
插件。谢谢@SteveE.的提示。我用更多的输出更新了这个问题。我想我可以使用返回的JSON格式,Ansible可以解析
enable\u plugins=script
而不是Ansible使用的格式。输出清楚地显示了正在尝试的
主机列表
yaml
和其他插件。但是它没有列出出乎意料的
脚本
插件@史蒂文更新了输出。似乎与缓存有关,对此我表示怀疑。