Linux 在ansible中查找在最后一分钟内修改的最新文件?

Linux 在ansible中查找在最后一分钟内修改的最新文件?,linux,ansible,Linux,Ansible,我有一个特定目录中的文件列表,如下所示: 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@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成功返回
  • 如果有这样的文件,则将该文件复制到“/tmp”文件夹
  • 如果在最后一分钟生成或修改了多个文件,则使用最新的文件
在ansible中可以这样做吗?我看到ansible中有一个find模块,但不确定如何使用该模块使上述内容正常工作

---
- 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

这绝对是可能的,你甚至离解决方案很近

您可能已经看到它正在对您的
查找
执行
调试
,它的返回包含一个
文件列表
,如果您没有文件,该列表将为空

因此,当列表中没有Jinja时,很容易看到它

- debug:
    msg: '{{ files.files if files.files|count > 0 else "cannot find any file" }}'
此语法使用的是以及

考虑到您现在需要最新的文件,您还可以使用一组Jinja文件管理器:将帮助您按修改时间对文件进行排序,将帮助您仅获取列表的第一个元素

- 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筛选器,也就是说,为了从文件的完整路径中获取文件名

- debug:
    msg: '{{ (files.files | sort(attribute="mtime", reverse=true) | first).path | basename 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
供您测试的完整工作手册:

---
- 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

这看起来棒极了。谢谢你的详细解释。所以基本上
msg
变量将具有最新的文件详细信息,对吗?那么,我们也可以将这个最新的文件复制到/tmp文件夹吗?有没有办法将最新的文件名传递到ansible脚本的下一步,ansible脚本将负责将文件复制到/tmp文件夹?@flash-sure,您现在有了它为什么在上面的脚本中有
10000000w
?对于我的用例,我需要在1分钟内生成或修改的最新文件,对吗?所以应该是
age=-1m
?如果我是,请纠正我wrong@flash因为,从您的问题来看,这似乎不是您的问题,并且没有必要增加文件创建步骤的复杂性,我只是为了有一个完整的示例而添加这些步骤。但是如果这真的是你想要的,那么有更新的答案。是的,有意义。我想也许你忘了纠正。只是想确定一下。谢谢你的帮助!