- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个特定目录中的文件列表,如下所示:
david@host:~/jobs/process/workspace/files$ ls -lrth
total 68K
-rw-r--r-- 1 david david 7.8K Oct 1 11:10 golden_proc.init.1569953435497
-rw-r--r-- 1 david david 7.7K Oct 2 12:11 golden_proc.init.1570043494149
-rw-r--r-- 1 david david 7.7K Oct 2 20:15 golden_proc.init.1570072510929
每个文件名都以时间戳结尾。现在我需要找到一个最新的文件,该文件在一分钟前在 ansible 中被修改或创建。
这可以在 ansible 中实现吗?我看到 ansible 中有一个查找模块,但不确定如何使用该模块使上述功能正常工作?
---
- name: Play 1
hosts: 127.0.0.1
tasks:
- name: find the latest file
find: paths=/var/lib/jobs/workspace/process/files
file_type=file
age=-{{ time_window }}m
age_stamp=mtime
register: files
最佳答案
这绝对是可能的,您甚至离解决方案还很近。
正如您可能已经看到它已经对您的find
进行了调试
,它的返回包含一个文件
列表,这个列表如果您没有文件,将只是空的。
所以在 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
关于linux - 在 ansible 中找到最后一分钟内修改的最新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243105/
这个问题在这里已经有了答案: Different ways of loading a file as an InputStream (6 个答案) 关闭 8 年前。 在我的 gradle java
给定一个 User 类: class User end 我想使用 .class_eval 定义一个新常量.所以: User.class_eval { AVOCADO = 'fruit' } 如果我尝试
这可能听起来很奇怪,但我正在开发一个需要查找 div 内的元素或 div 本身的插件。 脚本根据用户选择查找元素,但内容(包括标记)是可变的。因此脚本将按如下方式查找元素: $('.block').f
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我需要在按我自己的函数排序的对的多集中查找并删除一个值。显然, .find 总是将迭代器返回到末尾,而不是返回到搜索到的值。有小费吗?这是函数: struct cmp { bool operato
求助!我将如何通过遍历查看字符并计算有效字符出现之前的下划线数量来查找和删除前导下划线。以及从字符串末尾向后迭代以查找任何尾随下划线。 我可以使用下面的方法来删除下划线,但是如何迭代才能找到下划线。
如果你在 $(xml) 中有下面的 xml,你会变得懒惰: $(xml).find("animal").find("dog").find("beagle").text() 在 jQuery 中是否有类
你如何找到4个文件的交集? 我用了grep -Fx -f 1.txt 2.txt 3.txt 4.txt ,但它似乎只适用于 2 个文件。同样comm -12 1.txt 2.txt无法扩展为 4 个
我已经完成了标记的姿势估计并获得了 rvec 和 tvec 值。我不知道如何找到它的中心,因为我需要绘制一个需要中心值的圆柱体。 我该怎么做? 最佳答案 标记的 tvec 是标记从原点的平移 (x,y
我有一个任务,我需要找到 2 个单链接(单对单)列表的交集。我还必须为 2 个双向链接(双重 vs 双重)列表执行此操作: 对于单链表,我使用 mergeSort() 对两个列表进行排序,然后逐项比较
我是 R 的新手,我有一个 100x100 的方阵。我想找到这个矩阵的最大特征值。我试过了 is.indefinite(x) 但是它写 is.indefinite(x) : argument x is
您好,我是 svg 和 JavaScript 的新手,当鼠标位于 svg 上方时,我试图使一些 svg 元素弹出(通过缩放),反之亦然,当鼠标离开 svg 元素时。 我已经能够通过使用转换使 svg
我正在尝试为 scala 项目编写一个类,但在多个地方使用 class、def、while 等关键字出现此错误。 它发生在这样的地方: var continue = true while (conti
我有两个 pandas 数据框,它们只取自一列并将日期列设置为索引,所以现在我有两个 Series。我需要找到这些系列的相关性。 这里有几行来自dfd: index change 2018-
我正在尝试调整我的 Vagrantfile,因此如果它丢失,它会自动在项目根目录中创建一个文件夹。创建文件夹没问题,但我无法找到创建该文件夹的位置。 我发现此信息可在 Vagrant::Environ
我正在尝试在 jquery 中找到 Test3 的位置,请有人引导我走上正确的道路。 我需要jquery来显示5 Test7 Test2 Test6 Test5 Test3 Test8 谢谢 最佳
大家早上好 我有一个像这样的图像列表: 使用 jQuery 如何查找 ul#preload 中包含特定字符串(例如“green”)的所有图像 src 类似... var new_src = j
我正在开发一个修改 Excel 文件的应用程序。 如何找到任意行中最后使用的单元格? 示例:行号 => 5 中最后使用的单元格 最佳答案 要找到一行中的最后一个单元格,您需要 Range 的 End
我刚刚陷入 react native ,需要一些帮助才能在找到 token 时导航到 protected 屏幕。我应该在哪里寻找应用程序加载时的 token ?如何在不多次调用导航的情况下导航用户一次
非常奇怪...此页面是 protected 内容还是我不知道的内容?我尝试单击下一页 anchor 。 参见this page first. 我试图用这个来抓取元素 var buttonNext =
我是一名优秀的程序员,十分优秀!