Ansible how to fix destination path already exists and is not an empty directory?
I got an error when I am trying to do the git clone
on the remote machine using Ansible and the error message which I got was destination path already exists and is not an empty directory
How do fix ansible's destination path already exists error when you are trying to clone the git repo?
In my case, the error came because I already had .git
file present in my destination directory.
1- name: Read-write git checkout from github
2 ansible.builtin.git:
3 repo: https://github.com/rahulwagh/python-example.git
4 dest: ./basic-http-server
In the above code my destination directory ./basic-http-server
was pre-existing and already had .git
file inside.
How I fixed and removed .git file?
- Go inside the destination directory where you are trying to clone the repo. e.g.
1#Put the name of the destination directory
2
3cd NAME_OF_DESTINATION_DIRECTORY
- Run the following list command to view the content of the directory
1ls -lart
- Remove the .git directory if present.
1rm -rf .git
- After removing the .git directory try re-running the ansible-playbook and it should succeed.
How to fix ansible's destination path already exists error when you are using copy and file module of Ansible?
If you're getting an error that says "destination path already exists and is not an empty directory," it means that Ansible is trying to create a directory at a path that already exists and contains files or subdirectories.
This error can occur when using the file or copy module in Ansible to create or copy a directory.
Here is how you can resolve the issue -
force: yes - Use the state
option to set the directory's state to directory
. For example, state: directory
in the file
module or force: yes
in copy
module. This will ensure that the directory is present, but won't create it if it already exists.
File module:-
Here is an example ansible code snippet -
1#Replace the "path: /path/to/directory" with your desired destination directory
2
3- name: Ensure the directory is present
4 file:
5 path: /path/to/directory
6 state: directory
7 force: yes
Copy module:- You could use the same approach with copy
module instead of file
but the option would be called force
and the default is no
, so you have to explicitly set it to yes, like so:
Here is an example ansible code snippet -
1#Replace the "path: /path/to/directory" with your desired destination directory
2
3- name: copy files to the directory
4 copy:
5 src: /path/to/source/files
6 dest: /path/to/destination/directory
7 force: yes
This will copy files to the directory even if it is not an empty directory.
Refer to stackoverflow for more similar threads
Posts in this Series
- Ansible Handlers Explained Real-World Use Cases & Examples?
- How to Use SSH Keys with Ansible for Secure Server Management?
- Why YAML is so important in Ansible?
- Clone Public and Private Git Repositories with Ansible
- How to Limit Ansible playbook on Hosts, Groups and multiple hosts?
- Easy Fix for 'zsh command not found ansible' Error After Installing Ansible with Pip
- Demystifying Hosts, Inventory Roles, and Tasks
- Fixing-Unable to start service apache2 Job for apache2.service failed because the control process exited with error code?
- Why Ansible is the Ultimate Tool for DevOps Teams - A Beginner's Guide?
- Ansible how to fix destination path already exists and is not an empty directory?
- Install Ansible on MacOS, Windows, Ubuntu(debian) and Fedora(rpm) - Part 1