How is it possible to move/rename a file/directory using an Ansible module on a remote system? I don't want to use the command/shell tasks and I don't want to copy the file from the local system to the remote system.
相关问题
- Access ansible.cfg variable in task
- Unable to get “exclude” option working with unarch
- Ansible: find file and loop over paths
- Generating tuples variables in ansible templates
- Ansible: setting user on dynamic ec2
相关文章
- Ansible create postgresql user with access to all
- Difference between shell and command in ansible
- How to make Ansible run one certain task only on o
- clone a specific branch from git through ansible p
- Check if a list contains an item in Ansible
- Defining host as variable in Ansible hosts file
- ansible/jinja2 get unique subelements
- Custom credentials in Ansible Tower with Custom Py
I have found the creates option in the command module useful. How about this:
I used to do a 2 task approach using stat like Bruce P suggests. Now I do this as one task with creates. I think this is a lot clearer.
From version 2.0, in copy module you can use
remote_src
parameter.If
True
it will go to the remote/target machine for the src.If you want to move file you need to delete old file with file module
Another way to achieve this is using
file
withstate: hard
.This is an example I got to work:
Only tested on localhost (OSX) though, but should work on Linux as well. I can't tell for Windows.
Note that absolute paths are needed. Else it wouldn't let me create the link. Also you can't cross filesystems, so working with any mounted media might fail.
The hardlink is very similar to moving, if you remove the source file afterwards:
Another benefit is that changes are persisted when you're in the middle of a play. So if someone changes the source, any change is reflected in the target file.
You can verify the number of links to a file via
ls -l
. The number of hardlinks is shown next to the mode (e.g. rwxr-xr-x 2, when a file has 2 links).This is the way I got it working for me:
the playbook above, will check if file abc.xts exists before move the file to tmp folder.
This may seem like overkill, but if you want to avoid using the command module (which I do, because it using command is not idempotent) you can use a combination of copy and unarchive.
Another Option that has worked well for me is using the synchronize module . Then remove the original directory using the file module.
Here is an example from the docs: