获取Ansible目录中的最新文件名

时间:2017-02-01 02:54:25

标签: ansible

我有一个Ansible脚本,我试图获取目录中最新项目的文件名。我正在使用这个Ansible脚本:

- name: Finding newest file in a folder
  find:
    paths: "/var/www/html/wwwroot/somefolder/"
    age: "latest"
    age_stamp: mtime

但是,我收到以下错误 -

FAILED! => {"age": "latest", "changed": false, "failed": true, "msg": "failed to process age"}

如何让Ansible检索目录中最新文件的文件名?

2 个答案:

答案 0 :(得分:21)

Pure Ansible解决方案:

- name: Get files in a folder
  find:
    paths: "/var/www/html/wwwroot/somefolder/"
  register: found_files

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

答案 1 :(得分:0)

使用系统命令:

- name: List letencrypt certificate directory
  command: ls -t1 /etc/letsencrypt/live/
  register: certificates

- set_fact: latest_certificate="{{ certificates.stdout_lines[0] }}"

这将保存certbot生成的最新目录。