Ansible动态清单 - "([Errno 2] 没有这样的文件或目录:"

3

情况:我在尝试在Ansible中加载一个基本的动态清单python文件时遇到了问题。
背景:当我执行python test.py时,它会输出列表,但在ansible中失败了。
评估:我尝试了很多其他示例文件,但都没有成功,这是我能做到的最简单的。
建议:有人能指点我正确的方向吗?这是否与ansible.cfg有关?

vagrant@cd98f180bc88 /vagrant/adc-cmdb-inventory/enviroments $ python test.py --list
{"all": {"hosts": ["192.168.28.71", "192.168.28.72"], "vars": {"ansible_python_interpreter": "/usr/bin/python3", "ansible_ssh_private_key_file": "~/.vagrant.d/insecure_private_key", "example_variable": "value", "ansible_user": "vagrant"}}, "_me
ta": {"hostvars": {"192.168.28.72": {"host_specific_var": "bar"}, "192.168.28.71": {"host_specific_var": "foo"}}}}


问题:
命令使用:
ansible -i test.py all -m ping --list-host
[WARNING]:  * Failed to parse /vagrant/adc-cmdb-inventory/enviroments/test.py with script plugin: problem running /vagrant/adc-cmdb-inventory/enviroments/test.py --list ([Errno 2] No such file or directory: '/vagrant/adc-cmdb-
inventory/enviroments/test.py': '/vagrant/adc-cmdb-inventory/enviroments/test.py')

[WARNING]:  * Failed to parse /vagrant/adc-cmdb-inventory/enviroments/test.py with ini plugin: /vagrant/adc-cmdb-inventory/enviroments/test.py:4: Expected key=value host variable assignment, got: os

[WARNING]: Unable to parse /vagrant/adc-cmdb-inventory/enviroments/test.py as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

  hosts (0):


test.py

#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import sys
import argparse
import json

class ExampleInventory(object):

    def __init__(self):
        self.inventory = {}
        self.read_cli_args()

        # Called with `--list`.
        if self.args.list:
            self.inventory = self.example_inventory()
        # Called with `--host [hostname]`.
        elif self.args.host:
            # Not implemented, since we return _meta info `--list`.
            self.inventory = self.empty_inventory()
        # If no groups or vars are present, return empty inventory.
        else:
            self.inventory = self.empty_inventory()

        print(json.dumps(self.inventory))

    # Example inventory for testing.
    def example_inventory(self):
        return {
            'all': {
                'hosts': ['192.168.28.71', '192.168.28.72'],
                'vars': {
                    'ansible_user': 'vagrant',
                    'ansible_ssh_private_key_file':
                        '~/.vagrant.d/insecure_private_key',
                    'ansible_python_interpreter':
                        '/usr/bin/python3',
                    'example_variable': 'value'
                }
            },
            '_meta': {
                'hostvars': {
                    '192.168.28.71': {
                        'host_specific_var': 'foo'
                    },
                    '192.168.28.72': {
                        'host_specific_var': 'bar'
                    }
                }
            }
        }

    # Empty inventory for testing.
    def empty_inventory(self):
        return {'_meta': {'hostvars': {}}}

    # Read the command line args passed to the script.
    def read_cli_args(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--list', action = 'store_true')
        parser.add_argument('--host', action = 'store')
        self.args = parser.parse_args()

# Get the inventory.
ExampleInventory()

2
chmod 755 test.py 和/或 /usr/bin/python 是否存在? - undefined
已添加权限(同样的错误),是的,/usr/bin/python 存在。 - undefined
1
不知道该告诉你什么,它在我的机器上运行正常:chmod 755 test.py && ansible-inventory -i test.py --list 愉快地产生了 {"_meta":{"hostvars":{"192.168.28.71" 和其他内容;将 test.py 编辑为 #!/nope/404 的 shebang,然后导致 ansible-inventory 发出与你发布的相同错误,所以我们就在这里。 - undefined
我确认在我的端上也按预期工作。 - undefined
谢谢,我可以确认我已经完全构建了另一个CentOS版本,并且它可以正常工作。问题是我需要在当前环境中找到解决方案。我不认为这与Ansible或CentOS有关。 - undefined
1个回答

3

看不见的行尾符!

我最终安装了dos2unix并转换了文件,每行结尾都有CR和LF标记。

使用dos2unix删除CF行,第一次就可以正常工作。

https://imgur.com/AN8ACWC


在看到这篇帖子之前,我花了过去几天的时间试图弄清楚为什么这种情况会发生在我身上! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接