【问题标题】:ansible to find latest files from a directory recursivelyansible递归地从目录中查找最新文件
【发布时间】:2021-03-15 18:14:12
【问题描述】:

我有一个要求,我需要递归地在目录中搜索某些文件。然后在每个有匹配的子目录下,获取最新的文件即可。

假设这是一个目录结构:

现在如您所见,我在两个子目录 AB 中突出显示了最新的 *.txt 文件,而 C 没有。

我下面的代码将从子目录中获取所有*.txt 文件。我只是不知道如何只使用 Ansible 获取最新文件并避免使用 shell 脚本。

  - name: Ansible find file examples
    find:
      paths: "/home/sarah/demo/"
      patterns: "*txt"
      recurse: yes
    register: files_matched

  - name: Get latest file
    set_fact:
      latest_file: "{{ files_matched.files | sort(attribute='mtime',reverse=true) }}"

  - debug:
      msg: "{{ item }}"
    with_items: "{{latest_file|map(attribute='path')|list}}"

我们将不胜感激。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您可以根据dirnamedirname 的条件为此创建一个列表(感谢map 过滤器的帮助)。

    使用剧本:

    - hosts: all
      gather_facts: no
    
      tasks:
          - find:
              paths: /home/sarah/demo
              patterns: "*.txt"
              recurse: yes
            register: files_matched
    
          - set_fact:
              latest_files: "{{ latest_files | default([])  + [item.path] }}"
            loop: "{{ files_matched.files | sort(attribute='mtime', reverse=true) }}"
            when: "item.path | dirname not in latest_files | default([]) | map('dirname')"
            ## 
            # The loop_control is just there for validation purpose
            ##
            loop_control:
              label: "{{ '%Y-%m-%d %H:%M:%S' | strftime(item.mtime) }} {{ item.path }}"
    
          - debug:
              var: latest_files
    

    这给了我一个回顾:

    PLAY [all] ********************************************************************************************************
    
    TASK [find] *******************************************************************************************************
    ok: [localhost]
    
    TASK [set_fact] ***************************************************************************************************
    ok: [localhost] => (item=2021-03-15 14:07:10 /home/sarah/demo/B/b1.txt)
    ok: [localhost] => (item=2021-03-15 14:06:16 /home/sarah/demo/A/a2.txt)
    skipping: [localhost] => (item=2021-03-15 14:06:05 /home/sarah/demo/B/b.txt) 
    skipping: [localhost] => (item=2021-03-15 14:05:46 /home/sarah/demo/A/a.txt) 
    skipping: [localhost] => (item=2021-03-15 14:05:38 /home/sarah/demo/A/a1.txt)  
    
    TASK [debug] ******************************************************************************************************
    ok: [localhost] => 
      latest_files:
      - /home/sarah/demo/B/b1.txt
      - /home/sarah/demo/A/a2.txt
    
    PLAY RECAP ********************************************************************************************************
    localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

    所有这些都复制了您的文件结构:

    A:
    total 0
    -rw-r--r--    1 root     root             0 Mar 15 14:05 a.txt
    -rw-r--r--    1 root     root             0 Mar 15 14:05 a1.txt
    -rw-r--r--    1 root     root             0 Mar 15 14:06 a2.txt
    
    B:
    total 0
    -rw-r--r--    1 root     root             0 Mar 15 14:06 b.txt
    -rw-r--r--    1 root     root             0 Mar 15 14:07 b1.txt
    
    C:
    total 0
    

    【讨论】:

    • 嗨@β.εηοιτ.βε 非常感谢。这太棒了,在完成了基本的 Ansible 课程后,我假设我几乎可以完成所有任务。很明显,我需要专注于 Jinja2 编程。你能推荐一些课程来掌握这部分ansible吗?
    • 整个 Jinja 文档都在这个页面上:jinja.palletsprojects.com/en/2.11.x/templates 并且还有一组由 Ansible 构建的额外 Jinja 功能(测试、过滤器等),您可以在那里阅读:@987654324 @.
    • β.εηοιτ.βε:非常感谢。我希望我能用语言更好地表达自己。
    猜你喜欢
    • 2011-06-01
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多