简体   繁体   中英

Fetch files and remove them from source if succesful

I've been using Ansible to fetch files from Windows nodes to a Linux node for some time with good results. I would now like the nodes to remove fetched files once they have uploaded successfully.

However, since I'm fetching from lots of endpoints in various states, some files occasionally fail to transfer - and I'm having trouble using Ansible to skip those files, and those files only.

Here's what I have so far:

- name: Find .something files
    ansible.windows.win_find:
      paths: 'C:\Some\Path'
      patterns: [ '*.something' ]
      recurse: yes
      age_stamp: ctime
      age: -1w
    register: found_files
  - name: Create destination directory
    file:
      path: "/some/path/{{inventory_hostname}}/"
      state: directory
    delegate_to: localhost
  - name: Fetch .something files
    fetch:
      src: "{{ item.path }}"
      dest: "/some/path/{{inventory_hostname}}/"
      flat: yes
      validate_checksum: no
    with_items: "{{ found_files.files }}"
    register: item.sync_result
    ignore_errors: yes
      msg: "Would remove {{ item.path }}"
    when: sync_result is succeeded
    with_items: "{{ found_files.files }}"

The problem is, the sync_result variable seems to apply to each node instead of each file - that is, if one file has failed to transfer, no files will be deleted.

I've tried various loops and lists and could not get it to work.

Any pointers would be greatly appreciated.

In a nutshell:

  - name: Find .something files
    ansible.windows.win_find:
      paths: 'C:\Some\Path'
      patterns: [ '*.something' ]
      recurse: yes
      age_stamp: ctime
      age: -1w
    register: find_something

  - name: Create destination directory
    file:
      path: "/some/path/{{ inventory_hostname }}/"
      state: directory
    delegate_to: localhost

  - name: Fetch .something files
    fetch:
      src: "{{ item.path }}"
      dest: "/some/path/{{ inventory_hostname }}/"
      flat: yes
      validate_checksum: no
    loop: "{{ find_something.files }}"
    register: fetch_sync
    ignore_errors: yes

  - name: Delete successfully fetched files
    file:
      path: "{{ item.file }}"
      state: absent
    loop: "{{ fetch_sync.results | select('succeeded') }}"
    # If you are using ansible < 2.10 you need to cast to list i.e.
    # loop: "{{ fetch_sync.results | select('succeeded') | list }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM