Bug 271198

Summary: sysutils/ansible: probably missing py-toml dependency
Product: Ports & Packages Reporter: Mikael Urankar <mikael>
Component: Individual Port(s)Assignee: Mateusz Piotrowski <0mp>
Status: Closed FIXED    
Severity: Affects Only Me CC: matthew
Priority: --- Flags: bugzilla: maintainer-feedback? (0mp)
Version: Latest   
Hardware: Any   
OS: Any   

Description Mikael Urankar freebsd_committer freebsd_triage 2023-05-02 11:53:52 UTC
I'm hitting the following issue:
AttributeError: module 'toml' has no attribute 'loads'

full backtrace:

ansible-playbook [core 2.14.3]
  config file = /usr/local/etc/ansible/ansible.cfg
  configured module search path = ['/home/toto/.ansible/plugins/modules', '/usr/local/share/py39-ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/toto/.ansible/collections:/usr/local/share/py39-ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.9.16 (main, Feb 28 2023, 01:31:45) [Clang 15.0.7 (https://github.com/llvm/llvm-project.git llvmorg-15.0.7-0-g8dfdc (/usr/local/bin/python3.9)
  jinja version = 3.1.2
  libyaml = True
Using /usr/local/etc/ansible/ansible.cfg as config file
ERROR! Unexpected Exception, this is probably a bug: module 'toml' has no attribute 'loads'
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/ansible/cli/__init__.py", line 647, in cli_executor
    exit_code = cli.run()
  File "/usr/local/lib/python3.9/site-packages/ansible/cli/playbook.py", line 124, in run
    loader, inventory, variable_manager = self._play_prereqs()
  File "/usr/local/lib/python3.9/site-packages/ansible/cli/__init__.py", line 552, in _play_prereqs
    inventory = InventoryManager(loader=loader, sources=options['inventory'], cache=(not options.get('flush_cache')))
  File "/usr/local/lib/python3.9/site-packages/ansible/inventory/manager.py", line 167, in __init__
    self.parse_sources(cache=cache)
  File "/usr/local/lib/python3.9/site-packages/ansible/inventory/manager.py", line 228, in parse_sources
    parse = self.parse_source(source, cache=cache)
  File "/usr/local/lib/python3.9/site-packages/ansible/inventory/manager.py", line 279, in parse_source
    for plugin in self._fetch_inventory_plugins():
  File "/usr/local/lib/python3.9/site-packages/ansible/inventory/manager.py", line 207, in _fetch_inventory_plugins
    plugin = inventory_loader.get(name)
  File "/usr/local/lib/python3.9/site-packages/ansible/plugins/loader.py", line 853, in get
    return self.get_with_context(name, *args, **kwargs).object
  File "/usr/local/lib/python3.9/site-packages/ansible/plugins/loader.py", line 876, in get_with_context
    self._module_cache[path] = self._load_module_source(name, path)
  File "/usr/local/lib/python3.9/site-packages/ansible/plugins/loader.py", line 826, in _load_module_source
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/local/lib/python3.9/site-packages/ansible/plugins/inventory/toml.py", line 161, in <module>
    toml_loads = toml.loads  # type: ignore[assignment]
AttributeError: module 'toml' has no attribute 'loads'
Exit 250

Installing textproc/py-toml fixes the issue.
Comment 1 Matthew Seaman freebsd_committer freebsd_triage 2023-05-02 15:42:39 UTC
I ran into exactly this problem myself.  What happened in my case was that py-toml had been installed, and then deleted, but had left behind a directory containing some .pyc files.  This was enough to fool python into thinking the toml module was installed, but it had no actual code available to run.

Ansible can use any out of about three alternative modules to provide TOML functionality.  The port defaults to py-tomli, but the ansible code will use py-toml in preference, if it happens to be installed.

If you delete the py-toml module, and then manually remove /usr/local/lib/python3.9/site-packages/toml ansible should work properly again (for values of 3.9 equal to the version of python you've installed...)

This is fallout from the attempt to dynamically generate python cache files at package install time.  That change has been reverted, so all the .pyc files should be prt of the pkgs you install.  It's worthwhile scanning all the files under site-packages and deleting anything _not_ recognised by `pkg which`, possible with judicious re-installation of any affected python modules.
Comment 2 Mikael Urankar freebsd_committer freebsd_triage 2023-05-03 09:08:40 UTC
(In reply to Matthew Seaman from comment #1)
You're right, thanks for the explanation!