簡體   English   中英

"使用 Ansible 復制多個文件"

[英]Copy multiple files with 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

從 Ansible 2.5 開始, 不推薦使用with_*結構,應該使用loop語法。 一個簡單實用的例子:

- 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

為此,您可以使用 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

您可以使用目錄列表遍歷變量:

- name: Copy files from several directories
  copy:
    src: "{{ item }}"
    dest: "/etc/fooapp/"
    owner: root
    mode: "0600"
  loop: "{{ files }}"
  vars:
    files:
      - "dir1/"
      - "dir2/"

使用以下源代碼復制客戶端計算機上的多個文件。


 - 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

注意:

如果您使用變量傳遞多個路徑,則

src: "/root/{{ item }}"

如果您通過對不同項目使用變量來傳遞路徑,那么

src: "/root/{{ item.source_path }}"

copy模塊是用於復制許多文件和/或目錄結構的錯誤工具,請使用synchronize模塊而不是使用rsync作為后端。 請注意,它需要在控制器和目標主機上安裝rsync 它真的很強大,請查看ansible 文檔

示例 - 將文件從控制器的build目錄(帶有子目錄)復制到目標主機上的/var/www/html目錄:

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

這是復制文件的通用解決方案:

   ...
    - name: Find files you want to move
      ansible.builtin.find:
        paths: /path/to/files/
        file_type: file
        excludes: "*.txt" # Whatever pattern you want to exclude
      register: files_output

    - name: Copy the files
      ansible.builtin.copy:
        src: "{{ item.path }}"
        dest: /destination/directory/
      loop: "{{ files_output.files }}"
   ...

這比使用with_fileglob更強大,因為您可以使用正則表達式進行匹配。 這是正在運行的游戲:

$ ls /path/to/files
demo.yaml  test.sh  ignore.txt

$ ls /destination/directory
file.h

$ ansible-playbook playbook.yaml
...[some output]...

$ ls /destination/directory
file.h demo.yaml test.sh

從上面的示例中可以看出,由於 playbook 中的excludes regex, ignore.txt沒有被復制到目標目錄。 忽略這樣的文件是不可能的,因為簡單地使用with_fileglob

此外,您可以相對輕松地從多個目錄移動文件:

   ...
    - name: Find files you want to move
      ansible.builtin.find:
        paths: /path/to/files/
        # ... the rest of the task
      register: list1

    - name: Find more files you want to move
      ansible.builtin.find:
        paths: /different/path/
        # ... the rest of the task
      register: list2

    - name: Copy the files
      ansible.builtin.copy:
        src: "{{ item.path }}"
        dest: /destination/directory/
      loop: "{{ list1.files + list2.files }}"
   ...

這是一個示例 Ansible 腳本,用於在遠程主機上復制多個文件

  • 名稱:復制遠程主機上的多個文件

ansible.windows.win_copy:

               src: "{{ SrcPath }}/{{ item}}" #Remeber to use {{item}} as a postfix to source path

               dest: "{{destPath}}"

               remote_src: yes [if Source Path available on remote Host]

               with_items:
                 - abc.txt
                 - abc.properties

將文件從多個目錄復制到多個目錄<\/strong>

我發現guenhter<\/a>答案很有幫助,但還需要更改遠程文件的模式。 在示例中,我將兩個目錄中的兩個文件復制到我首先創建的 \/tmp 和 \/tmp\/bin 中。 我沒有足夠的分數來對答案發表評論。 所以我把它放在這里。

- name: cpldupd
  hosts: test
  remote_user: root
  become: true
  vars:
    - rpth: /tmp
  tasks:
    - name: Create '{{rpth}}/bin'
      file:
        path: '{{rpth}}/bin'
        state: directory

    - name: Transfer
      copy: src={{ item.src }} dest={{ item.dest }} mode=0775
      with_items:
      - { src: '../utils/cpldupd', dest: '{{rpth}}/cpldupd' }
      - { src: '../utils/bin/cpldupd', dest: '{{rpth}}/bin/cpldupd' }

  • 主機:測試收集事實:假成為:真變量:路徑:'\/home\/ansibm\/playbooks'遠程路徑:'\/home\/{{ansible_ssh_user}}'目錄:'yml_files'任務:
    • 名稱:“為備份文件創建目錄”文件:路徑:'{{ remote_path }}\/{{ dir }}' 狀態:目錄所有者:'{{ansible_ssh_user}}' 組:'{{ansible_ssh_user}}' 模式:0700<\/li>
    • 名稱:“復制 yml 文件” 復制:src:'{{item}}' dest:'{{ remote_path }}\/{{ dir }}' 所有者:'{{ansible_ssh_user}}' 組:'{{ansible_ssh_user}} ' 模式:0644 循環: - '{{ path }}\/ab.html' - '{{ path }}\/cp.yml'<\/li><\/ul><\/li><\/ul>"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM