如果成功,获取文件并从源中删除它们
Fetch files and remove them from source if succesful
一段时间以来,我一直在使用 Ansible 从 Windows 个节点获取文件到 Linux 个节点,并取得了不错的效果。
我现在希望节点在成功上传后删除获取的文件。
但是,由于我从许多处于不同状态的端点获取数据,因此某些文件偶尔会无法传输 - 我在使用 Ansible 跳过这些文件时遇到了问题,而且只跳过了这些文件。
这是我目前的情况:
- 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 }}"
问题是,sync_result
变量似乎适用于每个节点而不是每个文件 - 也就是说,如果一个文件传输失败,则不会删除任何文件。
我尝试了各种循环和列表,但无法正常工作。
任何指点将不胜感激。
简而言之:
- 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 }}"
一段时间以来,我一直在使用 Ansible 从 Windows 个节点获取文件到 Linux 个节点,并取得了不错的效果。 我现在希望节点在成功上传后删除获取的文件。
但是,由于我从许多处于不同状态的端点获取数据,因此某些文件偶尔会无法传输 - 我在使用 Ansible 跳过这些文件时遇到了问题,而且只跳过了这些文件。
这是我目前的情况:
- 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 }}"
问题是,sync_result
变量似乎适用于每个节点而不是每个文件 - 也就是说,如果一个文件传输失败,则不会删除任何文件。
我尝试了各种循环和列表,但无法正常工作。
任何指点将不胜感激。
简而言之:
- 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 }}"