ansible-community / molecule-plugins

Collection on molecule plugins
MIT License
109 stars 73 forks source link

No module named 'molecule' #59

Open fooock opened 2 years ago

fooock commented 2 years ago

I'm trying to run molecule-vagrant in my project but I get this error: ModuleNotFoundError: No module named 'molecule'.

I'm using:

$ python -V
Python 3.8.9

The installed packages in my virtual environment:

$ pip freeze
ansible-compat==2.2.0
arrow==1.2.2
attrs==21.4.0
binaryornot==0.4.4
Cerberus==1.3.2
certifi==2022.6.15
chardet==5.0.0
charset-normalizer==2.1.0
click==8.1.3
click-help-colors==0.9.1
commonmark==0.9.1
cookiecutter==2.1.1
distro==1.7.0
enrich==1.2.7
idna==3.3
importlib-resources==5.9.0
Jinja2==3.1.2
jinja2-time==0.2.0
jsonschema==4.7.2
MarkupSafe==2.1.1
molecule==4.0.1
molecule-vagrant==1.0.0
packaging==21.3
pluggy==1.0.0
Pygments==2.12.0
pyparsing==3.0.9
pyrsistent==0.18.1
python-dateutil==2.8.2
python-slugify==6.1.2
python-vagrant==1.0.0
PyYAML==6.0
requests==2.28.1
rich==12.5.1
selinux==0.2.1
six==1.16.0
subprocess-tee==0.3.5
text-unidecode==1.3
typing-extensions==4.3.0
urllib3==1.26.10
zipp==3.8.1

As you can see, I have molecule and molecule-vagrant installed.

The Vagrant version I'm using:

$ vagrant version
Installed Version: 2.2.19
Latest Version: 2.2.19

Running molecule converge -s vagrant results in this error:

$ molecule converge -s vagrant
INFO     vagrant scenario test matrix: dependency, create, prepare, converge
INFO     Performing prerun with role_name_check=0...
INFO     Set ANSIBLE_LIBRARY=/Users/javi/.cache/ansible-compat/ee13aa/modules:/Users/javi/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_COLLECTIONS_PATH=/Users/javi/.cache/ansible-compat/ee13aa/collections:/Users/javi/.ansible/collections:/usr/share/ansible/collections
INFO     Set ANSIBLE_ROLES_PATH=/Users/javi/.cache/ansible-compat/ee13aa/roles:/Users/javi/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
INFO     Using /Users/javi/.cache/ansible-compat/ee13aa/roles/yourbtc.bitcoin_core symlink to current repository in order to enable Ansible to find the role using its expected full name.
INFO     Running vagrant > dependency
WARNING  Skipping, missing the requirements file.
WARNING  Skipping, missing the requirements file.
INFO     Running vagrant > create

PLAY [Create] ******************************************************************

TASK [Create molecule instance(s)] *********************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'molecule'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/Users/javi/.ansible/tmp/ansible-tmp-1658674349.230397-55949-146925441104677/AnsiballZ_vagrant.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/Users/javi/.ansible/tmp/ansible-tmp-1658674349.230397-55949-146925441104677/AnsiballZ_vagrant.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/javi/.ansible/tmp/ansible-tmp-1658674349.230397-55949-146925441104677/AnsiballZ_vagrant.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible.modules.vagrant', init_globals=dict(_module_fqn='ansible.modules.vagrant', _modlib_path=modlib_path),\n  File \"/usr/local/Cellar/python@3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py\", line 224, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/local/Cellar/python@3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py\", line 96, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/local/Cellar/python@3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py\", line 86, in _run_code\n    exec(code, run_globals)\n  File \"/var/folders/gf/fn91_brj0dl9jm46x9q1xs7r0000gn/T/ansible_vagrant_payload_kx2mnr9g/ansible_vagrant_payload.zip/ansible/modules/vagrant.py\", line 37, in <module>\nModuleNotFoundError: No module named 'molecule'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

WARNING  Retrying execution failure 2 of: ansible-playbook --inventory /Users/javi/.cache/molecule/bitcoin-core-role/vagrant/inventory --skip-tags molecule-notest,notest /Users/javi/projects/yourbtc.link/yourbtc-ansible/bitcoin-core-role/molecule/vagrant/venv/lib/python3.8/site-packages/molecule_vagrant/playbooks/create.yml
CRITICAL Ansible return code was 2, command was: ['ansible-playbook', '--inventory', '/Users/javi/.cache/molecule/bitcoin-core-role/vagrant/inventory', '--skip-tags', 'molecule-notest,notest', '/Users/javi/projects/yourbtc.link/yourbtc-ansible/bitcoin-core-role/molecule/vagrant/venv/lib/python3.8/site-packages/molecule_vagrant/playbooks/create.yml']

Any idea on what is happening?? If you want to test it, my project branch is here

sio commented 2 years ago

I'd hazard a guess that Ansible uses incorrect Python interpreter when trying to invoke molecule module. Your tracebacks (which your didn't bother to format to a readable line length 👎) jump unexpectedly from using venv to referencing /usr/local/Cellar/...

Now that we're talking about venv, why is there no Ansible in yours? Molecule is a very thin wrapper around a bunch of Ansible playbooks. It can not work standalone.

PS: \<insert a generic disapproval of cryptocurrencies for consistently propagating the worst possible behavior through the largest possible group of people>

apatard commented 2 years ago

@fooock did you manage to solve your issue ? I've tried with your branch. I've managed to reproduce it and it's working here once I installed ansible-core to the virtualenv as suggested by @sio (well, create is failing due to missing vbox but it's not related to your problem). @ssbarnea since molecule-vagrant is providing a ansible module, maybe ansible-core should be added to the deps in setup.cfg (I guess it's a different thing than what's addressed by https://github.com/ansible-community/molecule-plugins/issues/65) ? or it should be yet another thing to add in the README (probably in same commit as a commit solving https://github.com/ansible-community/molecule-vagrant/discussions/179) ?

fooock commented 2 years ago

thank you both for your responses. Installing ansible-core in my virtual environment does the trick 👌🏼 should we add ansible-core to install_requires as you say @apatard ? I saw other molecule-* plugins and they don't contain this module