How to move/rename a file using an Ansible task on

2019-01-12 22:49发布

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.

标签: ansible
8条回答
闹够了就滚
2楼-- · 2019-01-12 23:27

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

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

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.

查看更多
\"骚年 ilove
3楼-- · 2019-01-12 23:28

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
查看更多
萌系小妹纸
4楼-- · 2019-01-12 23:29

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

查看更多
Bombasti
5楼-- · 2019-01-12 23:31

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.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-12 23:33

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.
查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-12 23:41

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 }}"
查看更多
登录 后发表回答