这绝对是可能的,你甚至离解决方案很近。
您可能已经看到它已经对您的find 进行了debug,它的返回包含files 的列表,如果您没有文件,则此列表将为空。
所以使用 Jinja 可以很容易地查看列表何时为空
- debug:
msg: '{{ files.files if files.files|count > 0 else "cannot find any file" }}'
此语法使用inline if 和count filter。
关于您现在想要最新文件这一事实,您还可以使用一组 Jinja 文件管理器:filter sort 将帮助您按修改时间对文件进行排序,filter first 将帮助您仅获取您列出的第一个元素。
- debug:
msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path }}'
现在您只需将两者结合在一个长的 Jinja 表达式中:
- debug:
msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path if files.files|count > 0 else "cannot find any file" }}'
为了复制文件,您将需要一个额外的特定于 Ansible 的 Jinja 过滤器,即 basename,以便从完整路径中获取文件名
- debug:
msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path | basename if files.files|count > 0 else "cannot find any file" }}'
但您还需要when 语句,因此如果没有匹配的文件,您的副本将被跳过:
- name: Copy file, if found
copy:
src: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path }}'
dest: '/tmp/{{ (files.files | sort(attribute="mtime", reverse=true) | first).path | basename }}'
when: files.files|count > 0
供您测试的完整工作手册:
---
- hosts: localhost
connection: locale
vars:
var_files:
- { 'name': 'a', 'time': 86400 }
- { 'name': 'b', 'time': 30 }
- { 'name': 'c', 'time': 20 }
tasks:
- name: creating a bunch of matching files
file:
path: '/data/{{ item.name }}'
state: touch
with_items: '{{ var_files }}'
- name: aging those files
file:
path: '/data/{{ item.name }}'
modification_time: '{{ "%Y%m%d%H%M.%S" | strftime( ( ansible_date_time.epoch | int ) - item.time ) }}'
with_items: '{{ var_files }}'
- name: find the latest file
find: paths=/data
file_type=file
age=-1m
age_stamp=mtime
register: files
- debug:
msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path if files.files|count > 0 else "cannot find any file" }}'
- name: Copy file, if found
copy:
src: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path }}'
dest: '/tmp/{{ (files.files | sort(attribute="mtime", reverse=true) | first).path | basename }}'
when: files.files|count > 0
- name: removing files to test the behaviour with no matching files
file:
path: '/data/{{ item.name }}'
state: absent
with_items: '{{ var_files }}'
- name: find the latest file
find: paths=/data
file_type=file
age=-1m
age_stamp=mtime
register: files
- debug:
msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path if files.files|count > 0 else "cannot find any file" }}'
- name: Copy file, if found
copy:
src: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path }}'
dest: '/tmp/{{ (files.files | sort(attribute="mtime", reverse=true) | first).path | basename }}'
when: files.files|count > 0
以及该剧本的相应输出
PLAY [localhost] ********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [localhost]
TASK [creating a bunch of matching files] *******************************************************************************************************
changed: [localhost] => (item={'name': 'a', 'time': 86400})
changed: [localhost] => (item={'name': 'b', 'time': 30})
changed: [localhost] => (item={'name': 'c', 'time': 20})
TASK [aging those files] ************************************************************************************************************************
changed: [localhost] => (item={'name': 'a', 'time': 86400})
changed: [localhost] => (item={'name': 'b', 'time': 30})
changed: [localhost] => (item={'name': 'c', 'time': 20})
TASK [find the latest file] *********************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************
ok: [localhost] => {
"msg": "/data/c"
}
TASK [Copy file, if found] **********************************************************************************************************************
changed: [localhost]
TASK [removing files to test the behaviour with no matching files] ******************************************************************************
changed: [localhost] => (item={'name': 'a', 'time': 86400})
changed: [localhost] => (item={'name': 'b', 'time': 30})
changed: [localhost] => (item={'name': 'c', 'time': 20})
TASK [find the latest file] *********************************************************************************************************************
ok: [localhost]
TASK [debug] ************************************************************************************************************************************
ok: [localhost] => {
"msg": "cannot find any file"
}
TASK [Copy file, if found] **********************************************************************************************************************
skipping: [localhost]
PLAY RECAP **************************************************************************************************************************************
localhost : ok=9 changed=4 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0