首页 > 解决方案 > 无法将库存脚本解析为库存

问题描述

我的 python 脚本动态查询并生成一个 JSON 文件,如下所示:

{
    "all": {
      "hosts": [
        "192.158.1.1"
      ], 
      "vars": {
        "ansible_become_method": "sudo", 
        "ansible_become": "yes"
      }
    }
  }

但是当我执行下面的命令时

ansible -i script.py -m ping 

它发出以下警告信息

我不知道我缺少什么或错了,因为我是 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))

标签: ansibleansible-2.xansible-inventory

解决方案


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

#!/path/to/python

推荐阅读