Marco Alfaro
Marco Alfaro

Reputation: 23

How can I use the mv module Ansible

I am trying to use the mv module on Ansible but I am not having luck.

In my initial attempt I did the following:

- name: changing the name of the file
  shell: mv /tmp/bundle /opt/Rocket.Chat

And I get the following error:

FAILED! => {"changed": true, "cmd": "mv /tmp/bundle /opt/Rocket.Chat", "delta": "0:00:00.033553", "end": "2019-02-11 06:06:43.273787", "msg": "non-zero return code", "rc": 1, "start": "2019-02-11 06:06:43.240234", "stderr": "mv: cannot move ‘/tmp/bundle’ to ‘/opt/Rocket.Chat/bundle’: File exists", "stderr_lines": ["mv: cannot move ‘/tmp/bundle’ to ‘/opt/Rocket.Chat/bundle’: File exists"], "stdout": "", "stdout_lines": []}

So, I changed it to:

   - name: create directory
       file:
         state: directory
         path: "/opt/Rocket.Chat"
    
   - name: copy the files
        copy:
          src: "/tmp/bundle"
          dest: "/opt/Rocket.Chat"
          remote_src: yes
    
    - name: delete the other files
        file: path=/tmp/bundle state=absent

My new error is:

FAILED! => {"changed": false, "msg": "Remote copy does not support recursive copy of directory: /tmp/bundle"}

Upvotes: 2

Views: 1750

Answers (1)

Kevin C
Kevin C

Reputation: 5750

Seems that the "copy module to work with recursive and remote_src" does not work yet, but will be supported from May 2019

Here is a workaround, edit the folder names to your setup.

# Copy all files and directories from /usr/share/easy-rsa to /etc/easy-rsa

- name: List files in /usr/share/easy-rsa
  find:
    path: /usr/share/easy-rsa
    recurse: yes
    file_type: any
  register: find_result

- name: Create the directories
  file:
    path: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
    state: directory
    mode: "{{ item.mode }}"
  with_items:
    - "{{ find_result.files }}"
  when:
    - item.isdir

- name: Copy the files
  copy:
    src: "{{ item.path }}"
    dest: "{{ item.path | regex_replace('/usr/share/easy-rsa','/etc/easy-rsa') }}"
    remote_src: yes
    mode: "{{ item.mode }}"
  with_items:
    - "{{ find_result.files }}"
  when:
    - item.isdir == False

Upvotes: 2

Related Questions