fileansiblemove

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


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.


Solution

  • 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