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