57

我正在尝试将 dist 目录的内容复制到 nginx 目录。

- name: copy html file
  copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/

但是当我执行剧本时,它会抛出一个错误:

TASK [NGINX : copy html file] **************************************************
fatal: [172.16.8.200]: FAILED! => {"changed": false, "failed": true, "msg": "attempted to take checksum of directory:/home/vagrant/dist/"}

如何复制包含另一个目录和文件的目录?

4

12 回答 12

54

您可以使用同步模块。文档中的示例:

# Synchronize two directories on one remote host.
- synchronize:
    src: /first/absolute/path
    dest: /second/absolute/path
  delegate_to: "{{ inventory_hostname }}"

这有一个额外的好处,那就是它对于大/许多文件会更有效。

于 2017-08-28T04:55:02.037 回答
38

编辑:发布问题时,此解决方案有效。后来 Ansible 弃用了递归复制remote_src

Ansible Copy模块默认将文件/目录从控制机器复制到远程机器。如果你想在远程机器上复制文件/目录并且你有Ansible 2.0,设置remote_srcyes

- name: copy html file
  copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/ remote_src=yes directory_mode=yes
于 2016-02-18T18:47:06.920 回答
30

已解决的答案:要将目录的内容复制到另一个目录,我使用下一个:

- name: copy consul_ui files
  command: cp -r /home/{{ user }}/dist/{{ item }} /usr/share/nginx/html
  with_items:
   - "index.html"
   - "static/"

它将这两个项目复制到另一个目录。在示例中,其中一项是目录,另一项不是。它完美地工作。

于 2016-02-24T13:51:01.490 回答
25

要将目录的内容复制到另一个目录,您可以使用 ansiblescopy模块:

- name: Copy content of directory 'files'
  copy:
    src: files/    # note the '/' <-- !!!
    dest: /tmp/files/

从有关参数的文档中src

如果 (src!) path 是一个目录,它会被递归复制...
...如果 path 以“/”结尾,则只有该目录的内部内容被复制到 destination
...如果它不以“/”结尾,则复制包含所有内容的目录本身。

于 2020-06-07T15:52:37.063 回答
13

我发现复制文件夹内容而不复制文件夹本身的最简单解决方案是使用以下内容:

- name: Move directory contents
  command: cp -r /<source_path>/. /<dest_path>/

这解决了@ surfer190的后续问题:

嗯,如果你想复制整个内容怎么办?我注意到 * 不起作用 – surfer190 2016 年 7 月 23 日 7:29

*是一个 shell glob,因为它依赖于你的 shell在运行之前cp枚举文件夹中的所有文件,而.直接指示cp获取目录内容(参见https://askubuntu.com/questions/86822/how-can- i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo )

于 2017-11-20T04:05:58.973 回答
4

Ansible remote_src 不支持递归复制。请参阅 Ansible 复制文档中的 remote_src 描述

要递归复制文件夹的内容并确保任务保持幂等性,我通常这样做:

- name: get file names to copy
  command: "find /home/vagrant/dist -type f"
  register: files_to_copy

- name: copy files
  copy:
    src: "{{ item }}" 
    dest: "/usr/share/nginx/html"
    owner: nginx
    group: nginx
    remote_src: True
    mode: 0644
  with_items:
   - "{{ files_to_copy.stdout_lines }}"

缺点是 find 命令仍然显示为“已更改”

于 2016-08-16T14:01:53.410 回答
4

对于参数 src, ansible 文档非常清楚 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html它说以下内容:

Local path to a file to copy to the remote server.
This can be absolute or relative.
If path is a directory, it is copied recursively. In this case, if path ends with "/", 
only inside contents of that directory are copied to destination. Otherwise, if it 
does not end with "/", the directory itself with all contents is copied. This behavior 
is similar to the rsync command line tool.

因此,您需要跳过 src 路径末尾的 / 。

- name: copy html file
  copy: src=/home/vagrant/dist dest=/usr/share/nginx/html/
于 2020-12-03T15:42:46.060 回答
2

我找到了从远程到远程递归复制的解决方法:

- 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
于 2018-02-23T01:11:26.953 回答
2

我也参与了一整天!最后在shell命令中找到解决方案,而不是复制:命令:如下:

- hosts: remote-server-name
  gather_facts: no
  vars:
    src_path: "/path/to/source/"
    des_path: "/path/to/dest/"
  tasks:
  - name: Ansible copy files remote to remote
    shell: 'cp -r {{ src_path }}/. {{ des_path }}'

严格注意: 1. src_path 和 des_path 以/符号结尾 2. 在 shell 命令中 src_path 以.显示目录的所有内容结尾 3. 我在主机中使用了我的远程服务器名称:并执行 jenkins 的 shell 部分,而不是说明remote_src:符在剧本中。

我想在 jenkins 的 Execute Shell 部分中运行以下命令是一个很好的建议:

ansible-playbook  copy-payment.yml -i remote-server-name
于 2018-05-24T08:20:10.183 回答
1

下面为我​​工作,

-name: Upload html app directory to Deployment host
 copy: src=/var/lib/jenkins/workspace/Demoapp/html dest=/var/www/ directory_mode=yes
于 2017-09-25T12:27:56.573 回答
0

我找到了一个将文件从 Ansible 服务器复制到远程的理想解决方案。

复制yaml文件

- hosts: localhost
  user: {{ user }}
  connection: ssh
  become: yes
  gather_facts: no
  tasks:
   - name: Creation of directory on remote server
     file:
       path: /var/lib/jenkins/.aws
       state: directory
       mode: 0755
     register: result
   - debug: 
       var: result

   - name: get file names to copy
     command: "find conf/.aws -type f"
     register: files_to_copy

   - name: copy files
     copy:
      src: "{{ item }}" 
      dest: "/var/lib/jenkins/.aws"
      owner: {{ user }}
      group: {{ group }}
      remote_src: True
      mode: 0644
     with_items:
      - "{{ files_to_copy.stdout_lines }}"
于 2017-12-07T05:50:38.743 回答
0

如何将目录和子目录和文件从 ansible 服务器复制到远程主机

- name: copy nmonchart39 directory  to {{ inventory_hostname }}
  copy:
    src: /home/ansib.usr.srv/automation/monitoring/nmonchart39
    dest: /var/nmon/data


Where:
copy entire directory: src: /automation/monitoring/nmonchart39
copy directory contents src: nmonchart39/
于 2020-07-21T09:34:29.407 回答