Ansible: copy a directory content to another direc

2020-02-18 03:48发布

I am trying to copy the content of dist directory to nginx directory.

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

But when I execute the playbook it throws an error:

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/"}

How can I copy a directory that has another directory and a file inside?

9条回答
仙女界的扛把子
2楼-- · 2020-02-18 04:33

Below worked for me,

-name: Upload html app directory to Deployment host
 copy: src=/var/lib/jenkins/workspace/Demoapp/html dest=/var/www/ directory_mode=yes
查看更多
乱世女痞
3楼-- · 2020-02-18 04:34

Ansible remote_src does not support recursive copying.See remote_src description in Ansible copy docs

To recursively copy the contents of a folder and to make sure the task stays idempotent I usually do it this way:

- 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 }}"

Downside is that the find command still shows up as 'changed'

查看更多
再贱就再见
4楼-- · 2020-02-18 04:38

Resolved answer: To copy a directory's content to another directory I use the next:

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

It copies both items to the other directory. In the example, one of the items is a directory and the other is not. It works perfectly.

查看更多
登录 后发表回答