Câu trả lời:
Bạn có thể sử dụng with_fileglobvòng lặp cho việc này:
- copy:
src: "{{ item }}"
dest: /etc/fooapp/
owner: root
mode: 600
with_fileglob:
- /playbooks/files/fooapp/*
/roles/db/filesnhưng tôi không thể làm cho nó hoạt động với phương pháp này. Tôi đã thử with_fileglob: - /roles/db/file/* nhưng nó sẽ không ổn với con đường
- name: Your copy task
copy: src={{ item.src }} dest={{ item.dest }}
with_items:
- { src: 'containerizers', dest: '/etc/mesos/containerizers' }
- { src: 'another_file', dest: '/etc/somewhere' }
- { src: 'dynamic', dest: '{{ var_path }}' }
# more files here
desttập hợp là một biến? { src: 'containerizers', dest: {{ containerizers }} }.
{ src: '{{ source.var }}', dest: '{{ dest.var }}' }
Bạn có thể sử dụng with_together cho mục đích này:
- name: Copy multiple files to multiple directories
copy: src={{ item.0 }} dest={{ item.1 }}
with_together:
- [ 'file1', 'file2', 'file3' ]
- [ '/dir1/', '/dir2/', '/dir3/' ]
Nếu bạn cần nhiều vị trí, bạn cần nhiều hơn một nhiệm vụ. Một tác vụ sao chép chỉ có thể sao chép từ một vị trí (bao gồm nhiều tệp) sang một vị trí khác trên nút.
- copy: src=/file1 dest=/destination/file1
- copy: src=/file2 dest=/destination/file2
# copy each file over that matches the given pattern
- copy: src={{ item }} dest=/destination/
with_fileglob:
- /files/*
- hosts: lnx
tasks:
- find: paths="/appl/scripts/inq" recurse=yes patterns="inq.Linux*"
register: file_to_copy
- copy: src={{ item.path }} dest=/usr/local/sbin/
owner: root
mode: 0775
with_items: "{{ files_to_copy.files }}"
findmô-đun chỉ làm việc cho 2.x ansible nhưng không phải cho 1.x ansible
stdout_linesgiá trị trả về nhưng không áp dụng cho findmô-đun. Nó chỉ có files, examinedvà matchedgiá trị trả về. Hy vọng rằng giúp đỡ người khác
finddường như chỉ nhìn vào hệ thống từ xa, không cho phép lấy bất cứ thứ gì từ nút quản lý. Các câu trả lời này, bằng cách sử dụng with_fileglob, có vẻ phù hợp hơn: stackoverflow.com/a/42290160/272387 , stackoverflow.com/a/36720342/272387 .
Vì Ansible 2.5 nên các with_*cấu trúc không được dùng nữa và loopnên sử dụng cú pháp. Một ví dụ thực tế đơn giản:
- name: Copy CA files
copy:
src: '{{item}}'
dest: '/etc/pki/ca-trust/source/anchors'
owner: root
group: root
mode: 0644
loop:
- symantec-private.crt
- verisignclass3g2.crt
- name: find inq.Linux*
find: paths="/appl/scripts/inq" recurse=yes patterns="inq.Linux*"
register: find_files
- name: set fact
set_fact:
all_files:
- "{{ find_files.files | map(attribute='path') | list }}"
when: find_files > 0
- name: copy files
copy:
src: "{{ item }}"
dest: /destination/
with_items: "{{ all_files }}"
when: find_files > 0
Sử dụng mã nguồn sau để sao chép nhiều tệp trên máy khách của bạn.
- name: Copy data to the client machine
hosts: hostname
become_method: sudo
become_user: root
become: true
tasks:
# Copy twice as sometimes files get skipped (mostly only one file skipped from a folder if the folder does not exist)
- name: Copy UFO-Server
copy:
src: "source files path"
dest: "destination file path"
owner: root
group: root
mode: 0644
backup: yes
ignore_errors: true
Ghi chú:
Nếu bạn đang chuyển nhiều đường dẫn bằng cách sử dụng biến thì
src: "/ root / {{item}}"
Nếu bạn đang chuyển đường dẫn bằng cách sử dụng một biến cho các mục khác nhau thì
src: "/ root / {{item.source_path}}"
copymô-đun là một công cụ sai để sao chép nhiều tệp và / hoặc cấu trúc thư mục, synchronizethay vào đó hãy sử dụng mô-đun được sử dụng rsynclàm phụ trợ. Xin lưu ý bạn, nó yêu cầu rsynccài đặt trên cả bộ điều khiển và máy chủ đích. Nó thực sự mạnh mẽ, hãy kiểm tra tài liệu rõ ràng .
Ví dụ - sao chép tệp từ buildthư mục (với thư mục con) của bộ điều khiển vào /var/www/htmlthư mục trên máy chủ đích:
synchronize:
src: ./my-static-web-page/build/
dest: /var/www/html
rsync_opts:
- "--chmod=D2755,F644" # copy from windows - force permissions