Couldn't execute inventory file

Hey,

I’m unable to run the inventory file for my project. It’s throwing up few errors. please help me out.

ansible-inventory2.9.27.post0
config file = / runner/project/ansible.cfg
configured module search path = [‘/home/runner/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible-inventorv
python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3) ]
Using /runner/project/ansible.cfg as config file
Reading vault password file: /runner/project/rheva/temp _vault. txt
setting up inventory plugins
host_list declined parsing /runner/project/rheva/test.py as it did not pass its verify file() method
script declined parsing /runner/project/rheva/test.py as it did not pass its verify_file() method
auto declined parsing /runner/project/rheva/test.py as it did not pass its verify_file() method
yaml declined parsing /runner/project/rheva/test.py as it did not pass its verify_file() method
toml declined parsing / runner/project/rheva/test.py as it did not pass its verify_file() method
[WARNING]: « Failed to parse /runner/project/rheva/test.py with ini plugin:
/runner/project/rheva/test.py:1: Expected key=value host variable assignment,
got: psycopg2
File “/usr/local/lib/python3.8/site-packages/ansible/inventory/manager.py”,line 280, in parse source
plugin. parse(self._inventory, self._loader, source, cache=cache)
File “/usr/local/lib/python3.8/site-packages/ansible/plugins/inventory/ini.py”,line 138, in parse
raise AnsibleParserError(e)
[WARNING]: Unable to parse /runner/proiect/rheva/test.py as an inventory source
ERROR! No inventory was parsed, please check your configuration and options.

Sounds like test.py is intended to be run as a script, but was not marked as executable. Make it executable, and that should solve the problem.

I have created another executable file but even that doesn’t work and throws out following errors.

ansible-inventory2.9.27.post0
config file = / runner/project/ansible.cfg
configured module search path = [‘/home/runner/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible-inventorv
python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3) ]

Using /runner/project/ansible.cfg as config file
host_list declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
auto declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
yaml declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
toml declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
[WARNING]: * Failed to parse /runner/project/rheva/tower_inventory.py with
script plugin: problem running /runner/project/rheva/tower inventory.py --list
([Errno 13] Permission denied: ‘/runner/project/rheva/tower_ inventory.py’)
File “/usr/local/lib/python3.8/site-packages/ansible/inventory/manager.py”, line 290, in parse source
plugin.parse(self._inventory, self.loader, source, cache=cache)
File “/ur/local/lib/python3.8/site-packages/ansible/plugins/inventory/script.py”, line 169, in parse
raise AnsibleParserError(to_native(e))
[WARNING]: * Failed to parse /runner/project/rheva/tower inventory.py with ini
plugin: /runner/project/rheva/tower
inventory.py:3: Expected key=value host
variable assignment, got: os
File “/usr/local/lib/python3.8/site-packages/ansible/inventory/manager.py”, line 290, in parse_source
plugin.parse(self._inventory, self.loader, source, cache=cache)
File “/usr/local/lib/python3.8/site-packages/ansible/plugins/inventory/ini.py”,line136,inparse
raise AnsibleParserError(e)
[WARNING]: Unable to parse /runner/project/rheva/tower
inventory.py as an
inventory source
ERROR! No inventory was parsed, please check your configuration and options.

The script plugin reports:

([Errno 13] Permission denied: ‘/runner/project/rheva/tower_ inventory.py’)

This indicates that the file is not actually marked as executable.

Hello Matt,

I’ve given the execution permission again and retried but it still throws out few errors. Please resolve the issue. I’m also posting my tower inventory python script. Please help me out. Thanks a lot!

Below is the errors I received.

[ansible-inventory2.9.27.post0
config file = / runner/project/ansible.cfg
configured module search path = [‘/home/runner/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
executable location = /usr/local/bin/ansible-inventorv
python version = 3.8.12 (default, Sep 21 2021, 00:10:52) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3) ]

Using /runner/project/ansible.cfg as config file

host_list declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
auto declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
yaml declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method
toml declined parsing /runner/project/rheva/tower_inventory.py as it did not pass its verify_file() method

[WARNING]: * Failed to parse /runner/project/rheva/tower_inventory. py with
script plugin: Inventory script (/runner/project/rheva/tower_inventory.py) had
an execution error: Traceback (most recent
call last):
File “/runner/project/rheva/tower_inventory.py”, line 58, in TowerInventory()
File “/runner/project/rheva/tower_inventory.py”, line 16, in init.
self.inventory = self. tower_ inventory()
File “/runner/project/rheva/tower_inventory.py”,
Line 29. in tower inventory
tmp_inventory = ({ tower _inventory | to_nice_json }} NameError: name
"tower inventory’ is not defined
File “/sr/local/lib/python3.8/site-packages/ansible/inventory/manager.py”, line 280, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File “/usr/local/lib/python3.8/site-packages/ansible/plugins/inventory/script.py”, line 161, in parse
raise AnsibleParserError(to_native(e))
[WARNING]: * Failed to parse /runner/project/rheva/tower inventory.py with ini
plugin: /runner/project/rheva/tower_inventory.py:3: Expected key=value host
variable assignment, got: os
File “/usr/local/lib/python3.8/site-packages/ansible/inventory/manager.py”, line 280, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File “/usr/local/lib/python3.8/site-packages/ansible/plugins/inventory/ini.py”,line138,inparse
raise AnsibleParserError(e)
[WARNING]: Unable to parse /runner/project/rheva/tower inventory.py as an
inventory source
ERROR! No inventory was parsed, please check your configuration and options.]

The tower_inventory.py script is shown below:

import os

import sys

import argparse

import json

class TowerInventory(object):

def init(self):

self.inventory = {}

self.read_cli_args()

# Called with --list.

if self.args.list:

self.inventory = self.tower_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 an empty inventory.

else:

self.inventory = self.empty_inventory()

print (json.dumps(self.inventory));

# Tower inventory for testing.

def tower_inventory(self):

tmp_inventory = {{ tower_inventory | to_nice_json }}

tmp_inventory = remove_empty_elements(tmp_inventory)

return tmp_inventory

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

# Remove empty groups and elements from dict

def remove_empty_elements(d):

“”“recursively remove empty lists, empty dicts, or None elements from a dictionary”“”

def empty(x):

return x is None or x == {} or x == [] or x == [“”]

if not isinstance(d, (dict, list)):

return d

elif isinstance(d, list):

return [v for v in (remove_empty_elements(v) for v in d) if not empty(v)]

else:

return {k: v for k, v in ((k, remove_empty_elements(v)) for k, v in list(d.items())) if not empty(v)}

# Get the inventory.

TowerInventory()