【问题标题】:Unable to parse inventory script as an inventory无法将库存脚本解析为库存
【发布时间】: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_listyaml 和其他插件正在尝试中。但是它没有列出意外的script 插件。 The defaults are here
  • @SteveE.updated 输出。似乎与我怀疑的缓存有关。

标签: ansible ansible-2.x ansible-inventory


【解决方案1】:

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

#!/path/to/python

【讨论】:

    【解决方案2】:

    你需要像这样运行命令

    ansible-inventory -i 'path-to-the-file/script.py' --list
    

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

    【讨论】:

      【解决方案3】:

      您的脚本必须是可执行的:

      $ chmod a+x script.py
      

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

      您的脚本还必须accept certain arguments

      【讨论】:

      • 设置 chmod 不会使其工作。是的,我的脚本也有 --list 作为参数,当我使用 --list 运行它时,它会返回 JSON 格式。
      • 是否提供完整路径?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2021-12-23
      • 1970-01-01
      • 2021-11-24
      • 2021-10-24
      相关资源
      最近更新 更多