首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不可靠的动态库存脚本不会得到评估

不可靠的动态库存脚本不会得到评估
EN

Stack Overflow用户
提问于 2022-10-28 15:30:08
回答 1查看 42关注 0票数 -2

我有一个自定义python脚本,它生成一个与Ansible兼容的库存:

代码语言:javascript
运行
复制
...

if __name__ == "__main__":
    with open("main.log", "a") as f:
        f.write(f"{sys.argv}\n")

    arg_parser = argparse.ArgumentParser(description=__doc__, prog=__file__)
...

当我手动运行它时,一切都很好:

代码语言:javascript
运行
复制
python scripts/main.py --list

main.log文件附加了一个新行:

代码语言:javascript
运行
复制
['scripts/main.py', '--list']

我的ansible.cfg启用了脚本插件:

代码语言:javascript
运行
复制
[inventory]
enable_plugins = script

但是,当我尝试在中使用脚本时,它失败了:

代码语言:javascript
运行
复制
ansible-inventory -i scripts/main.py --list -vvvv
ansible-inventory [core 2.13.2]
  config file = /Users/jaep/code/ic/pocs/poc_ansible_dynamic_inventory/ansible.cfg
  configured module search path = ['/Users/jaep/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/6.2.0/libexec/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/jaep/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible-inventory
  python version = 3.10.6 (main, Aug 11 2022, 13:36:31) [Clang 13.1.6 (clang-1316.0.21.2.5)]
  jinja version = 3.1.2
  libyaml = True
Using /Users/jaep/code/ic/pocs/poc_ansible_dynamic_inventory/ansible.cfg as config file
setting up inventory plugins
script declined parsing /Users/jaep/code/ic/pocs/poc_ansible_dynamic_inventory/scripts/main.py as it did not pass its verify_file() method
[WARNING]: Unable to parse
/Users/jaep/code/ic/pocs/poc_ansible_dynamic_inventory/scripts/main.py as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

真正令人惊讶的是,由于main.log仍然是空的,所以ansible-inventory没有调用脚本。

EN

回答 1

Stack Overflow用户

发布于 2022-10-28 20:03:53

我只是忘了把正确的shebang放在脚本的顶部:

代码语言:javascript
运行
复制
#!/usr/bin/env python3

并授予脚本的执行权限:

代码语言:javascript
运行
复制
chmod +x scripts/main.py
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74237606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档