Development Tip

Ansible로 여러 파일 복사

yourdevel 2020. 10. 8. 19:09
반응형

Ansible로 여러 파일 복사


Ansible이 작업에서 하나 이상의 파일을 원격 노드에 복사하려면 어떻게해야합니까?

파일을 정의하기 위해 내 작업에서 복사 모듈 줄을 복제하려고 시도했지만 첫 번째 파일 만 복사합니다.


이를 위해 with_fileglob루프를 사용할 수 있습니다 .

- copy:
    src: "{{ item }}"
    dest: /etc/fooapp/
    owner: root
    mode: 600
  with_fileglob:
    - /playbooks/files/fooapp/*

- 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

이 목적으로 with_together를 사용할 수 있습니다.

- name: Copy multiple files to multiple directories
  copy: src={{ item.0 }} dest={{ item.1 }}
  with_together:
    - [ 'file1', 'file2', 'file3' ]
    - [ '/dir1/', '/dir2/', '/dir3/' ]

둘 이상의 위치가 필요한 경우 둘 이상의 작업이 필요합니다. 하나의 복사 작업은 한 위치 (여러 파일 포함)에서 노드의 다른 위치로만 복사 할 수 있습니다.

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

- 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

또는 with_items를 사용할 수 있습니다.

- copy:
    src: "{{ item }}"
    dest: /etc/fooapp/
    owner: root
    mode: 600
  with_items:
    - dest_dir

copy모듈은 많은 파일 및 / 또는 디렉토리 구조를 복사하는 데 잘못된 도구입니다 . synchronize대신 모듈 rsync을 백엔드로 사용하십시오. rsync컨트롤러와 대상 호스트 모두에 설치 해야 합니다. 정말 강력 합니다 . ansible 문서를 확인 하세요 .

Example - copy files from build directory (with subdirectories) of controller to /var/www/html directory on target host:

synchronize:
  src: ./my-static-web-page/build/
  dest: /var/www/html
  rsync_opts:
    - "--chmod=D2755,F644" # copy from windows - force permissions

참고URL : https://stackoverflow.com/questions/36696952/copy-multiple-files-with-ansible

반응형