Skip to content

Commit

Permalink
Fix symlinks creation in ansible provisioneer (ansible#4134)
Browse files Browse the repository at this point in the history
When I use
```yaml
provisioner:
  name: ansible
  ...
  inventory:
    links:
      .files: ../.inventory/.files
      host_vars: ../.inventory/host_vars
      group_vars: ../.inventory/group_vars
```
I receive FileExistError
```shell
Traceback (most recent call last):
  File ".venv/bin/molecule", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File ".venv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "venv/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/molecule/command/test.py", line 113, in test
    base.execute_cmdline_scenarios(scenario_name, args, command_args, ansible_args)
  File ".venv/lib/python3.11/site-packages/molecule/command/base.py", line 124, in execute_cmdline_scenarios
    execute_scenario(scenario)
  File "venv/lib/python3.11/site-packages/molecule/command/base.py", line 167, in execute_scenario
    execute_subcommand(scenario.config, action)
  File ".venv/lib/python3.11/site-packages/molecule/command/base.py", line 157, in execute_subcommand
    return command(config).execute(args)
           ^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/molecule/command/base.py", line 57, in __init__
    self._setup()
  File ".venv/lib/python3.11/site-packages/molecule/command/base.py", line 76, in _setup
    self._config.provisioner.manage_inventory()
  File ".venv/lib/python3.11/site-packages/molecule/provisioner/ansible.py", line 838, in manage_inventory
    self._link_or_update_vars()
  File ".venv/lib/python3.11/site-packages/molecule/provisioner/ansible.py", line 912, in _link_or_update_vars
    os.symlink(source, target)
FileExistsError: [Errno 17] File exists: 'roles/common/molecule/docker-al-8/../.inventory/.files' -> '.cache/molecule/common/docker-al-8/inventory/.files'
```

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
rakhinskiy and pre-commit-ci[bot] committed Feb 13, 2024
1 parent 650d67d commit 4767203
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/molecule/provisioner/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,14 @@ def _link_or_update_vars(self):
if not os.path.exists(source):
msg = f"The source path '{source}' does not exist."
util.sysexit_with_message(msg)
if os.path.exists(target):
if os.path.realpath(target) == os.path.realpath(source):
msg = f"Required symlink {target} to {source} exist, skip creation"
LOG.debug(msg)
continue
msg = f"Required symlink {target} exist with another source"
LOG.debug(msg)
os.remove(target)
msg = f"Inventory {source} linked to {target}"
LOG.debug(msg)
os.symlink(source, target)
Expand Down

0 comments on commit 4767203

Please sign in to comment.