Ansible动态资源清册“([Errno 2]没有此类文件或目录:”

2024-05-29 05:03:16 发布

您现在位置:Python中文网/ 问答频道 /正文

情况:我试图在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——列出主机

[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()

Tags: keypytestselfhostargsansiblelist

热门问题