How to move/rename a file using an Ansible task on a remote system
Asked Answered
T

14

265

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.

Teteak answered 11/6, 2014 at 12:30 Comment(9)
Why don't you want to use command/shell?Printing
Just wanted to know if there is a way without using the mentioned tasks. Looks like there is no other way at the moment.Teteak
Why do you want to move it specifically instead of copy it? That seems like a one-off action, rather than an idempotent ensuring-the-state-of-the-system type of step.Printing
I have a sample configuration file included in a RPM package and I want to move this sample configuration file.Teteak
How about creating a symlink to the file? Or doing a get_url on the source file (it's probably available online somewhere if it's open-source).Printing
At the moment I'm using a symlink to reference the file. Using get_url is no option for me because the system cannot reach the internet.Teteak
Ansible 2.0 have added an option which takes care of this issue on the copy module: "remote_src".Argumentation
I suggested add a module function in order to avoid non-portable commands at github.com/ansible/ansible/issues/51694.Pruitt
What is more generic than moving a file? Answer: nothing. What would posses somebody to then use Ansible to invoke a proprietary shell command for something that is so clearly generic? Not "getting it" I suppose. Please see more common sense solution below.Schoolboy
M
261

The file module doesn't copy files on the remote system. The src parameter is only used by the file module when creating a symlink to a file.

If you want to move/rename a file entirely on a remote system then your best bet is to use the command module to just invoke the appropriate command:

- name: Move foo to bar
  command: mv /path/to/foo /path/to/bar

If you want to get fancy then you could first use the stat module to check that foo actually exists:

- name: stat foo
  stat: path=/path/to/foo
  register: foo_stat

- name: Move foo to bar
  command: mv /path/to/foo /path/to/bar
  when: foo_stat.stat.exists
Mishandle answered 11/6, 2014 at 14:22 Comment(9)
Thanks for your answer. But like mentioned in the question I don't want to do it using the command task. Looks like it's not yet possible to copy or move remote files using an Ansible module.Teteak
Without using the command module, about your only other choice would be to write your own custom module.Mishandle
Probably I'll extend the copy module to also support remote-only copy operations.Teteak
Tagged as correct answer because this is the current way to copy remote files.Teteak
Regarding looking before you leap: is there any reason to not use the removes option to the command module (documented here)? It seems that that option would make Ansible check first.Geochemistry
Ansible tracks changes to notify the handler, which makes this solution suboptimal.Fossil
You do not have to manually check for the file's existence if you use removes: /path/to/foo and creates: /path/to/bar. @Fonant already mentioned this as comment on another answer, but as this is the accepted one, I want to point it out again.Griffy
downvote for not using removes: /path/to/foo and creates: /path/to/barCarburetor
Although that solution is ansibleish and correct, as you are using a bash command you can also leave it in one task using the following: test -f file && mv file file.old. In that way mv only would run when the file existsSutlej
C
269

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.

- name: Copy files from foo to bar
  copy: remote_src=True src=/path/to/foo dest=/path/to/bar

If you want to move file you need to delete old file with file module

- name: Remove old files foo
  file: path=/path/to/foo state=absent

From version 2.8 copy module remote_src supports recursive copying.

Cytotaxonomy answered 4/1, 2016 at 15:7 Comment(17)
Small remark: "Currently remote_src does not support recursive copying." taken from the ansible module doc. So if you want to copy recursively you still need the shell/command module.Biforate
If you want to copy many files you have to use synchronize module. From ansible : "The “copy” module recursively copy facility does not scale to lots (>hundreds) of files. For alternative, see synchronize module, which is a wrapper around rsync."Cytotaxonomy
I don't understand. Copying then deleting is not the same as moving. For one, it's not atomic. For another, it's slower, especially for large files. I'm new to Ansible, but this seems really weird to me.Bookmark
@Bookmark Logically it's the same but in file systems it's not the same as copying and deleting. For this time this is only one right way for moving file in the remote host (without command module).Cytotaxonomy
@alex what I'm saying is can't be the right way to do this. I'm going upwind against 50 something upvotes, but this is crazy. Another issue: permissions and other attributes aren't maintained. Another: What if the file is changed during the copy?Bookmark
@alex also if what you want to do is effectively rename a directory containing hundreds of MB of files, copy/sync then delete is much much worse. (And if big enough you could run out of space during the copy.) I'll stick to command: mv foo bar for my use case.Matteson
@Hamish Downer and mlissner. I didn't say that it's the best solution for all your needs. Also I wrote that if you want to copy many files you shouldn't use copy module. Read the question "I don't want to use the command/shell tasks".Cytotaxonomy
@AlecWenzowski Yes, sure! I think everyone know this. What's the main idea of your comment? Downvoting answer?)Cytotaxonomy
@Cytotaxonomy this is the second highest voted answer on a question about idempotently moving files. The question is not about copying. There are many problems with copying instead of moving therefore this answer is incorrect. So it gets a downvote. Etiquette on SO is to explain downvotes. As noted elsewhere, the best option so far is to command: mv /path/to/foo /path/to/bar creates=/path/to/bar removes=/path/to/fooCarburetor
Beware ansible copy module is very stupid it doesn't do privilege escalation for src file/directory therefore if there is file/directory in src which need privilege escalation before you read then the task will fail!Ureter
This answer is just WRONG for the question, and a it's a horrible practice to encourage. I'm saddened for the community that @Cytotaxonomy does not accept the pushback here and just edit/delete his answer. If bad upvotes really matter that speaks more to pride mattering more than a good answer.Downes
@Crossfit_and_Beer that question was discussed in comments already. Copying isn't moving I agree with that many times before! That's one more way how you can do some tasks using copy/file module (because you can set some additional parameters and control more return values that command module doesn't provide).Cytotaxonomy
All good @Alex. For anyone considering alternative approaches: what I did was "file" with "state=absent" to remove the file. Then I redeploy it using "copy:". In my case, I just want to disable a cron job, and I wanted to do this using "pure Ansible" because avoiding command/shell makes dry-runs (--check) and the output simpler for other people to read.Downes
`remote_src' supports recursive copying as of version 2.8.Carberry
this is not good. If you have a 100GB file, this is a mess.Nosewheel
As others mentioned, this "solution" is not idempotent at all, which is the foundation of anything Ansible.Wilbourn
This will only works on the first run, not on subsequent runs. Even adding force: no to copy to only copy if the dest doesn't exist will fail if the source doesn't exist anymore, even it wouldn't need to copy as the dest has already been created.Provincial
M
261

The file module doesn't copy files on the remote system. The src parameter is only used by the file module when creating a symlink to a file.

If you want to move/rename a file entirely on a remote system then your best bet is to use the command module to just invoke the appropriate command:

- name: Move foo to bar
  command: mv /path/to/foo /path/to/bar

If you want to get fancy then you could first use the stat module to check that foo actually exists:

- name: stat foo
  stat: path=/path/to/foo
  register: foo_stat

- name: Move foo to bar
  command: mv /path/to/foo /path/to/bar
  when: foo_stat.stat.exists
Mishandle answered 11/6, 2014 at 14:22 Comment(9)
Thanks for your answer. But like mentioned in the question I don't want to do it using the command task. Looks like it's not yet possible to copy or move remote files using an Ansible module.Teteak
Without using the command module, about your only other choice would be to write your own custom module.Mishandle
Probably I'll extend the copy module to also support remote-only copy operations.Teteak
Tagged as correct answer because this is the current way to copy remote files.Teteak
Regarding looking before you leap: is there any reason to not use the removes option to the command module (documented here)? It seems that that option would make Ansible check first.Geochemistry
Ansible tracks changes to notify the handler, which makes this solution suboptimal.Fossil
You do not have to manually check for the file's existence if you use removes: /path/to/foo and creates: /path/to/bar. @Fonant already mentioned this as comment on another answer, but as this is the accepted one, I want to point it out again.Griffy
downvote for not using removes: /path/to/foo and creates: /path/to/barCarburetor
Although that solution is ansibleish and correct, as you are using a bash command you can also leave it in one task using the following: test -f file && mv file file.old. In that way mv only would run when the file existsSutlej
T
128

I have found the creates option in the command module useful. How about this:

- name: Move foo to bar
  command: mv /path/to/foo /path/to/bar
  args:
    creates: /path/to/bar 

I used to do a two task approach using stat like @Bruce P suggests. Now I do this as one task with creates. I think this is a lot clearer.

Tenfold answered 27/6, 2014 at 17:42 Comment(3)
Or, even better: command: mv /path/to/foo /path/to/bar creates=/path/to/bar removes=/path/to/fooSkell
As you are using a command, you can also leave it in: test -f file && mv file file.oldSutlej
This is the best answer if you want to move (but preserve) an original config file/directory out of the way and create or clone a custom one in its place. Bruce P's answer would clobber the new custom file/folder every time. This is also the most elegant solution. Check the example in the docs for better syntax: docs.ansible.com/ansible/latest/collections/ansible/builtin/…Nae
F
36
- name: Move the src file to dest
  command: mv /path/to/src /path/to/dest
  args:
    removes: /path/to/src
    creates: /path/to/dest

This runs the mv command only when /path/to/src exists and /path/to/dest does not, so it runs once per host, moves the file, then doesn't run again.

I use this method when I need to move a file or directory on several hundred hosts, many of which may be powered off at any given time. It's idempotent and safe to leave in a playbook.

Farber answered 19/2, 2021 at 18:27 Comment(0)
M
10

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:

- synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
    archive: yes
  delegate_to: "{{ inventory_hostname }}"
Maestro answered 28/9, 2017 at 14:55 Comment(1)
This doesn't work locally in every case because dest is accessed through SSH even if the directory is on the same machine.Pruitt
C
10

I know it's a YEARS old topic, but I got frustrated and built a role for myself to do exactly this for an arbitrary list of files. Extend as you see fit:

main.yml

- name: created destination directory
  file:
    path: /path/to/directory
    state: directory
    mode: '0750'
- include_tasks: move.yml
  loop:
    - file1
    - file2
    - file3

move.yml

- name: stat the file
  stat:
    path: {{ item }}
  register: my_file

- name: hard link the file into directory
  file:
    src: /original/path/to/{{ item }}
    dest: /path/to/directory/{{ item }}
    state: hard
  when: my_file.stat.exists

- name: Delete the original file
  file:
    path: /original/path/to/{{ item }}
    state: absent
  when: my_file.stat.exists

Note that hard linking is preferable to copying here, because it inherently preserves ownership and permissions (in addition to not consuming more disk space for a second copy of the file).

Counterproposal answered 28/1, 2020 at 22:4 Comment(1)
This will not work across partitionsNicolettenicoli
J
5

This is the way I got it working for me:

  Tasks:
  - name: checking if the file 1 exists
     stat:      
      path: /path/to/foo abc.xts
     register: stat_result

  - name: moving file 1
    command: mv /path/to/foo abc.xts /tmp
    when: stat_result.stat.exists == True

the playbook above, will check if file abc.xts exists before move the file to tmp folder.

Jp answered 13/7, 2016 at 15:58 Comment(3)
No need to use when: stat_result.stat.exists == True. Just using when: stat_result.stat.exists is good enough.Bonne
I usually use the == True because I'm always doing something when the file is not found or == False.Jp
According to Official documentation page of stat module exists property returns a boolean value. So, if you only put when: stat_result.stat.exists that will satisfy the condition if the file is present which is also identical for when: stat_result.stat.exists == True but with more texts and unnecessary conditional check.Bonne
I
5

Another way to achieve this is using file with state: hard.

This is an example I got to work:

- name: Link source file to another destination
  file:
    src: /path/to/source/file
    path: /target/path/of/file
    state: hard

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:

- name: Remove old file
  file:
    path: /path/to/source/file
    state: absent

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).

Intrigue answered 26/5, 2017 at 22:19 Comment(2)
Unfortunately, this will not work for a directory, as hard links are not allowed for directories (((Anesthetize
This answer makes an assumption about the target system, specifically that both src and dest are on the same partition. This may not be true and therefore this answer should not be used.Khorma
D
4

Bruce wasn't attempting to stat the destination to check whether or not to move the file if it was already there; he was making sure the file to be moved actually existed before attempting the mv.

If your interest, like Tom's, is to only move if the file doesn't already exist, I think we should still integrate Bruce's check into the mix:

- name: stat foo
  stat: path=/path/to/foo
  register: foo_stat

- name: Move foo to bar
  command: creates="path/to/bar" mv /path/to/foo /path/to/bar
  when: foo_stat.stat.exists
Durian answered 10/11, 2015 at 23:58 Comment(0)
P
1

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.

  1. Use tar to archive the file(s) you will need. If you think ahead this actually makes sense. You may want a series of files in a given directory. Create that directory with all of the files and archive them in a tar.
  2. Use the unarchive module. When you do that, along with the destination: and remote_src: keyword, you can place copy all of your files to a temporary folder to start with and then unpack them exactly where you want to.
Promoter answered 11/7, 2017 at 22:27 Comment(1)
There is no idempotence in archiving with tarKries
P
1

On Windows: - name: Move old folder to backup win_command: "cmd.exe /c move /Y {{ sourcePath }} {{ destinationFolderPath }}"

To rename use rename or ren command instead

Petronille answered 4/6, 2020 at 16:35 Comment(3)
Please explain the use of /c and /Y, I was more interested on rename and able to do it using without /Y. I have gone through help topic with cmd /? but didn't make much sense to include /Y here.Cunctation
@user2964808, /c is to have cmd exit and return control, otherwise it will wait for user inputPetronille
@user2964808, /Y is to skip asking for confirmation if file already exists. If you're 100% sure the destination folder is empty, you may skip itPetronille
F
0

You can Do It by --

Using Ad Hoc Command

ansible all -m command -a" mv /path/to/foo /path/to/bar"

Or You if you want to do it by using playbook

- name: Move File foo to destination bar
  command: mv /path/to/foo /path/to/bar
Fabianfabianism answered 12/8, 2019 at 7:30 Comment(0)
G
0

to copy/move/rename any file we can use shell or command module

  • name: copy or mv within remote host command: cp /path/of/the/file/with/name /path/of/the/destination/directory/

while giving source make sure name of the file is last with no slash(/) and destination ends with slash(/)

  • name: rename within remote host shell: chdir=/path/ mv file_name new_file_name

command and shell and be used interchangeably.

Gunfire answered 17/3, 2023 at 15:38 Comment(0)
B
-2
- name: Example
  hosts: localhost
  become: yes

  tasks:
  - name: checking if a file exists
    stat:
      path: "/projects/challenge/simplefile.txt"
    register: file_data
  
  - name: move the file if file exists
    copy: 
      src: /projects/challenge/simplefile.txt
      dest: /home/user/test
    when: file_data.stat.exists

  - name: report a missing file
    debug: 
      msg: "the file or directory doesn't exist"
    when: not file_data.stat.exists
Burnham answered 25/10, 2021 at 12:35 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Augmented

© 2022 - 2024 — McMap. All rights reserved.