ansible / molecule

Molecule aids in the development and testing of Ansible content: collections, playbooks and roles
https://ansible.readthedocs.io/projects/molecule/
MIT License
3.89k stars 664 forks source link

Molecule Reset overwrites projects in COLLECTIONS_PATHS #4061

Open markfaine opened 1 year ago

markfaine commented 1 year ago

Prerequisites

Environment

molecule 6.0.2 using python 3.11 ansible:2.15.1 default:6.0.2 from molecule Linux wsl 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 GNU/Linux Ubuntu 22.04.2 LTS

What happened

Running molecule reset can delete the entire project. The role is in a collection and I think the issue is it tries to install the same collection from GitLab using ansible-galaxy, since my collections path is the same as my development path it overwrites my project. I've been using this same setup for years and this did not happen with previous versions of Molecule.

I am actively developing the collection so it is expected that I might need to run them. When I run them they need to be on the collections path.

If I edit my ansible.cfg and comment out the collections_path, it works correctly:

molecule reset
WARNING  The scenario config file ('/home/ansible/ansible/development/collections/ansible_collections/nats/default/roles/aide/molecule/default/molecule.yml') has been modified since the scenario was created. If recent changes are important, reset the scenario with 'molecule destroy' to clean up created items or 'molecule reset' to clear current configuration.
INFO     default scenario test matrix:
INFO     Performing prerun with role_name_check=0...
INFO     Running from /home/ansible/ansible/development/collections/ansible_collections/nats/default/roles/aide : ansible-galaxy collection install -vvv --force ../..
INFO     Removing /home/ansible/.cache/molecule/aide/default

I could probably work around it by using a shell function or alias but I don't want to have to rely on a workaround.

Reproducing example

To reproduce ensure your COLLECTIONS_PATHS contains the role that you are running molecule on and do `molecule reset` in any role.

For example:

ansible-config dump
...
COLLECTIONS_PATHS(/home/ansible/ansible/development/ansible.cfg) = ['/home/ansible/ansible/development/collections']
...

# Change into a role that is under COLLECTIONS_PATHS and run molecule reset

cd /home/ansible/ansible/development/collections/my/ns/roles/myrole
molecule reset

WARNING  The scenario config file ('/home/ansible/ansible/development/collections/ansible_collections/nats/default/molecule/default/molecule.yml') has been modified since the scenario was created. If recent changes are important, reset the scenario with 'molecule destroy' to clean up created items or 'molecule reset' to clear current configuration.
INFO     default scenario test matrix:
INFO     Performing prerun with role_name_check=0...
INFO     Set ANSIBLE_LIBRARY=plugins/modules:/home/ansible/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
INFO     Set ANSIBLE_ROLES_PATH=roles:/home/ansible/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
INFO     Provisioning collection ansible.posix:>=1.4.0 from galaxy.yml
INFO     Running from /home/ansible/ansible/development/collections/ansible_collections/nats/default : ansible-galaxy collection install -vvv ansible.posix:>=1.4.0
INFO     Provisioning collection community.general:>=5.0.2 from galaxy.yml
INFO     Running from /home/ansible/ansible/development/collections/ansible_collections/nats/default : ansible-galaxy collection install -vvv community.general:>=5.0.2
INFO     Provisioning collection community.crypto:>=2.3.2 from galaxy.yml
INFO     Running from /home/ansible/ansible/development/collections/ansible_collections/nats/default : ansible-galaxy collection install -vvv community.crypto:>=2.3.2
INFO     Running from /home/ansible/ansible/development/collections/ansible_collections/nats/default : ansible-galaxy collection install -vvv --force /home/ansible/ansible/development/collections/ansible_collections/nats/default
WARNING  Retrying execution failure 250 of: ansible-galaxy collection install -vvv --force /home/ansible/ansible/development/collections/ansible_collections/nats/default
ERROR    Command returned 250 code:
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'nats.default:1.1.3' to '/home/ansible/ansible/development/collections/ansible_collections/nats/default'
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/__init__.py", line 659, in cli_executor
    exit_code = cli.run()
                ^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 715, in run
    return context.CLIARGS['func']()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 117, in method_wrapper
    return wrapped_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 1369, in execute_install
    self._execute_install_collection(
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 1406, in _execute_install_collection
    install_collections(
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 769, in install_collections
    install(concrete_coll_pin, output_path, artifacts_manager)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1494, in install
    install_src(collection, b_artifact_path, b_collection_path, artifacts_manager)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1623, in install_src
    file_manifest = _build_files_manifest(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1078, in _build_files_manifest
    return _build_files_manifest_walk(b_collection_path, namespace, name, ignore_patterns)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1245, in _build_files_manifest_walk
    _walk(b_collection_path, b_collection_path)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1205, in _walk
    for b_item in os.listdir(b_path):
                  ^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: b'/home/ansible/ansible/development/collections/ansible_collections/nats/default'

ansible-galaxy
  config file = /home/ansible/ansible/development/ansible.cfg
  configured module search path = ['/home/ansible/ansible/development/collections/ansible_collections/nats/default/plugins/modules', '/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
  ansible collection location = /home/ansible/ansible/development/collections:/usr/local/lib/python3.11/site-packages
  executable location = /usr/local/bin/ansible-galaxy
  python version = 3.11.4 (main, Aug 16 2023, 05:31:52) [GCC 10.2.1 20210110] (/usr/local/bin/python3)
  jinja version = 3.1.2
  libyaml = True
Using /home/ansible/ansible/development/ansible.cfg as config file
Found installed collection ansible.posix:1.5.4 at '/home/ansible/ansible/development/collections/ansible_collections/ansible/posix'
Found installed collection community.general:7.1.0 at '/home/ansible/ansible/development/collections/ansible_collections/community/general'
Found installed collection nats.default:1.1.3 at '/home/ansible/ansible/development/collections/ansible_collections/nats/default'
Found installed collection community.crypto:2.14.0 at '/home/ansible/ansible/development/collections/ansible_collections/community/crypto'
ERROR! Unexpected Exception, this is probably a bug: [Errno 2] No such file or directory: b'/home/ansible/ansible/development/collections/ansible_collections/nats/default'

Traceback (most recent call last):
  File "/usr/local/bin/molecule", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/molecule/command/reset.py", line 46, in reset
    base.execute_cmdline_scenarios(scenario_name, args, command_args)
  File "/usr/local/lib/python3.11/site-packages/molecule/command/base.py", line 113, in execute_cmdline_scenarios
    scenario.config.runtime.prepare_environment(
  File "/usr/local/lib/python3.11/site-packages/ansible_compat/runtime.py", line 653, in prepare_environment
    self.install_collection_from_disk(
  File "/usr/local/lib/python3.11/site-packages/ansible_compat/runtime.py", line 476, in install_collection_from_disk
    self.install_collection(path, destination=destination, force=True)
  File "/usr/local/lib/python3.11/site-packages/ansible_compat/runtime.py", line 467, in install_collection
    raise InvalidPrerequisiteError(msg)
ansible_compat.errors.InvalidPrerequisiteError: Command returned 250 code:
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'nats.default:1.1.3' to '/home/ansible/ansible/development/collections/ansible_collections/nats/default'
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/__init__.py", line 659, in cli_executor
    exit_code = cli.run()
                ^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 715, in run
    return context.CLIARGS['func']()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 117, in method_wrapper
    return wrapped_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 1369, in execute_install
    self._execute_install_collection(
  File "/usr/local/lib/python3.11/site-packages/ansible/cli/galaxy.py", line 1406, in _execute_install_collection
    install_collections(
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 769, in install_collections
    install(concrete_coll_pin, output_path, artifacts_manager)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1494, in install
    install_src(collection, b_artifact_path, b_collection_path, artifacts_manager)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1623, in install_src
    file_manifest = _build_files_manifest(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1078, in _build_files_manifest
    return _build_files_manifest_walk(b_collection_path, namespace, name, ignore_patterns)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1245, in _build_files_manifest_walk
    _walk(b_collection_path, b_collection_path)
  File "/usr/local/lib/python3.11/site-packages/ansible/galaxy/collection/__init__.py", line 1205, in _walk
    for b_item in os.listdir(b_path):
                  ^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: b'/home/ansible/ansible/development/collections/ansible_collections/nats/default'

ansible-galaxy [core 2.15.1]
  config file = /home/ansible/ansible/development/ansible.cfg
  configured module search path = ['/home/ansible/ansible/development/collections/ansible_collections/nats/default/plugins/modules', '/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
  ansible collection location = /home/ansible/ansible/development/collections:/usr/local/lib/python3.11/site-packages
  executable location = /usr/local/bin/ansible-galaxy
  python version = 3.11.4 (main, Aug 16 2023, 05:31:52) [GCC 10.2.1 20210110] (/usr/local/bin/python3)
  jinja version = 3.1.2
  libyaml = True
Using /home/ansible/ansible/development/ansible.cfg as config file
Found installed collection ansible.posix:1.5.4 at '/home/ansible/ansible/development/collections/ansible_collections/ansible/posix'
Found installed collection community.general:7.1.0 at '/home/ansible/ansible/development/collections/ansible_collections/community/general'
Found installed collection nats.default:1.1.3 at '/home/ansible/ansible/development/collections/ansible_collections/nats/default'
Found installed collection community.crypto:2.14.0 at '/home/ansible/ansible/development/collections/ansible_collections/community/crypto'
ERROR! Unexpected Exception, this is probably a bug: [Errno 2] No such file or directory: b'/home/ansible/ansible/development/collections/ansible_collections/nats/default'

I've lost all of my uncommitted changes.

mafalb commented 9 months ago

It happened to me too yesterday when running molecule test. Sounds similar enough. I was running "molecule test" but had ANSIBLE_COLLECTIONS_PATH=../../.. in my environment which I find sometimes necessary. Not sure if this should be another issue. However, not much more to say about it.

ThSchrad commented 9 months ago

I had the same problem today running molecule test with my collection inside ANSIBLE_COLLECTIONS_PATH. After that, there were two files in my collection named FILES.json and MANIFEST.json. All other files were gone.

I think this is caused by #3962. One user even commented that the changes in this pull request will cause problems.

markfaine commented 5 months ago

I can't believe this is still a problem since 2023, even worse I fell into it again yesterday and lost all of the work I had not yet committed. Please someone fix this, or at least provide some sort of confirmation prompt before deleting all my work.