Search code examples
pythonansiblecentosvagrant

Ansible Dynamic Inventory - "([Errno 2] No such file or directory:"


Situation: I am getting an issue when trying to load a basic dynamic inventory python file in Ansible.
Background: When I perform python test.py it will output the list however in ansible it fails
Assessment: I have tried lots of other example files and none have worked, this is the simplest i could make it
Recommendation: Can someone please point me in the right direction? is this to do with the 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"}}}}

ISSUE:

Command used: 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()

Solution

  • Invisible Line endings!

    I ended up installing dos2unix and converting the files there were CR and LF tags at the end of each line.

    Remove the CF lines with dos2unix and it working first time.

    https://i.sstatic.net/Whll6.jpg