我该如何使用MV模块Ansible

时间:2019-02-11 06:41:18

标签: ansible mv

我正在尝试在Ansible上使用mv模块,但是我没有运气。

在我最初的尝试中,我做了以下事情:

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

我收到以下错误:

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": []}

因此,我将其更改为:

   - 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

我的新错误是:

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

1 个答案:

答案 0 :(得分:1)

似乎“与递归和remote_src一起使用的复制模块”尚不起作用,但是will be supported from May 2019

这是一种解决方法,将文件夹名称编辑为您的设置。

# 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